Skip to content

Notes for Week 2 Getting Started

ssanj edited this page Dec 1, 2011 · 28 revisions

Links

Exercises:

  1. Rewrite the following with the use of the notElem (not an element of) function:

    >> [x | x <- [10..20], x /= 13, x /= 15, x /=19]

  2. Write the replicate function, in terms of repeat and take. Eg:

    >> replicate 3 10 == ??

  3. Write the sum of the numbers in each sublist of xxs without flattening it:

    >> let xxs = [ [1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6] ]

    Eg: [ [x], [y], [z] ], where x,y,z are totals.

  4. What is the type of the tuple in the following expression:

    >> ([1,2,3], "hello", ['a','b'], (1, 'c', True))

    Eg. (True, 'a') is a (Bool, Char)

  5. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

  6. What do the following return?

    1. [2,2..10]
    2. [2.1..10]
    3. [1.0..10.0]
    4. [1.0, 1.1..10.0]
    5. [15,13..13]

Questions raised during the group:

  1. Xor function I found this nice blog post on multiple implementations of the xor function.

  2. Nested if-then-else statements:

    if (a && b) then not a else if (a || b) then True else False

  3. Max size of tuples:

    There is no upper bound on the size of a tuple, but some Haskell implementations may restrict the size of tuples, and limit the instances associated with larger tuples. However, every Haskell implementation must support tuples up to size 15, together with the instances for Eq, Ord, Bounded, Read, and Show. The Prelude and libraries define tuple functions such as zip for tuples up to a size of 7.

    As of GHC 6.12.2, the maximum size of a tuple is 62:

    source : SO

  4. How to catch errors:

    Control.Exception.catch (head []) (\m -> putStrLn $ "caught can error: " ++ show (m::Control.Exception.SomeException))

  5. What is the difference between calling a function and the $ operator?

    ($) :: (a -> b) -> a -> bSource

    Application operator. This operator is redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted; for example:

    f $ g $ h x = f (g (h x))

    It is also useful in higher-order situations, such as map ($ 0) xs, or Data.List.zipWith ($) fs xs.

    source: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html

  6. Laziness: Why does this run out of memory: head (reverse [1..100000000])
    But this come back straight away: last [1..100000000]

    Haskell builds up a full array for the reverse function. This means that if you request an array of a 100 million elements, it needs to create an array of a 100 million elements in reverse. This is why you run out of memory.

    With the last function, Haskell still needs to go through an array of a 100 million elements, but it does so 1 element at a time using tail recursion - which basically means the stack size is always 1.

  7. How do you prevent data structures being passed into functions that will throw an exception?

  8. Can you have properly abstracted code with meaningful variable names? https://groups.google.com/forum/#!topic/scala-debate/ho8FIT5afKA/discussion

Answers to Exercises:

  1. [x | x <- [10..20], x notElem [13,15,19]]

  2. replicate 3 10 == take 3 (repeat 10)

  3. [[sum xs] | xs <- xxs]

  4. ([Num], [Char], [Char], (Num, Char, Bool))

  5. sum [3,6..999] + sum [5,10..999] - sum [15,30..999]

  6. Left to the reader to find out.

Clone this wiki locally