
Oct 22, 2008
Chris Smith, a Microsoft SDET on the F# team, has a new book coming out next year called “Learning F#”. I can’t wait to see it – there are only a handful of F# books at all right now, and we welcome a new addition.
If you’re into F#, you should add Chris’ RSS feed to your reader. He has some great posts.

Sep 28, 2008
> let a_plus_abs_b a b =
let add a b = a + b
let sub a b = a - b
if b > 0 then
add a b
else
sub a b;;
val a_plus_abs_b : int -> int -> int
> a_plus_abs_b 1 -4;;
val it : int = 5

Sep 28, 2008
> let proc3 a b c =
let sqr x = x * x
let sum_of_squares m n = sqr m + sqr n
let biggest m n = if m > n then m else n
let smallest m n = if m > n then n else m
sum_of_squares (biggest a b) (biggest (smallest a b) c);;
val proc3 : int -> int -> int -> int
> proc3 2 3 4;;
val it : int = 25

Sep 28, 2008
> 10;;
val it : int = 10
> 5 + 3 + 4;;
val it : int = 12
> 9 - 1;;
val it : int = 8
> 6 / 2;;
val it : int = 3
> (2 * 4) + (4 - 6);;
val it : int = 6
> let a = 3;;
val a : int
> let b = a + 1;;
val b : int
> b;;
val it : int = 4
> a + b + (a * b);;
val it : int = 19
> (a = b);;
val it : bool = false
> if (b > a) && (b if (a = 4) then 6
elif (b = 4) then (6 + 7 + a)
else 25;;
val it : int = 16
> 2 + (if b > a then b else a);;
val it : int = 6
> if (a > b) then a
elif (a (if (a > b) then a
elif (a < b) then b
else -1) * (a + 1);;
val it : int = 16

Sep 28, 2008
NOTE: No exercises necessary for section 1.1.5
> let abs = function
| n when n > 0 -> n
| n when n -n
| _ -> 0;;
val abs : int -> int
> let abs x =
match x with
| _ when x -x
| _ -> x;;
val abs : int -> int
> abs -12;;
val it : int = 12
> abs 12;;
val it : int = 12
> abs 0;;
val it : int = 0
> let abs x =
if (x int
> let r1 x =
(x > 5) && (x bool
> r1 5;;
val it : bool = false
> r1 6;;
val it : bool = true

Sep 28, 2008
> let square x = x * x;;
val square : int -> int
> square 21;;
val it : int = 441
> square (2 + 5);;
val it : int = 49
> square (square 3);;
val it : int = 81
> let sum_of_squares x y = (square x) + (square y);;
val sum_of_squares : int -> int -> int
> sum_of_squares 3 4;;
val it : int = 25
> let f a = sum_of_squares (a + 1) (a * 2);;
val f : int -> int
> f 5;;
val it : int = 136

Sep 28, 2008
> ((4 * 6) + 2) * (3 + 5 + 7);;
val it : int = 390

Sep 28, 2008
> let size = 2;;
val size : int
> size;;
val it : int = 2
> 5 * size;;
val it : int = 10
> let pi = 3.14159
let radius = (float)10;;
val pi : float
val radius : float
> pi * (radius * radius);;
val it : float = 314.159
> let circumference = 2 * pi * radius;;
let circumference = 2 * pi * radius;;
------------------------^^^
stdin(34,25): error FS0001: The type 'float' does not match the type 'int'.
> let circumference = 2.0 * pi * radius;;
val circumference : float
> circumference;;
val it : float = 62.8318

Sep 28, 2008
> 486;;
val it : int = 486
> 137 + 349;;
val it : int = 486
> 1000 - 334;;
val it : int = 666
> 5 * 99;;
val it : int = 495
> 10 / 5;;
val it : int = 2
> 2.7 + 10;;
2.7 + 10;;
------^^^
stdin(11,7): error FS0001: The type 'int' does not match the type 'float'.
> 2.7 + (float)10;;
val it : float = 12.7
> 21 + 35 + 12 + 7;;
val it : int = 75
> 25 * 4 * 12;;
val it : int = 1200
> (3 * 5) + (10 - 6);;
val it : int = 19
> (3 * ((2 * 4) + (3 + 5)) + ((10 - 7) + 6);;
(3 * ((2 * 4) + (3 + 5)) + ((10 - 7) + 6);;
-----------------------------------------^^^
stdin(16,42): error FS0010: Unexpected symbol ';;' in expression. Expected ')' or other token
> (3 * ((2 * 4) + (3 + 5))) + ((10 - 7) + 6);;
val it : int = 57

Sep 23, 2008
F# functions are weird. OK, that’s the C# programmer in me speaking. I’m going to make a point of writing up F# fundamentals over the next month to (a) help me understand what’s going on; and (b) to help you, the Searcher Brought By The Google, to understand if you search for something like “first f# function”.
So: function declarations. You can do them several ways. First, the direct way:
let sqr x = x * x;;
- This declares a function sqr that takes a single parameter x, and returns the square. Simple? Maybe. There’s a lot going on.
- Functions are declared the same way that normal variables are, using the let statement.
- You don’t need to declare types. F# will infer the types for you. In this case, it will assume x is an integer value, since you’re multiplying it in the body of the function.
Further, there are alternate ways to declare the same function. Here’s one, from F# for Scientists, by Dr. Jon D. Harrop (p. 13):
let sqr = fun x -> x * x;;
This one says, assign the sqr variable to an anonymous function that takes a single parameter x and returns the square of x. This shows even more clearly that functions are first-class citizens in F#.
How about this one?
let sqr = function
| x -> x * x;;
This uses F#’s pattern-matching syntax.
So, there are several ways to skin that particular cat. Which one is best? F# seems to encourage terseness, so I would keep that in mind.