Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Haskell/Experimental Modules/Cheat sheet prototype 1

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Cheat sheet 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 true tail [a] -> [a] All elements after the first one. tail [1,2,3] = [2,3] true 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 true (!!) [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] true filter (a -> Bool) -> [a] -> [a] Return only those elements that satisfy the predicate. filter odd [1..7] = [1,3,5,7] true (++) [a] -> [a] -> [a] Append two lists. [1,2] ++ [3,4] = [1,2,3,4] true [] [a] The empty list. [] true (:) a -> [a] -> [a] Prepend one element. [1,2,3] = 1:(2:(3:[])) true Folds and scans - advanced foldl (a -> b -> a) -> a -> [b] -> a Left-to-right fold true 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 true 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 true 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 true scanr1 (a -> a -> a) -> [a] -> [a] List of partial right-to-left folds, initialized with first element General notes 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 functions More detailed notes. The auto-generated hyperlinks on the table point to the lv. 3 headers here. head­ 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. tail 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. deep