#light module Five printfn "%A" (1 + 2 * 3) // p. 66 printfn "%A" ("H"," ", 'H') // p. 68 printfn "%A" (-1 + 2 - 3 * 4 / 5 % 6) printfn "%A" (-1.0+2.0-3.0*4.0/5.0) printfn "%A" ("bibity" + "bobity" + "boo") printfn "%A" (2<3, 1.0<=1.0, 'd'>'c', "abce">"abd") // p. 69 printfn "%A" (1<2 || 3>4, 1<2 && not (3<4), true || 1/0=0) printfn "%A" (if 1<2 then 'x' else 'y') // p. 70 printfn "%A" (if 1>2 then 34 else 56) printfn "%A" ((if 1<2 then 34 else 56) + 1) printfn "%A" (if 1<2 then print_any 34) // F#, 'if' needs unit type printfn "%A" (float(123),floor(3.6), floor 3.6, string 'a') // p. 72 printfn "%A" (ceil 3.6, round 3.6, truncate 3.6, int 'a', char 97) let x = 1 + 2 * 3 // p. 73 let y = if x = 7 then 1.0 else 2.0 let fred = 23 //let fred = true // error, cannot redefine top-level (let fred = true printfn "%A" (x, y, fred) // fred has local scope ) printfn "%A" (fred) // uses top-level fred let barney = (1+2, 3.0*4.0, "brown") // p. 74 let point1 = ("red", (300, 200)) let _, b, _ = barney // p. 75 printfn "%A" b let xx = (1,2,3) let (_, (c,_)) = point1 printfn "%A" c match point1 with | (_, (cc,_)) -> printfn "%A" (cc) printfn "%A" ([1;2;3]) printfn "%A" ([1.0; 2.0]) printfn "%A" ([true]) let x1 = [1;2;3] printfn "%A" x1.IsEmpty // p. 76 printfn "%A" ([1;2;3]@[4;5;6]) let x2 = 'c' :: []; let y2 = 'b' :: x2 // p. 77 let z2 = 'a' :: y2 printfn "%A" (x2, y2, z2) printfn "%A" (x1, x1.Head, x1.Tail, x1.Tail.Tail, x1.Tail.Tail.Tail) let explode (s : string) // p. 78 = s.ToCharArray() |> Array.to_list printfn "%A" (explode "fred") let firstChar s = (explode s).Head printfn "%A" (firstChar "abc") let quot (a,b) = a/b printfn "%A" (quot (6,2)) // p. 79 let pair = (6,2) printfn "%A" (quot pair) let rec fact n = if n=0 then 1 else n*fact(n-1) printfn "%A" (fact 5) let rec listsum (x : int list) = if x.IsEmpty then 0 // p. 80 else x.Head + listsum x.Tail printfn "%A" (listsum [1;2;3;4;5]) let rec length (x : 'a list) = if x.IsEmpty then 0 else 1 + length x.Tail printfn "%A" (length [true; false; true]) printfn "%A" (length [4.0; 3.0; 2.0; 1.0]) let rec badlength (x : 'a list) = if x=[] then 0 else 1 + length x.Tail printfn "%A" (badlength [true; false; true]) printfn "%A" (badlength [4.0; 3.0; 2.0; 1.0]) // p. 81 let rec reverse (L : 'a list) = if L.IsEmpty then [] else reverse(L.Tail) @ [L.Head] printfn "%A" (reverse [1;2;3]) let prod (a,b) = a*b let prodf (a:float, b:float) : float = a * b