Skip to content

Latest commit

 

History

History
685 lines (511 loc) · 13.9 KB

haskell.org

File metadata and controls

685 lines (511 loc) · 13.9 KB

Haskell

Learn You a Haskell for Great Good!

Starting Out

List Operations

Text

haskell:

Concatenate two lists: {{c1:: LIST1 ++ LIST2 }}

Prepend an element to a list: {{c1:: ELEMENT:LIST }}

Get an element from a list by index: {{c1:: LIST !! INDEX }}

Get the first element of a list: {{c1:: head LIST }}

Get all but the first element of a list: {{c1:: tail LIST }}

Get the last element element of a list: {{c1:: last LIST }}

Get all but the last element of a list: {{c1:: init LIST }}

Get the length of a list: {{c1:: length LIST }}

Check that a list is empty: {{c1:: null LIST }}

Check that a list contains an element: {{c1:: ELEMENT `elem` LIST }}

List Ranges

Text

haskell: Get a list from a range of an enumerable: {{c1:: [FIRST..LAST] }}

List Comprehension

Text

haskell: Build a list comprehension: {{c1:: [OUTPUT_FUNCTION | INPUT_SET[, ...][, PREDICATE, ...] ]}}

List Comprehension Example

Text

haskell: Using list comprehensions, build a list of odd integers under 10: {{c1:: [x | x <- [1..10], odd x] ]}}

fst and snd

Text

haskell:

Extract the first element of a pair tuple: {{c1:: fst TUPLE }}

Extract the second element of a pair tuple: {{c1:: snd TUPLE }}

zip

Text

haskell: Create a list of tuples for each pair of elements in two lists: {{c1:: zip LIST1 LIST2 }}

Types and Typeclasses

:t in GHCI

Text

haskell: ghci: Show the type of an expression: {{c1:: :t EXPRESSION }}

Polymorphic Functions

Text

cs: {{c1::A function that can be applied to values of different types}} is a {{c2::polymorphic function}}

=>

Text

haskell: {{c1::=>}} is called a {{c2::Class Constraint}}

Syntax in Functions

Pattern Matching Example

Text

haskell: Define the factorial function with Pattern Matching:

{{c1::

factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)

}}

Function Guards Example

Text

haskell: Define the max function with Function Guards:

{{c1::

max' :: (Ord a) => a -> a -> a
max' a b
  | a > b = a
  | otherwise = b

}}

Using Prefix Functions with Infix Notation

Text

haskell: {{c1::Define or use prefix functions with infix notation}} by {{c2::enclosing the function in backticks}}

Examples:

{{c2::

myCompare :: (Ord a) => a -> a -> Ordering
a `myCompare` b
    | a > b     = GT
    | a == b    = EQ
    | otherwise = LT
ghci> 3 `myCompare` 2
GT

}}

where Clause in Function

Text

haskell: Easily bind variables for functions bodies and guards with {{c1::where}}

let Bindings

Text

haskell: Define let bindings: {{c1:: let BINDINGS in EXPRESSION }}

case Expression

Text

haskell: Define a case expression:

{{c1::

case EXPRESSION of PATTERN -> RESULT
                   PATTERN -> RESULT
                   [...]

}}

Higher order functions

Currying

Text

cs: {{c1::Currying}} is {{c2::the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument}}

Using Infix Functions with Prefix Notation

Text

haskell: {{c1::Partially apply or use infix functions with prefix notation}} by {{c2::enclosing the function in parentheses}}

Examples:

divideByTen = {{c2::(/10)}}
plus = {{c2::(+)}}

Fold

Text

cs: {{c1::A fold}} is {{c2::a function that processes a data-structure to build a return value}}

foldl1 and foldr1

Text

haskell: {{c1::Fold a list with the first or last element as the accumulator}} with {{c2::foldl1 or foldr1}}

scanl and scanr

Text

haskell: {{c1::Fold a list while creating a new list with the accumulators}} with {{c2::scanl or scanr}}

Function Composition

Text

