Exercise : Basic types

For these exercises, the integers are expected to be ≥ 0. Do not consider negative cases.

  • StarWrite a function e1q1 with type int → string → string such that
    e1q1 0 "ab" ""
    e1q1 1 "ab" "ab"
    e1q1 2 "ab" "abab"
    e1q1 n s s repeated n times
  • StarWrite a function e1q4 with type int → int such that
    e1q4 0 0
    e1q4 1 1
    e1q4 10 2
    e1q4 99 2
    e1q4 999 3
    e1q4 n the number of digits of n (except for 0, where it returns 0)
  • StarWrite a function e1q2 with type int → string → string such that
    e1q2 0 ":" ""
    e1q2 7 ":" "7:"
    e1q2 72 ":" "2:7:"
    e1q2 987 ":" "7:8:9:"
    e1q2 n s the (reversed) digits of n, interleaved with s
    Note: to get the last digit of a number, use n mod 10.
    Remember also string_of_int.
  • StarWrite a function e1q3 similar to e1q2, but the digits are in the right order (not reversed):
    e1q3 987 "#" returns "9#8#7#"
  • StarWrite a function e1q5 with type int → bool → bool such that
    e1q5 0 b true (whatever b is)
    e1q5 n true returns true iff all digits of n are even
    e1q5 n false returns true iff all digits of n are odd
    e1q5 17359 false true
    e1q5 17369 false false
    e1q5 288062 true true
    e1q5 298462 true false
  • StarWrite a function e1q6 with type int → int → bool such that
    e1q6 n m true iff all digits of n are smaller or equal
    than the corresponding digits of m (prefixing with zeroes if necessary).
    e1q6 0 0 true
    e1q6 1234 2345 true
    e1q6 1234 999 false (because 999 is actually 0999)
    e1q6 999 1648 false
    e1q6 333 1444 true
Exercise : Curried functions
  • StarWrite a higher-order, polymorphic function curry3 with type (α × β × γ → δ) → α → β → γ → δ which maps a 3-argument uncurried function to a curried function.
    Try it with let ccat (a,b,c) = a ^ b ^ c
  • Write the converse function: uncurry3 with type (α → β → γ → δ) → α × β × γ → δ which maps a 3-argument curried function to an uncurried function.
  • Write a function apply3 with type (α → β) × (γ → δ) × (ε → ζ) → α × γ × ε → β × δ × ζ which takes two arguments: a triple of functions and a triple of values. It returns a triple of results (the first function applied to the first value, the second function applied to the second value, ...).
  • Write a function sort2 with type (α → β) → (α → β) → α → (α → β) × (α → β) which takes three arguments: two functions, f and g, and an argument x. It returns the pair f,g if f(x) is smaller than g(x), otherwise it returns the pair g,f
  • Write a function comp3 with type (α → β) → (γ → α) → (δ → γ) → δ → β which takes three arguments: three functions f, g, and h, and returns their composition: f∘g∘h
Exercise : Pattern-matching
  • Write a function ext_and (extended and) with type bool → bool → bool → bool such that ext_and a b c returns a AND b when c is true, and returns not (a AND b) when c is false. Do not use any logical operator (such as &&, ||, not). Do not use IF.
    ext_and false true true false
    ext_and false true false true
    ext_and true true false false
    ext_and true true true true
  • Write a function encode_char with type char → char which (bijectively) transforms each vowel to another vowel of your choice, and keeps consonant as is. Use it to define encode with type string → string which transforms a string to another string by transforming each character. You need String.map
    encode "I am a superhero." Y em e sopirhira.
    encode "Les bons etudiants" Lis bans itodyents