User Tools

Site Tools


ex-basics-sol

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ex-basics-sol [2015/10/08 15:20] (current)
Line 1: Line 1:
 +====== LiP assignment #1: solutions ======
  
 +===== Exercise 1 =====
 +
 +''​f1:​ int -> string''​ such that ''​f1 x''​ evaluates to the string "​even"​ if ''​x''​ is even, "​odd"​ if ''​x''​ is odd
 +
 +<code ocaml>
 +let f1 x = 
 +  if x mod 2 = 0 
 +  then "​even" ​
 +  else "​odd"​
 +;;
 +</​code>​
 +
 +\\
 +
 +===== Exercise 2 =====
 +
 +''​f2:​ int -> int -> int -> bool''​ such that ''​f2 x a b''​ is undefined if x<0, and if x≥0 then ''​f2 x a b''​ evaluates to true if x∈[a,b], false otherwise
 +
 +<code ocaml>
 +let f2 x a b = 
 +  if x<0 then failwith "​Negative number"​
 +  else x>=a && x<=b
 +;;
 +</​code>​
 +
 +\\
 +
 +===== Exercise 3 =====
 +
 +''​f3:​ int -> int -> string''​ such that ''​f3 x y''​ evaluates to the string "​+"​ if the product between ''​x''​ and ''​y''​ is positive, "​-"​ if it is negative, "​0"​ if the product is zero.
 +
 +<code ocaml>
 +let f3 x y = 
 +  if (x>0 && y>0) || (x<0 && y<0) then "​+"​
 +  else if (x=0 || y=0) then "​0"​
 +  else "​-"​
 +;;
 +</​code>​
 +
 +\\
 +
 +===== Exercise 4 =====
 +
 +''​f4::​ int -> int -> int * int * int'',​ such that ''​f4 x y''​ evaluates to a triple ''​(a,​b,​c)''​ where 
 +''​a''​ is the minimum between ''​x''​ and ''​y'', ​
 +''​b''​ is the average between ''​x''​ and ''​y'',​ and
 +''​c''​ is the maximum between ''​x''​ and ''​y''​.
 +
 +<code ocaml>
 +let f4 x y =
 +      (if x<y then x else y),
 +      (x+y)/2,
 +      (if x>y then x else y)
 +;;
 +</​code>​
 +
 +\\
 +
 +===== Exercise 5 =====
 +
 +''​f5:​ int -> bool''​ such that ''​f5 x''​ fails if x≥10000, otherwise it evaluates to true if and only if all the digits in odd positions are odd, and all those in even positions are even. The position of rightmost digit is 0. For instance, ​
 +
 +<code ocaml>
 +let ( => ) a b = not a or b;;
 +
 +let f5 x =
 +  if x>=10000 then failwith "Too large"
 +  else 
 +    x mod 2 = 0 && ​
 +    x>=10 => ((x / 10) mod 2 = 1) && ​
 +    x>=100 => ((x / 100) mod 2 = 0)  && ​
 +    x>=1000 => ((x / 1000) mod 2 = 1)
 +;;
 +</​code>​
 +
 +\\
 +
 +===== Exercise 6 =====
 +
 +''​f6:​ bool -> bool -> bool -> int''​ such that ''​f6 e1 e2 e3''​ evaluates to n if exactly n of the boolean expressions ''​e1'',​ ''​e2'',​ ''​e3''​ are true.
 +
 +<code ocaml>
 +let f6 e1 e2 e3 = 
 +    (if e1 then 1 else 0) +
 +    (if e2 then 1 else 0) +
 +    (if e3 then 1 else 0)
 +;;
 +</​code>​
 +
 +\\
 +
 +===== Exercise 7 =====
 +
 +''​f7:​ string -> bool''​ such that ''​f7 s''​ evaluates to true if and only if the number, the binary representation of which is ''​s'',​ is a power of 2. If the string ''​s''​ has more than 8 digits, then fails.
 +
 +<code ocaml>
 +let f7 s = 
 +  let f i s = 
 +    if i<0 then failwith "​negative index" ​
 +    else if (i >= (String.length s)) then 0
 +    else if s.[i] = '​0'​ then 0 
 +    else if s.[i] = '​1'​ then 1 
 +    else failwith "not a binary digit"
 +  in
 +  if (String.length s) > 8 then failwith "too long"
 +  else (f 0 s) + (f 1 s) + (f 2 s) + (f 3 s) + (f 4 s) + (f 5 s) + (f 6 s) + (f 7 s) = 1
 +;;
 +</​code>​
 +
 +\\
ex-basics-sol.txt · Last modified: 2015/10/08 15:20 (external edit)