(* CS:3820, Fall 2018 *) (* Cesare Tinelli *) (* The University of Iowa *) (* F# examples seen in class *) (* Simple types *) 3 + 5 3.0 1.4 + 3.0 "hi" // some operator names are overloaded 1.4 + 3.0 "hi" + " there" // no implicit type conversions 1.4 + 3 // error (* Boolean type *) true not true true && false true || false 4 = 5 // comparison operator, not assignment! 4 < 5 (* Variable binding *) let a = 3 // declares symbolic value a as a synonym for 3 a // evaluates to 3 a = 4 // Boolean expression, not assignment! let b = 4 + a (* Type ascription *) let c:string = 55 // requires c to be of type string let c:float = 7 // type mismatch error // any term can be annotated with a type constraint (4:int) (((4:int) = 1):bool) (* logical operators, conditional expressions *) if 3 < 4 then "yes" else "no" // The if construct is an expression. // It can be seen as a mixfix operator with three arguments: B, T1 and T2 // // if B then T1 else T2 // // B must be of type bool. T1 and T2 must be of the same type. // The type of the whole if is the type of T1 and T2 if 3 < 4 then "yes" else 4 // ill-typed let c = 5 if (c > 0) then 10 else 20 // if expressions can go everywhere an expression of can go (if (c > 0) then 1 else 0) + 7 let r1 = (if 3 < 4 then "yes" else "no") let r2 = (if 3 < 4 then "yes" else "no") + ", sir" (* tuple types *) (1,3) (3,4,5) (3,"pp",4.3) // note the different types of these tuples (1,2,3) ((1,2),3) (1,(2,3)) ((1,2),3) = (1,2,3) // ill-typed // the empty tuple is a value () // () is the *only* value of type unit // it is used for instance for functions // that are called only for their side effect