简体   繁体   English

无法从递归f#调用函数

[英]Unable to call a function from recursive f#

trying out F#, learnt a lot today not sure if I am doing this try but I have a pattern match and recursive for some reason I am unable to call this from recursive. 尝试F#,今天不确定了我是否正在做这个尝试,但中学到了很多东西,但是由于某种原因我有模式匹配和递归,所以我无法从递归中调用它。

// Define my active recognizer for keywords
let(|MyGirlFriend|Bye|) input =
   match input with
   |"lonely|"love"|"friendship"
       -> MyGirlFriend
   |"goodbye"|"bye"|"go"|
      -> Bye
   |_ 
        -> None 

I think the above code that above looks right. 我认为上面看起来正确的上述代码。

//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 ""       

my function is : 我的职能是:

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"

a couple of issues here |_ when token.Contains("yes") -> "goodbye" 0 the zero that is used in F# as an exit on here I get a red line and it states expression should have type string but has type int, I know zero is an int. 这里有几个问题| _当token.Contains(“ yes”)->“再见” 0在F#中用作此处出口的零时,我得到一条红线,它指出表达式应该具有字符串类型,但具有类型整数,我知道零是整数。

so how do I exit the recursive loop? 那么如何退出递归循环?

any sugguestion would be most welcome 任何建议都将受到欢迎

It is not entirely clear what part are you struggling with, because there is quite a lot of minor issues in the code. 目前尚不清楚您在苦苦挣扎,因为代码中有很多小问题。 However, a minimal working example that shows how to do the recursion is something like this: 但是,一个最小的工作示例显示了如何进行递归,如下所示:

open System

let (|Bye|Other|) input =
  match input with
  | "goodbye" | "bye" | "go" -> Bye
  | _ -> Other

let response (token: string) =
  match token with
  | Bye -> false, "bye!"
  | Other -> true, "sorry, I didn't get that"

let rec chat () =
  let input = Console.ReadLine()
  let keepRunning, message = response input
  printfn ">> %s" message
  if keepRunning then chat ()

The response function now also returns a Boolean - if this is true , the chat function calls itself recursively to ask another question. 现在, response函数还返回一个布尔值-如果为true ,则chat函数将递归调用自身以询问另一个问题。 Otherwise, it just returns without asking more questions. 否则,它只会返回而不会询问更多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM