Docsity

Assignment 2 Solved Questions - Software Foundations | CIS 500, Assignments of Computer Science

Toggle sidebar
bg1
CIS 500 โ€” Software Foundations
Homework Assignment 2
More OCaml
Due: Monday, September 18, 2006, by noon
Instructions: Use the same submission procedure as last time, paying attention to the following
points:
โ€ขPut all of your solutions together in a single OCaml source file named hw2.ml.
โ€ขSubmit this file as hw2, for example, using the command:
~cis500/bin/cis500submit hw2 hw2.ml
โ€ขAnything that isnโ€™t valid OCaml code should be placed in a comment. We want to be able
to run your file directly.
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:
โ€ขmap sequences of digits to appropriate instances of the Num constructor
pf3
pf4
pf5

Partial preview of the text

Download Assignment 2 Solved Questions - Software Foundations | CIS 500 and more Assignments Computer Science in PDF only on Docsity!

CIS 500 โ€” Software Foundations

Homework Assignment 2

More OCaml

Due: Monday, September 18, 2006, by noon

Instructions: Use the same submission procedure as last time, paying attention to the following points:

  • Put all of your solutions together in a single OCaml source file named hw2.ml.
  • Submit this file as hw2, for example, using the command:

~cis500/bin/cis500submit hw2 hw2.ml

  • Anything that isnโ€™t valid OCaml code should be placed in a comment. We want to be able to run your file directly.

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:

  • map sequences of digits to appropriate instances of the Num constructor
  • map the characters โ€™+โ€™, โ€™-โ€™, โ€™*โ€™, โ€™=โ€™, โ€™(โ€™, and โ€™)โ€™ to Plus, Minus, Times, Equal, LParen, and RParen, respectively
  • map the two-character sequence โ€™&โ€™;โ€™&โ€™ to the token And, the sequence โ€™|โ€™;โ€™|โ€™ to the token Or, the sequence โ€™iโ€™;โ€™fโ€™ to the token If, the sequence โ€™tโ€™;โ€™hโ€™;โ€™eโ€™;โ€™nโ€™ to the token Then, and the sequence โ€™eโ€™;โ€™lโ€™;โ€™sโ€™;โ€™eโ€™ to the token Else.
  • ignore whitespace (the โ€™ โ€™ and โ€™\nโ€™ characters)
  • fail (by raising the exception Bad) on all other characters

Examples:

lex [โ€™(โ€™;โ€™1โ€™;โ€™2โ€™;โ€™+โ€™;โ€™3โ€™;โ€™4โ€™;โ€™0โ€™;โ€™)โ€™;โ€™ โ€™];;

  • : token list = [LParen; Num 12; Plus; Num 340; RParen]

lex [โ€™+โ€™;โ€™ โ€™;โ€™*โ€™];;

  • : token list = [Plus; Times]

lex [โ€™ifโ€™;โ€™ โ€™;โ€™5โ€™;โ€™tโ€™;โ€™hโ€™;โ€™eโ€™;โ€™nโ€™];;

  • : token list = [If; Num 5; Then]

lex [โ€™aโ€™];;

Exception: Bad.

lex [];;

  • : token list = []

lex [โ€™(โ€™;โ€™(โ€™;โ€™1โ€™;โ€™2โ€™;โ€™+โ€™;โ€™3โ€™;โ€™4โ€™;โ€™0โ€™;โ€™)โ€™;โ€™*โ€™;โ€™ โ€™;โ€™ โ€™;โ€™\nโ€™;โ€™5โ€™;โ€™)โ€™];;

  • : token list = [LParen; LParen; Num 12; Plus; Num 340; RParen; Times; Num 5; RParen]

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:

calc "((1+2)*3)";;

  • : int = Int 9

calc "(1+2) 5";;

Exception: Bad.

calc "((2+1) * (11+8))";;

  • : int = Int 57

calc "(3=(1+2))";;

  • : value = Bool true

calc "if (3=4) then (3+6) else (5*200)";;

  • : value = Int 1000

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.

exists (fun x -> x >= 3) [2;11;4];;

  • : bool = true

exists (fun x -> x >= 3) [1;1;2];;

  • : bool = false

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.