Hide this comment

Perhaps it's best to go from an expression that contains a function call:

1
1+foo(2+3)+4

Such expressions can be thought of as a series of single value bindings that use primitive operations involving literals and bindings only. (The compiler generates new names automatically):

1
2
3
4
5
let b1 = 2 + 3
let a1 = foo(b1)
let res1 = 1 + a1
let res2 = res1 + 4
res2

As you can see, one primitive operation is function application itself - applying a function to one or more arguments (that each have been eagerly evaluated first). As such, function application always returns a value, even if that value is (), the empty value.

Various F# constructs, including the imperative for, while, etc. ones are broken down in a similar way, each resulting in a value (for instance, the return value of a for is the value from the last iteration). Therefore, these are not "statements" as known in other languages.

By on 12/6/2011 11:23 AM ()Reply
Hide this comment

"the return value of a for is the value from the last iteration"

That means

1
for x in [1; 2; 3] do x

should return 3, but it actually ignores the return value (asserting the body to be of type unit) and compiles with a warning: This expression should have type 'unit', but has type 'int'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name..

By on 12/7/2011 11:09 AM ()Reply
Hide this comment

You are right, the compiler also enforces that the body of the loop has type unit, although it's debatable whether such restriction is necessary.

By on 12/7/2011 11:34 AM ()Reply
Hide this comment

so,

1
let func a b = a + b

is equivalent to

1
let nofunc = 2 + 3

with the only difference consisting in evaluating the first expression each time when applied to new arguments like I presumed above?
That is how a function in F# is in fact an expression, right?
and every construct in F# evaluates to an expression. which means it is a true functional language, with only difference it allows mutable state?

By on 12/7/2011 10:46 AM ()Reply
Hide this comment

Kind of, yes.

By on 12/7/2011 11:43 AM ()Reply
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

Logging in...