## CSE 130 Sample Final Answers

- Ocaml Types
- ans:int = 804
- type error
- ans:int = 100
- ans:int = 20

- Function Type / Write reverse
- 'a -> 'a * 'a -> bool * 'a -> 'a
- let reverse l =
- let f (c,b) = match c with [] -> ([], b) | h::t -> (t, h::b) in
- let g (l, acc) = match l with [] -> false | h::t -> true in
- let base = (l, []) in
- Semantic Equivalence
- Semantically Equivalent because addition is transitive
- Not Semantically Equivalent because g is not defined in e2
- Semantically Equivalent because multiplication is transitive
- OCaml Module
- Part A
- Signature B
- Stack.top [];;
- Because signature A says that 'a stack = 'a list, you can call any function on 'a stack on an 'a list instead. This allows Stack.top to be called on [], which will throw the EmptyStack exception. When using signature B, you must call Stack.make, and it will never give you back an empty list.
- Part B
- Signature A
- Inferred Type: 'a stk -> 'a list
- Part C
- Doesn't work
- Boolean Formulas
- type boolexp = Var of int | Not of boolexp | Or of boolexp * boolexp | And of boolexp * boolexp

And(Or(Var(0), Not(Var(1))), Or(Var(1), Not(Var(2)))) - let rec eval(list, expr) =

let rec find (list, index) =

if(index = 0) then List.hd(list)

else find(List.tl list, index - 1) in

match expr with

Var(i) -> find(list, i)

|Not(v) -> !eval(list, v)

|And(a1,a2) -> eval(list, a1) && eval(list, a2)

|Or(a1,a2) -> eval(list,a1) || eval(list,a2)

## Comments

## chinese-tea-supplier said:

thanks for your share.