> 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
Archive for September, 2008
SICP Exercises in F#, Exercise 1.4
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
SICP Exercises in F#, Exercise 1.3
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
> 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
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
> 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
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
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
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
> 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
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
> ((4 * 6) + 2) * (3 + 5 + 7);; val it : int = 390
SICP Exercises in F#, Section 1.1.2
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
> 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
SICP Exercises in F#, Section 1.1.1
Published September 28, 2008 Software Leave a CommentTags: F#, SICP
> 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
Pascal is for building pyramids — imposing, breathtaking, static structures built by armies pushing heavy blocks into place. Lisp is for building organisms — imposing, breathtaking, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place.
— from the foreword to Structure and Interpretation of Computer Programs
Recent Comments