Haskell Cheat Sheet by tamir13


									Haskell Cheat Sheet                                                                                                                    maneu.fr | c-maneu.developpez.com

BASICS                                                        functionName::inputType1[->inputTypeN]-               Using lists
                                                              >outputType                                            An empty list is designed by []
Comments                                                      Declare function with pattern matching (sample)        In (h:q), h stands for the first element of the list, and q
All comments start with two hyphens                           intToChar 1 = "One"
-- This is a comment in the code                                                                                     for the rest. With (f:s:t:q), you can directly gets
                                                              intToChar 2 = "Two"
                                                                                                                     the first (f), second (s) and third (t) element of the
Data Types                                                     Declare function with guards

Haskell uses various data types, all of them starts by a      intToChar x
                                                                                                                     You can add the element e to the list l with e:l
capital letter:                                                       | x==1 = "One"
      Int: Integer number with fixed precision                        | x==2 = "Two"
                                                                                                                    Predefined operations
      Integer: Integer number with virtually no limits
                                                              Type redefinition                                     Append two lists                          list1++list2
      Float: Floating number
                                                              Type NewTypeName = TypeValue                          Return element n                          list!!n
      Bool: Boolean. Takes two values: True or False.
                                                               Sample : Type String = [Char]                        Get the first/last element                head/last list
      Char: Character. Any character in the code is
                                                                                                                    Get the sum of all list elements          sum list
      placed between quotes (').
                                                              LISTS                                                 Get the product of all list elements      product list
      String: Strings (In fact, a list of Chars).
                                                              Tuples are designed to group data (multiple types     USE HASKELL
 Identity: ==, non identity /=                                allowed).                                              Interpreter
 Comparatives (type must be a subclass of Ord) :              (El1,El2,[Elx…])                                       Hugs : Available for Windows, Linux, FreeBSD and
 >, >=, <, <=                                                  Sample: ("James",41,1.85)                             MacOs X. http://www.haskell.org/hugs/
 if conditional then truePart else falsePart
                                                              Basic list creation                                   Compiler
Scripts types                                                 Lists are between [], elements are separated by        GHC (Glasgow Haskell Compiler) : Available for
 Classic (.hs) the code is more important                     comma.                                                 Windows, Linux.
 Litterals (.lhs) lines with code starts with >, lines with   Sample : [1,2,3,4,5] [“John”,“Paul”,“Andy”]            http://www.haskell.org/ghc/download.html
 comments don’t start by two hyphens.                         You can create lists by populate them with a range:
                                                              [1..5]=[1,2,3,4,5] [1..]=[1,2,3,4,5,6,…               Editors
Hugs basics                                                   (infinite)                                             - Any good text editor :)
 Load file :load filename                                                                                            - Visual Studio Haskell.
 reload file :reload                                          Comprehension lists                                    http://www.haskell.org/visualhaskell
 Launch file editor with current file :edit                   = creating liste using arithmetic operations or
 Get information about a function :info command               functions. [ body | generator ].                      Documentation
                                                              Samples :                                              - Haskell API search : http://www.haskell.org/hoogle/
FUNCTIONS                                                             [2*a | a <- [1..3]] = [2,4,6]                  - Haskell reference : ftp://ftp-
                                                                      [x*y | x <- [1..3], y <- [3..6] ]              developpez.com/c-
Declare a new function                                                [x | x <- [1,5,12,3,23,11,7,2], x>10]          maneu/langages/haskell/haskell-
Start with explicit type declaration (optional)                       [(x,y) | x <- [1,3,5], y <- [2,4,6],           reference.zip
                                                                                                                                                     Christopher MANEU
                                                                                                                                      Creative Commons Licence BY-NC-ND

To top