Skip to content

Notes for Week 2 Getting Started

ssanj edited this page Nov 22, 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)

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

Clone this wiki locally