



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




More OCaml
Due: Monday, September 18, 2006, by noon
Instructions: Use the same submission procedure as last time, paying attention to the following points:
~cis500/bin/cis500submit hw2 hw2.ml
Reading assignment: Before beginning the programming exercises below, read Chapter 6 of Jason Hickeyโs Introduction to Objective Caml.
1 Exercise Consider the following datatype of tokens:
type token = Num of int | Plus | Minus | Times | LParen | RParen | If | Then | Else | And | Or | Equal
Write a function lex that takes a list of characters as input and produces a list of tokens as output. Your function should:
Examples:
Exception: Bad.
2 Exercise Here is a very simple grammar of fully parenthesized arithmetic and boolean expressions (extending the one we saw in class),
exp ::= n number (exp + exp) parenthesized sum of expressions (exp โ exp) parenthesized difference of expressions (exp โ exp) parenthesized product of expressions (exp = exp) parenthesized comparison of expressions (exp&&exp) parenthesized conjunction of expressions (exp||exp) parenthesized disjunction of expressions if exp then exp else exp conditional
and here is a datatype definition representing the corresponding set of abstract syntax trees.
type ast = ANum of int
| _ -> raise Bad in match rest2 with RParen::rest3 -> (e, rest3) | _ -> raise Bad) | If::rest -> (let (e1,rest1) = parse rest in let rest2 = parseToken Then rest1 in let (e2,rest3) = parse rest2 in let rest4 = parseToken Else rest3 in let (e3,rest5) = parse rest4 in (AIf(e1,e2,e3), rest5)) | _ -> raise Bad
And here is a function explode that takes a string and returns a list of the characters it contains:
let rec explode s = match s with "" -> [] | _ -> (String.get s 0):: (explode (String.sub s 1 ((String.length s)-1)))
Put all of these pieces together: take the eval function from the previous exercise, the parse and explode functions above, and your lex function from the first exercise, and write a function calc that takes a string and returns an integer. If the string represents a valid arithmetic expression, the calc function should return its value as computed by eval. If it is not a valid expression, it should raise the exception Bad. Examples:
Exception: Bad.
4 Exercise [Required for all groups (of any size, including 1) containing at least one PhD student; optional otherwise] Extend your parser to handle unparenthesized expressions, using the usual rules of precedence (&& and || have lower precedence than =, which has lower precedence than + and - , which have lower precedence than *) and associativity (1+2+3 means (1+2)+3).
5 Exercise
The exists function takes a predicate p (a one-argument function returning a boolean) and a list l and checks whether there is some element of l for which p returns true.
Define exists as a recursive function.
6 Exercise Give an alternate definition of exists using fold and without any other use of recursion.
7 Exercise Define map using fold (and without any other use of recursion).
8 Exercise Instead of defining the stream data type as we did in the lecture,
type โa stream = Stream of โa * (unit -> โa stream);;
suppose we defined it like this:
type โa stream = Stream of (unit -> (โa * โa stream));;
Under this new definition, a stream does not โpre-computeโ its first element. Instead, it waits until asked to compute anything at all; only then does it compute and return a head element and a new stream. Rewrite all of the definitions of stream processing functions and examples (including the sieve of prime numbers) from the lecture notes so that they work with this new version of streams.