Last modified on 16 May 2010, at 15:39

Haskell/Experimental Modules/Cheat sheet prototype 1

Cheat sheetEdit

The table is built with templates to make things less tedious. Bold face usage on the function names is a naïve first approach on how to call the attention of newbies for the bread-and-butter functions (when contrasted to the advanced stuff). To trigger bold face, set the "key" parameter of the template to some non-null value.

Naming the last column "Evaluates to" is quite clever because the entries can be really short that way. Doesn't always work, but I like it. -- apfeλmus 19:15, 15 May 2010 (UTC)

We can steal the categorization and such from the Prelude documentation.

List Operations
Name Type signature Evaluates to Example
Basic functions - basic
head­ :: [a] -> a First element of a list. head [1,2,3] = 1
tail :: [a] -> [a] All elements after the first one. tail [1,2,3] = [2,3]
last :: [a] -> a Last element. last [1,2,3] = 3
init :: [a] -> [a] All elements before the last one. init [1,2,3] = [1,2]
null :: [a] -> Bool Whether the list is empty. null ['a','x'] = False

null [] = True

length :: [a] -> Int Number of elements. length [True,False] = 2
(!!) :: [a] -> Int -> a Element at a given position, starting at 0 [4,5,6] !! 1 = 5.
reverse :: [a] -> [a] Elements in reverse order. reverse [1,2,3] = [3,2,1]
map :: (a -> b) -> [a] -> [b] Apply a function to each element. map f [1,2,3] = [f 1, f 2, f 3]
filter :: (a -> Bool) -> [a] -> [a] Return only those elements that satisfy the predicate. filter odd [1..7] = [1,3,5,7]
(++) :: [a] -> [a] -> [a] Append two lists. [1,2] ++ [3,4] = [1,2,3,4]
[] :: [a] The empty list. []
(:) :: a -> [a] -> [a] Prepend one element. [1,2,3] = 1:(2:(3:[]))
Folds and scans - advanced
foldl :: (a -> b -> a) -> a -> [b] -> a Left-to-right fold
foldl1 :: (a -> a -> a) -> [a] -> a Left-to-right fold, initialized with first element
scanl :: (a -> b -> a) -> a -> [b] -> [a] List of partial left-to-right folds
scanl1 :: (a -> a -> a) -> [a] -> [a] List of partial left-to-right folds, initialized with first element
foldr :: (a -> b -> b) -> b -> [a] -> b Right-to-left fold
foldr1 :: (a -> a -> a) -> [a] -> a Right-to-left fold, initialized with first element
scanr :: (a -> b -> b) -> b -> [a] -> [b] List of partial right-to-left folds
scanr1 :: (a -> a -> a) -> [a] -> [a] List of partial right-to-left folds, initialized with first element

General notesEdit

Here we might put some general discussion on the functions. In my opinion, however, general comments would fit better in the actual "Building a vocabulary" module, while the cheat sheet pages would stay as a separate appendices linked from the main body of the book.

Notes on the functionsEdit

More detailed notes. The auto-generated hyperlinks on the table point to the lv. 3 headers here.

head­Edit

I don't know if it is a good idea to repeat information from the table on the comments section. If we choose to do so, templates could be used to help with the formatting as well.

Type signature:

[a] -> a

Description:

Evaluates to the first element of a given list.

Additional remarks:

Triggers an error if applied on an empty list.

tailEdit

Type signature:

[a] -> [a]

Description:

Evaluates to the list obtained by dropping the first element of the argument.

Additional remarks:

Triggers an error if applied on an empty list. Evaluates to [] if the list has just a single element.