(* CS:4420 Artificial Intelligence Spring 2019 The University of Iowa Instructor: Cesare Tinelli *) (* OCaml examples seen in class *) (* This is a multi-line comment. Ocaml has no special syntax single line comments (* comments can be nested *) *) (* Copy declarations from this file and past them in the OCaml interactive top-level *) (* int values *) 3 + 5 ;; 4 - 2 ;; (* the symbol ;; tells the OCaml top-level to evaluate the expression entered so far *) (* float values *) 3.7 ;; (* the + operators is not overloaded *) 1.4 + 3.0 ;; (* no implicit conversions either! *) 1.4 + 3 ;; (* string values *) "hi" ;; "hi" ^ " there" ;; String.length "aaa" ;; (* bool values *) true ;; not true ;; true && false ;; true || false ;; (* comparison operator, not assignment! *) 4 = 5 ;; 4 < 5 ;; (* Variable binding *) (* declares symbolic constant n as a synonym for 3 *) let n = 3 ;; (* evaluates to 3 *) n ;; (* Boolen expression, not assignment! *) n = 4 let m = 4 + n (* Symbolic constants (immutable variables) vs references (mutable variables) *) (* declares symbolic constant v as a synonym of 3 *) let v = 3 ;; v ;; (* declares a _reference_ r *) let r = ref 3 ;; r ;; (* the value/content of a reference is extracted with ! *) !r ;; (* this is the same as !r *) r.contents ;; (* references are mutable *) r := 4 ;; !r ;; (* increments r by 1 *) r := !r + 1 ;; (* error, constants are immutable *) v := 4 ;; (* expressions with side effects *) (* assignments are also expressions ... *) r := 5 ;; let w = (r := 0) ;; (* ... of type unit *) (* Type ascription *) (* requiring c to be of type string *) let c : string = 55 ;; (* type mismatch error, no implicit upcast *) let c : float = 7 ;; (* any term can be annotated with a type constraint *) (4 : int) ;; (((4:int) = 1) : bool) ;; (* type constraints are useful when type inference cannot be done fully automatically. See later. *) (* 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 *) (* ill-typed *) if 3 < 4 then "yes" else 4 ;; 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") ;; r1 ;; let r2 = (if 3 < 4 then "yes" else "no") ^ ", sir" ;; r2 ;; (* tuple types *) (1,3) ;; (3,4,5) ;; (3,"pp",4.3) ;; (* note the different types of these tuples *) let t1 = (1,2,3) ;; let t2 = ((1,2),3) ;; let t3 = (1,(2,3)) ;; (* ill-typed comparisons, = requires its arguments to have the same type *) t1 = t2 ;; t2 = t3 ;; (* the empty tuple is a value of type unit *) () ;; (* // () is the *only* value of unit // it is used for instance for functions // that are called only for their side effect *) let u = Printf.printf "AI\n" ;; (* expressions of type unit can be combined with ; *) let u = () ; () ;; (* the results is also of type unit *) (* they allow for sequential evaluation of expressions with side effect *) let u = Printf.printf "\nHumpty"; Printf.printf " "; Printf.printf "Dumpty\n" ;;