tags:

views:

104

answers:

2

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

A: 

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

Brian
Whoops, it already was in my code, but it transferred badly. Fixed my example.
JBristow
+4  A: 

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

Jose Basilio
However, if you're using the most recent version of F#, you shouldn't need `#light` (I'm not sure if that works well on Mac, but I believe it should - if no, please report issues to _fsbugs (at) microsoft.com_). It is a good idea to use the most recent version as there are some improvements in every release :-)
Tomas Petricek
Yes, this version is clearly very old - upgrade to RTM, just announced today! http://blogs.msdn.com/dsyme/archive/2010/04/12/f-2-0-released-as-part-of-visual-studio-2010.aspx
Brian
Heh, since I have it working on my PC as well in VS.Net2010, I'm going to be lazy... Is there a way to force darwinports to get a later release than 1.9.4.19 ???
JBristow