开发者

F# on Mac: Syntax Error on Inner Function

The following code:

exception NoElements of string

let nth(k, list) =
    let rec loop count list = 
        match list with
        | head :: _ when count = k -> head
        | _ :: tail when count <> k -> loop (count+1) tail
        | [] -> raise (NoElements("No Elements"))
    loop 0 list
;;
printfn "%A" (nth(2, [1; 1; 2; 3; 5; 8]))

Produces the following errors when compiling on a mac, but not in Visual Studio 2010:

nth.fs(10,0): error FS0191: syntax error.

nth.fs(4,4): error FS0191: no matching 'in' found for this 'let'.

开发者_开发问答


Make sure you are using the lightweight syntax directive at the top of your code

#light

(This is only necessary for old versions of the compiler; grab a newer version.)


You need to indent the match line.

Note also that list elements should be separated by semicolons; you have a one-element list containing a six-tuple.

Here's a working version:

exception NoElements of string 

let nth(k, list) = 
    let rec loop count list =  
        match list with 
        | head :: _ when count = k -> head 
        | _ :: tail when count <> k -> loop (count+1) tail 
        | [] -> raise (NoElements("No Elements")) 
    loop 0 list 

printfn "%A" (nth(2, [1; 1; 2; 3; 5; 8]))

Is it possible your mac has some much older version of F#? What version does fsc.exe report? (If it is very very old, try adding "#light" as the first line.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