Examples illustrate Haskell type inference

Some useful modules:

Note that strings are lists of characters.

Start WinGHCi, in Prelude type the definitions. In the lab, type ghci to start the interpreter. You can load files using :load command. The file path is relative to your current directory (so if you are running the interpreter in the same directory as your file, just use the file name, no quotation marks).
Use Ctrl-D to exit.


    -- polymorphic functions
    map succ [4, 5.6, (-7)]
    map succ [4, 0, 5]
    map Data.Char.toLower "Hi There!"
    
    -- writing polymorphic functions, type inference
    let f g = [g 2, g 7.8] -- what is the type of f?
    f succ
    f truncate
    f (\x -> x < 5)
    :type f

    let g h = h (h 2)
    g succ
    g (\x -> x + 5)
    :type g

    

    :load types.hs
    -- examine the type of addLast, explain why the following calls
    -- are valid, and what all type instances are in this case
    addLast [1, 2] 6
    addLast [1, 2] 6.0
    addLast "Hi there" '!'

    
    
    count (\x -> x < 5) [2, 6, 4, 7, 8]
    -- Exercise 1
    -- examine the type of count
    -- use it on at least two examples other than integers
    -- write down what you used it on and what were specific type instances

    -- Exercise 2
    -- examine the types of firstTrue and firstTrue2 in types.hs
    -- clearly explain the difference; which one is more general?
    -- How is this generality achieved? 
    -- provide examples of use of both; use more general examples
    -- when possible

    -- Exercise 3
    -- examine the function mystery in types.hs
    -- what does it do? Describe it and provide a few examples
    -- of its usage. What are the requirements for the types of
    -- the two lists?

    -- Exercise 4
    -- Write a function that reverses a list. What is its type?
    -- Explain what the type means, provide examples of use.

    -- Exercise 5
    -- Write a function that takes a list, a two-parameter function,
    -- and a numeric threshold. The function acts like fold (for isntance,
    -- adds up all the elements of the list, but stops once the result
    -- is greater than or equal to the value of the threshold. What are
    -- the types of all function parameters? Why? 
		     

The file types.hs:


addLast [] x = [x]
addLast (y:ys) x = y:(addLast ys x)

count pred [] = 0
count pred (x:xs) = if pred x then 1 + count pred xs else count pred xs

-- the function finds the first element that
-- satisfies the predicate. If no such item found, False is returned
firstTrue pred [] = False
firstTrue pred (x:xs) = if pred x then x else firstTrue pred xs

-- the function finds the first element that
-- satisfies the predicate and returns a singleton list
-- with that element. If no such element found, an empty list
-- is returned
firstTrue2 pred [] = []
firstTrue2 pred (x:xs) = if pred x then [x] else firstTrue2 pred xs

mystery [] ys = []
mystery xs [] = []
mystery (x:xs) (y:ys) = (x y):mystery xs ys

CSci 4651 course web site.