User Tools

Site Tools


LiP assignment #1

Write the following functions:

  • f1: int → string such that f1 x evaluates to the string “even” if x is even, “odd” if x is odd
  • 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
  • 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 (Warning: you cannot use the multiplication operator *)
  • 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.
  • 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, f5 34 evaluates to true, while f5 8234 evaluates to false.
  • 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. For instance, f6 true (2>3) (2+2=4) evaluates to 2.
  • 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. For instance, f7 “0100” evaluates to true, f7 “1010” evaluates to false, f7 “101110010” fails. Hint. A binary number is a power of 2 if and only if its binary representation has exactly one non-zero bit. You can use String.length s to compute the length of s, and s.[i] to access to the i-th element of s.
ex-basics.txt · Last modified: 2015/10/08 15:20 (external edit)