Problems 1

Basic Expressions and Functions

1. Give the type and value of each of the following expressions. For example, given the expression 2+2, the answer should be written as 4 :: Int. Note, some expressions may be errors.

• 1 + sqrt 9 * 4 + 13

• "asfd" ++ "dfsa"
• "a" : "dfsa
• [‘a’] ++ [‘d’,’f’,’s’,’a’]
• [20,30..100]
• sum [1..10]
• let x = 5 in x*(x+1)
• 1:(take 1 [1,2])
• [[1,2],[3,4]]

1. Given the following definition of the function f, simplify the expression f (f 1 2) (f 3 4), showing all your steps.
2. f :: Int -> Int -> Int

f x y = 2*x + 3

3. Write a function isLetter :: Char -> Bool that determines whether its argument is a letter of the alphabet.
4. Write a function capitalise :: String -> String that makes all the letters upper case.
5. Write a function that determines whether or not one string is the prefix of another string.
6. Write a function that occurs :: Int -> [Int] -> Int that returns the number of times a given number occurs in a list.
7. Write a function reverse that reverses a string.
8. Write a function palindrome :: String -> Bool that determines whether its argument is a palindrome. A palindrome is a string which is the same when you reverse it; for example, ‘abccba’ is a palindrome, but ‘abcca’ is not.