Posts Tagged 'F#'

New F# Book By Chris Smith Coming Out in 2009

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.

Advertisements

SICP Exercises in F#, Exercise 1.4

> 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

SICP Exercises in F#, Exercise 1.3

> 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

SICP Exercises in F#, Exercises 1.1

> 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

SICP Exercises in F#, Section 1.1.6

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

SICP Exercises in F#, Section 1.1.4

> 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

SICP Exercises in F#, Section 1.1.3

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