0
votes

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.

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

so how do I exit the recursive loop?

any sugguestion would be most welcome

1
Indentation is off. Please correct. - Fyodor Soikin

1 Answers

2
votes

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. Otherwise, it just returns without asking more questions.