RvBVakama
RvBVakama

Reputation: 101

F# if statement function with parameter syntax

The issue is: I cannot figure out what the error is refering to when it diplays

Here is the error: source_file.fs(10,5): error FS0010: Unexpected keyword 'if' in binding. Expected '=' or other token.

And I've been researching this error and proper syntax for a good while.

Now what I want to do, I hope, is obvious from the general look of the program.

Knowing the correct syntax would be great as microsofts docs are not great.

Seeing as this is case, I just don't understand what could be wrong.

open System

let one = "one"
let two = "two"

if oneortwo one then printfn one + " 1"
else printfn two + " 2"

let oneortwo(a : string)
    if a = "one" then return true
    elif a = "two" then return false

return false

Upvotes: 1

Views: 279

Answers (1)

nilekirk
nilekirk

Reputation: 2383

F# is an expression based language, which means that everything has a value (returns something). F# is also statically typed, so everything returned is of a specific type.

Since everything is an expression, the return keyword is not used. The final expression in a function body is the returned value.

This goes also for if ... then ... else: every branch must return a value and be of the same type.

The correct syntax for your function is

let oneortwo a =
    if a = "one" then true
    else false

An excellent source of learning F# is Scott Wlaschin's site F# for fun and profit

Upvotes: 6

Related Questions