Sign in
|
Join
Search
David Stone's Blog
I'm open to suggestions for a subtitle here! (Really!)
Home
About
RSS
Atom
Comments RSS
Recent Posts
Vint Cerf and the 700MHz Spectrum
Defcon
More Compiler Construction stuff...
Some GPA bragging...
David's Compilers Survival Guide
Tags
#sdcolleges
700MHz
auction
cse
cse130
cse131a
defcon
eclipse
fcc
general software development
google
greasemonkey
javascript
jquery
LINQ
ocaml
school
toorcon
ucsd
xquery
Navigation
Home
Blogs
General
About Me
My Articles
Archives
July 2007 (1)
May 2007 (1)
February 2007 (2)
January 2007 (1)
December 2006 (2)
October 2006 (1)
September 2006 (1)
July 2006 (1)
May 2006 (4)
November 2005 (1)
October 2005 (3)
September 2005 (2)
December 2004 (1)
November 2004 (1)
February 2004 (1)
January 2004 (6)
November 2003 (3)
October 2003 (1)
September 2003 (8)
August 2003 (10)
July 2003 (6)
June 2003 (7)
May 2003 (3)
April 2003 (47)
March 2003 (45)
February 2003 (6)
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)
Posted:
Dec 05 2006, 10:33 PM
by
David Stone
| with
2 comment(s)
Filed under:
ucsd
,
ocaml
,
cse130
,
school
Comments
Feed Search Engine - All Fresh Articles And News Are Here
said:
Pingback from Feed Search Engine - All Fresh Articles And News Are Here
#
November 25, 2007 10:07 AM
nick_zelcad
said:
www.message_c4tolovig.com
#
May 14, 2009 10:01 AM
Leave a Comment
Title
(required)
Name
(required)
Your URL
(optional
)
Comments
(required)
Remember Me?