F # Create a single loop with multiple functions

I am just starting to delve into some programs and decided to go with F #. As a practice, I tried to convert the script I made in .bat to F #. I had a problem creating a looping function that does more than one. Here is the code from the old script for this loop.

:Select
cls
echo.
echo Which Game?
echo.
echo    1. Assassin Creed
echo    2. Crysis
echo    3. Mass Effect
echo.
echo.
set/p "game=>"
if /I %game%==1 goto Creed
if /I %game%==2 goto Crysis
if /I %game%==3 goto Mass
echo.
echo Invalid selection!
echo.
echo.
pause
goto Select

The code I tried to do for the same function in F # looks like this:

let rec gameprint gameselect =
    printfn "Which Game?\n\n   1.%s\n   2.%s\n   3.%s\n\n\n" game1 game2 game3
    let mutable gameselect = Int32.Parse(stdin.ReadLine())
    if gameselect = "1" then game1
    elif gameselect = "2" then game2
    elif gameselect = "3" then game3
    else printf "temp"
    Console.Clear

I know that I am missing something that says it starts up again if it reaches the last "else"; and I get these errors:

The expression must be of type 'unit', but of type 'string'. Use "ignore" to cancel the result of the expression, or "let" bind the result to the name.

1 int, 19 21

2 int, 20 23

3 int, 21 23

4 , , 22 17

5 'unit', 'string'. "", , "" . 19 5

( ):

let rec getGame() =
    match Int32.Parse(stdin.ReadLine()) with
    | 1 -> "Assassin Creed"
    | 2 -> "Crysis"
    | 3 -> "Mass Effect"
    | _ -> printf "Temp"

:

1 , , 36 19

, , "printf" "Console.Clear"

, , , , : -)

!

+3
2

, string int, . 1, 2 3 "1", "2" "3" , , .

, F # , : , -. , , , :

let rec getGame() =
    match Int32.Parse(stdin.ReadLine()) with
    | 1 -> "Assassin Creed"
    | 2 -> "Crysis"
    | 3 -> "Mass Effect"
    | _ -> printf "Temp"; getGame()

:

type Game = Creed | Crysis | Mass

let rec getGame() =
  printfn "Which Game?\n\n   1.%A\n   2.%A\n   3.%A\n\n" Creed Crysis Mass
  match stdin.ReadLine() |> Int32.TryParse with
  | true,1 -> Creed
  | true,2 -> Crysis
  | true,3 -> Mass
  | _ -> 
     printfn "You did not enter a valid choice."
     // put any other actions you want into here
     getGame()
+7

! xD

:

#light
open System
open System.IO
//Simplify pausing
let pause() = Console.ReadLine()
//Identify the durrent drive letter associated with the flash drive for use
//when saving/deleting Save Data
let drive = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory())
printfn "You're using the %s drive.\n\n" drive

//Identify the games to save
let game1 = "Assassin Creed"
let game2 = "Crysis"
let game3 = "Mass Effect"

//Identify which game to Save/Load the data for
let rec getGame() =
  printfn "Which Game?\n\n   1.%s\n   2.%s\n   3.%s\n\n" game1 game2 game3
  match Int32.TryParse(stdin.ReadLine()) with
  | true,1 -> game1
  | true,2 -> game2
  | true,3 -> game3
  | _ ->
     printfn "You did not enter a valid choice."
     //The '_' is to ignore the result of Console.Readline() without binding it to anything
     //that way you can re-use the same line as many times as you like
     let _ = pause()
     Console.Clear()
     getGame()

//Print the selected game
let gameprint = getGame()
printf "You have chosen %s\n\n" gameprint
let _ = pause()

:

Type Game = Creed | Crysis | Mass

"printf" getGame()

, .

0

Source: https://habr.com/ru/post/1764952/


All Articles