
Solutionstoproblemsinlecture29
OCamlprogramming
1.funfij=i+j
fung(i,j)=i+j
funh[i;j]=i+j
2.letrecsums_leftx=matchxwith
[]‐>[]
|[a]‐>[a]
|y::ys‐>letz::zs=sums_leftys
iny+z::z::zs;;
letsums_rightx=
letrecsums'nx=matchxwith
[]‐>[]
|y::ys‐>(y+n)::sums'(y+n)ys
insums'0x;;
3.(a)[5;6;7;8]
(b)44
Abstractsyntax
4.letrecppns=matchswith
Assign(s,e)‐>s^"="^(pp_expre)^";"
|If(e,s,s')‐>"if("^(pp_expre)^")\n"^
(indentn)^"then"^(pp(n+3)s)^
(matchs'with
Block[]‐>""
|_‐>"\n"^(indentn)^"else"^(pp(n+3)s'))^"\n"
|While(e,s)‐>"while("^(pp_expre)^")\n"^
(indent(n+3))^(pp(n+3)s)^"\n"
|Blocksl‐>"{\n"^
(fold_right(^)
(map(funs‐>(indent(n+3))^pp(n+3)s^"\n")sl)
"")^
(indentn)^"}\n";;
(Note:Wewillnotaskaquestionthisdifficultonthefinal.)