CS 430: Lecture 7 - Expressions
Dear students:
A new language is upon us. New languages require us to regress to infancy. The world around is filled with expressions that we can't understand, and we are uncomfortable. Today we embrace that discomfort and start babbling in Haskell by solving some Haskell programming problems in small groups.
Inventory
To solve these problems, we'll to make use of some of Haskell's builtin functions. These are the ones from the reading plus a few more that you might find useful:
-
head items
: get first element of a list -
tail items
: get all elements but the head of a list -
take n items
: get firstn
elements of a list -
drop n items
: get list without firstn
elements -
fst pair
: get first component of a 2-tuple -
snd pair
: get second component of a 2-tuple -
item : items
: prepend a new head onto a list -
these ++ those
: concatenate two lists -
div a b
: integer division -
mod a b
: integer modulus -
a ^ b
: positive integer exponentiation -
a ** b
: floating point exponentiation -
ord c
: convert character to ASCII -
chr c
: convert ASCII to character
PairSum
Write a function pairSum
that, given a pair of numbers, returns their sum. For example:
-
pairSum (5, 6)
→11
-
pairSum (-3, -6)
→-9
-
pairSum (0, 0)
→0
Majority
Write a function majority
that, given a population as an Int
, returns the number of people that are needed to count as a majority in that people. For example:
-
majority 100
→51
-
majority 99
→50
-
majority 98
→50
-
majority 1
→1
-
majority 2
→2
Neck
Write a function neck
that, given a list, returns its second element. For example:
-
neck [5, 3, 1]
→3
-
neck "guarantee"
→'u'
-
neck [10..20]
→11
-
neck [0,5..100]
→5
Slice
Write a function slice
that, given a list, a starting index, and an ending index, returns the sublist between the given indices. For example:
-
slice 2 6 "romania"
→"mania"
-
slice 0 2 [9, 3, 2, 11, 8]
→[9, 3, 2]
Frankenstring
Write a function frankenstring
that, given two strings, returns a string that is the concatenation of the first half of the first string and the second half of the second string. For example:
-
frankenstring "abcd" "1234"
→"ab34"
-
frankenstring "im" "ur"
→"ir"
Transfer
Write a function transfer
that, given a pair of lists, returns a new pair of lists in which the first element of the first list has been prepended on the second list. For example:
-
transfer ("bless", "other")
→("less", "bother")
-
transfer ([1..5], [3, 7])
→([2, 3, 4, 5], [1, 3, 7])
-
transfer ([2], [])
→([], [2])
Next
Write a function next
that accepts two numbers and in an arithmetic sequence and returns the next number. For example:
-
next 5 8
→11
-
next 2 2
→2
-
next 10 0
→-10
-
next 5.3 5.4
→5.5
Quadrant
Write a function quadrant
that accepts a Cartesian coordinate pair of numbers. It returns "I"
, "II"
, "III"
, or "IV"
to indicate the pairs quadrant. If the pair doesn't lie in a quadrant, it returns "?"
. For example:
-
quadrant (1, 2)
→"I"
-
quadrant (0, 0)
→"?"
-
quadrant (3, -10)
→"IV"
Shy
Write a function shy
that accepts an Int
number of photos that you wish to arrange in a display. It returns the number of photos you need to achieve a fully-packed square. For example:
-
shy 61
→3
-
shy 10
→6
-
shy 128
→0
Mxb
Write a function mxb
that accepts a slope and a y-intercept. It returns the slope-intercept form of the designated line as a string. For example:
-
mxb 3 (-2)
→"3 * x + -2"
-
mxb 0 0
→"0 * x + 0"
-
mxb 1 3000
→"1 * x + 3000"
RotateLetter
Write a function rotateLetter
that accepts a Char
uppercase letter and an Int
offset and returns the letter with the given offset from the given letter. The offset wraps back to A as necessary. For example:
-
rotateLetter 'A' 5
→'E'
-
rotateLetter 'M' 3
→'P'
-
rotateLetter 'Z' 4
→'D'
TODO
Here's your list of things to do before we meet next:
See you next time.
Sincerely,