Commit def09d66 authored by James R. Wilcox's avatar James R. Wilcox
Browse files

lec16live.ml

parent 0de2eedf
(* unit: built-in type has exactly one value, written "()" *)
let f () =
print_endline "hello"
(* print_endline : string -> unit *)
let x = f ()
let g () =
f ();
print_endline "world"
(* semicolon is a separator, not a terminator *)
(*
e1; e2
evaluate e1 to value v1
then throw v1 away
evaluate e2 to value v2 and return that
*)
let x =
17;
"hello"
let y =
ignore(17);
"hello"
let f2() =
print_endline "hello";
17
let x =
f2();
"hello"
let y =
ignore(f2());
"hello"
let y =
let _ = f2() in
"hello"
(* if expression without else *)
let x = 17
(* divides by but only if it's even *)
let h x =
if x mod 2 = 1
then failwith "oops";
(* above is shorthand for below *)
if x mod 2 = 1
then failwith "oops" else ();
x / 2
(* equality *)
(*
- two different equality operators in ocaml
- == is the same as java (reference equality)
- != is "not reference equal"
- = "structural equality". sort of like .equals() in java, but not user defined -- built-in
- <> is "not structurally equal"
let x = [1;2; 3]
let y = 1 :: 2 :: 3 :: []
x = y
x <> y
- in OCaml (up until function values) all values are just trees of data
- structural equality "does what you expect"
- you will implement this in trefoil!
- long description in LANGUAGE.md
*)
(* from last time *)
let rec n_times (f, n, x) =
if n = 0
then x
else f (n_times (f, n-1, x))
let rec filter (f, xs) =
match xs with
| [] -> []
| x :: xs ->
if f(x)
then x :: filter (f, xs)
else filter (f, xs)
let is_even x = x mod 2 = 0
let is_odd x = x mod 2 = 1
(* anonymous functions *)
let _ =
filter ((fun x -> x mod 2 = 0), [1; 2; 3])
(* unnecessary function wrapping *)
let tail_n_times_better (n, l) = n_times ((fun l -> List.tl l), n, l)
let tail_n_times_better (n, l) = n_times (List.tl, n, l)
(* returning a function as a result *)
let mult_by_n n =
fun x -> n * x
(* val mult_by_n : int -> (int -> int) = <fun> *)
(* type of a function that takes an int and returns a function that takes an int and returns an int *)
let foo: int -> (int -> int) = mult_by_n
(* type of a function that takes a function (int -> int) as an argument and returns an int *)
(*
let bar: (int -> int) -> int = mult_by_n
*)
let f = mult_by_n 3
let x = f 10
let y = f 5
let multiply (n, x) =
n * x
(* val multiply : int * int -> int = <fun> *)
let double_or_triple (b: bool): int -> int =
if b
then fun x -> 2 * x
else fun x -> 3 * x
let weird_double = double_or_triple true
let weird_triple = double_or_triple false
(* more higher order functions *)
type exp =
| Const of int
| Negate of exp
| Add of exp * exp
| Multiply of exp * exp
(* example of a higher order function on ASTs *)
(* returns true if f is true of every constant in e *)
let rec true_of_all_constants (f, e) =
match e with
| Const n -> f n
| Negate e' -> true_of_all_constants (f, e')
| Add (e1, e2) -> true_of_all_constants (f, e1) && true_of_all_constants (f, e2)
| Multiply (e1, e2) -> true_of_all_constants (f, e1) && true_of_all_constants (f, e2)
(* val true_of_all_constants : (int -> bool) * exp -> bool = <fun> *)
let _ = true_of_all_constants (is_even, Add(Const 1, Const 2))
let _ = true_of_all_constants (is_even, Add(Const 4, Const 2))
let _ = true_of_all_constants ((fun x -> x mod 2 = 0), Add(Const 4, Const 2))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment