【发布时间】:2017-05-12 23:41:20
【问题描述】:
尝试 F#,今天学到了很多,不确定我是否正在尝试,但我有一个模式匹配和递归,由于某种原因我无法从递归调用它。
// Define my active recognizer for keywords
let(|MyGirlFriend|Bye|) input =
match input with
|"lonely|"love"|"friendship"
-> MyGirlFriend
|"goodbye"|"bye"|"go"|
-> Bye
|_
-> None
我认为上面的代码看起来是对的。
//recursive response function
let rec response (token: string) (str: string) =
match token with
| Bye
-> good_bye_response ()
| RoomLocation
-> sprintf "%s" "Your call is log. Do you wish to quit?"
|_ when token.Contains("yes") -> "good bye" 0
|_ when token.Contains("no") -> answer_response ()
| None when (str.IndexOf(" ") > 0)
-> response (str.Substring(0,str.IndexOf(" ")))
(str.Substring(str.IndexOf(" ")+1))
| None when (str.IndexOf(" ") < 0)
-> response str ""
我的功能是:
let rec chat () =
if Break = false then
let valueInput = Console.ReadLine()
printf "Helpdesk-BCU Response --> %s \n" (response "" valueInput)
if Break = false then
chat()
else
ChatEnd()
let BCU_response (str: string) =
if (str.IndexOf(" ") > 0) then
response (str.Substring(0,str.IndexOf(" "))) (str.Substring(str.IndexOf("
")+1)) + "\n"
else
response str "" + "\n"
这里有几个问题 |_ when token.Contains("yes") -> "goodbye" 0 F# 中用作出口的零在这里我得到一条红线,它指出表达式应该具有类型字符串但有 int 类型,我知道零是 int。
那么我该如何退出递归循环呢?
欢迎任何建议
【问题讨论】:
-
缩进已关闭。请更正。