haskell: {{c1::Compose two or more functions}} with {{c2::.}}

Making Our Own Types and Typeclasses

Defining a Type

Text

haskell: Define the Bool type: {{c1:: data Bool = True | False }}

Type Value Constructors

Text

haskell: In data Bool = True | False, True and False are called {{c1::Value Constructors}}

Defining a Type With Record Syntax

Text

haskell: Define a Person type with a first and last name and with record syntax:

{{c1::

data Person = Person { firstname :​: String
                     , lastname :​: String
                     } deriving (Show)

}}

Type Constructors

Text

haskell: In data Maybe a = Nothing | Just a, Maybe is called a {{c1::Type Constructor}}

Type Alias

Text

haskell: Alias a type with {{c1:: type ALIAS = ORIGINAL_TYPE }}

Either Type

Text

haskell: By convention, in the Either type, the errors use the {{c1::Left}} value constructor while the results use the {{c1::Right}} value constructor

{{c1::Mnemonic: “right” also means “correct”}}

Typeclass Example

Text

haskell: Define the Eq typeclass:

{{c1::

class Eq a where
    (==) :: a -> a -> Bool
    (/=) :: a -> a -> Bool
    x == y = not (x /= y)
    x /= y = not (x == y)

}}

Instance of Typeclass Example

Text

haskell: Make data TrafficLight = Red | Yellow | Green an instance of the Eq typeclass:

{{c1::

instance Eq TrafficLight where
    Red == Red = True
    Green == Green = True
    Yellow == Yellow = True
    _ == _ = False

}}

:i in GHCi

Text

haskell: ghci: Get typeclasses’ functions and instances: {{c1:: :i TYPE|TYPECLASS }}

Functor in CS

Text

cs: A Functor is a {{c1::type that can have a function applied to its value without changing the type}}

Functor in Haskell

Text

haskell: A Functor is a type that implements {{c1::fmap}}

:k in GHCi

Text

haskell: ghci: Get the kind of a type: {{c1:: :k TYPE }}

Input and Output

IO Actions in do Blocks

Text

haskell: Read and print a string:

{{c1::

main = do
  string <- getLine
  putStrLn string

}}

Result of a do Block

Text

haskell: The result of a do block is the {{c1::result of the last action in the block}}

Bind Monad To A Name

Text

haskell: Bind a monad to a name: {{c1:: NAME <- MONAD }}

return

Text

haskell: Inject a value in a monad: {{c1:: return VALUE }}

let Bindings

Text

haskell: Bind variables in do contexts:

{{c1::

main = do
  let var1 = value
      var2 = value

}}

sequence

Text

haskell: Transform a list of IO actions in a single IO action: {{c1::sequence [ACTION1, ACTION2]}}

mapM

Text

haskell: Map a function over a list and sequence that list: {{c1::mapM FUNCTION LIST}}

Functors, Applicative Functors and Monoids

Functor Laws

Text

haskell: The functor laws are: {{c1::

  • fmap id FUNCTOR = id FUNCTOR
  • fmap (f . g) FUNCTOR = fmap f (fmap g FUNCTOR)

}}

Applicative

Text

haskell: An applicative is a type that implements {{c1::pure and <*>}}

<*>

Text

haskell: {{c1::Apply a function in an applicative to a value in an applicative}} = {{c2::<*>}}

<$>

Text

haskell: {{c1::f <$> x}} = {{c2::fmap f x}}

(,)

Text

haskell: {{c1::(,)}} = {{c2::\x y -> (x,y)}}

liftA2

Text

haskell: {{c1::liftA2 f x y}} = {{c2::(fmap f x) <*> y}}

Monoid

Text

haskell: A monoid is a type that implements {{c1::mempty, mappend and mconcat}}

Monoid Laws

Text

haskell: The monoid laws are: {{c1::

  • mempty `mappend` x = x
  • x `mappend` mempty = x
  • (x `mappend` y) `mappend` z = x `mappend` (y `mappend` z)

}}

<>

Text

haskell: {{c1::<>}} = {{c2::mappend}}