1
2
3
let main() = 
      i = 5
      printf "%i",i

Don't put a comma between function arguments. Write

1
printf "%i" i

. In F# commas are used for tuples.

By on 3/30/2010 4:31 PM ()

printf doesn't take tupled arguments - you need to get rid of the comma on the printf line. As it is right now,

printf "%i", i

evaluates like

return a 2-tuple containing the two values

printfn "%i" // a function that will print a value

i // an int

which has that weird result type.

You might want to check out

[link:lorgonblog.spaces.live.com]

for some starter info about calling functions with either 'tupled' syntax or 'curried' syntax.

See here regarding posting well-formatted code:

[link:cs.hubfs.net]

By on 3/30/2010 4:28 PM ()

Hey thanks a million for the reply, and for getting back so quick - can go to sleep happy
I can't believe I missed that - was changing all sorts of things but not that
Thanks for the link, and yes, I'll read up and make sure to post code right next time
Thanks again!

By on 3/30/2010 4:34 PM ()

Hi again,
Still having trouble getting what I do in the REPL to work in a file for compilation.
(I'm afraid I haven't sorted out the code formatting yet - using firefox for ubuntu - seems a bit different, or I'm blind (could be that) )

When I do this in the REPL it's fine..
___________________________
let rec test2 list =
if (List.length list) = 0 then []
else printf "%i " (List.head list) :: test2(List.tail list);;

test2[1;2;3;4];;
___________________________
I cannot, however, replicate these results in a file. I first tried the above exactly (minus the double semi-colons of course)
I have since tried something like this -
___________________________
let rec test2 list =
if (List.length list) = 0 then []
else printf "%i " (List.head list):: test2(List.tail list)

test2

let x =
test2[1;2;3]
x
___________________________
I get an error at line 5 (test2) : the function value is missing arguments, but when I tried to give it arguments, i.e. test2[1;2;3], I get a different error.
I get an error at line 9 (x) : this expression should have type unit but has type unit list.

Essentially my problem is that I can't really figure out how to resolve functions for a file as opposed to on the REPL. I really need to use files, because I'm on linux, and the REPL in the terminial is pretty sucky. I've tried looking at examples in the documentation but something just isn't clicking.
I would really appreciate it if I could just get a simple demonstration of how to conclude a function.
Thanks a million!

By on 3/31/2010 10:46 AM ()

test2 takes an int list as an argument and returns type unit list. Since the return type is non-unit, F# requires you to use 'let' to bind the return value to a name.

So, the following will both "call" the function and bind the return value to x in one statement:

1
2
let x = 
    test2 [1;2;3]

If you really don't want to use the let binding, you can use the ignore function to "throw away" the return value like this:

1
2
 
ignore (test2 [1;2;3])

or more idiomatically like this:

1
2
3
4
5
6
 

test2 [1;2;3] 
|> ignore

By on 3/31/2010 1:35 PM ()

That's great - thanks.
Think I'll go with the let binding way.
Thanks again!

By on 3/31/2010 3:47 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper