Exercise : Basic types
For these exercises, the integers are expected to be ≥ 0. Do not consider negative cases.
Write 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 Write 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) Write a function e1q2 with type int → string → string such that
Note: to get the last digit of a number, usee1q2 0 ":"
"" e1q2 7 ":"
"7:" e1q2 72 ":"
"2:7:" e1q2 987 ":"
"7:8:9:" e1q2 n s
the (reversed) digits of n, interleaved with s n mod 10
.
Remember alsostring_of_int
.Write a function e1q3 similar to e1q2, but the digits are in the right order (not reversed):
e1q3 987 "#"
returns"9#8#7#"
Write 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 Write 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