简体   繁体   English

Mac上的F#:内部函数的语法错误

[英]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: 在mac上编译时产生以下错误,但在Visual Studio 2010中没有:

nth.fs(10,0): error FS0191: syntax error. nth.fs(10,0):错误FS0191:语法错误。

nth.fs(4,4): error FS0191: no matching 'in' found for this 'let'. nth.fs(4,4):错误FS0191:找不到匹配'in'这个'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. 你需要缩进match线。

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#? 你的mac有可能有一些更旧版本的F#吗? What version does fsc.exe report? fsc.exe报告什么版本? (If it is very very old, try adding "#light" as the first line.) (如果它非常老,请尝试添加“#light”作为第一行。)

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

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