Please indicate if the statement is either True or False.
(1) If an expression (f e) is well typed, then f has some type of the form A -> B.
[TRUE]
(2) unit is a value in OCaml. [FALSE]
(3) An unambiguous grammar permits to have multiple parse trees for the same sentence. [FALSE]
(4) Every grammar with multiple rules for the same nonterminal is ambiguous. [FALSE]
(6) If (f 3) always evaluates to 1 and (f 1) always evaluates to 3, then f (f (f 3)) evaluates to 1.
[TRUE]
(7) In an OCaml data type T, the constructors can refer to the data type T itself. [TRUE]
(8) Curried functions are more easily composable. [TRUE]
P2-1 What is the type of the expression printer?
type printable =
Bofbool |Iofint |Cofchar|
S of string | L of int list | P of printable * printable
let rec printer f p =
match p with
| L l -> L (map f l)
| P(p1,p2) -> P(printer f p1, printer f p2)
| _-> p
A: int -> int -> printable -> int list
B: (int -> int) -> printable -> printable
C: int list -> int list -> printable -> int list
D: int -> int -> printable -> printable
E: (int list -> int list) -> printable -> int list
F: int list -> int list -> printable -> printable
P2-2 Given the type printable defined in P2-1, what is the type of the expression printer2?
let rec printer2 mix p1 p2 p3 =
match mix with
| [] -> L p1
| x::xs ->
match x with
|B b -> printer2 xs p1 (b::p2) p3
|I i -> printer2 xs (i::p1) p2 p3
|C c -> printer2 xs p1 p2 (c::p3)
|_ -> printer2 xs p1 p2 p3
A: printable list -> bool -> int -> char -> int list
B: printable list -> int -> bool -> char -> printable
C: printable list -> bool list -> int list -> char list -> int list D: printable list -> int list -> bool list -> char list -> printable E: printable list -> int list -> bool list -> char list -> int list F: printable list -> bool -> int -> char -> printable
P2-13 Consider the following expression in OCaml:
let f = fun x-> if x<=2 then 0 else x in
let y= [1;2;3] in map f y
What value this expression will evaluate to?
A: [0;0;0]
B: [0;0;3]
C: [1;2;3]
D: [0;2;3]
E: [3;0;0]
14) What is printed to the screen when the following Ocaml code is executed?
let s = 4 in
let f = let s = 3
in (print_endline (string_of_int s)) in let s = 5
in let _ = print_endline (string_of_int s) in f
A.3 5
B.4 5
C.3 4
D.4 3
E. This code contains a type error
(17) Consider the following functions in OCaml:
let foo(x) = (let _ = print_endline("foo")in x) let bar(x) = (let _ = print_endline("bar")in x) let baz(x) = (let _ = print_endline("baz")in x)
Which of the following statements will print the same string to the screen:
1: foobazbar1
2: foo (bar (baz 1)) 3: foo bar (baz(1))
A. B. C. D. E.
None, they all print something different
2 and 3 will print the same thing 1 and 3 will print the same thing
1 and 2 will print the same thing
1, 2, and 3 will print the same thing
Consider the following grammar:
|
|a|b|c
Is the grammar ambiguous? Please explain:
yes
a+a+a
has the parse trees:
++ /\ /\ a+ +a /\ /\ aaaa
Write a new grammar such that:
– it accepts exactly the same strings as the grammar above
– the grammar is not ambiguous.
– the operation + has higher precedence then -,
– the operation + is left recursive and – is right recursive.