Haskell Notes

JerryXia 发表于 , 阅读 (19)

defining functions

Haskell is a functional programming language. This basically means functions are first-class citizens.Let’s look at how to define a function.

add x y = x + yadd 1 23

functions are partial

In haskell, functions can be partially applied (applied with less arguments than defined)

add1 = add 1add1 23

If we apply add with only one argument 1, then we get a new function add1 which takes one argument.

infix and prefix

In haskell, infix functions (i.e. operators in other languages) and prefix functions are treated alike. They can be transformed into each other using () and ` `

-- infix -> prefix1 + 23(+) 1 23-- prefix -> infixmyadd x y = x + y1 `add` 23

let

The let statement provides syntactic sugar for "defining variables".

let x = 1    y = 2    add x y = x + yin 1 + (add x y)4

I put quotes around “defining variables” because x and y are not variables in other languages’ sense. Instead, they are just bindings local to the statement, which is somewhat unique to haskell. The let statement can also define functions.

closures/anonymous functions/lambdas

-- the follwing are equivalent:add x y = x + yadd = \x y -> x + y-- the follwing are equivalent:add 1\x add 1 x

lazy evaluation

-- the cycle functiontake 10 (cycle [1,2,3])[1,2,3,1,2,3,1,2,3,1]-- one way to define the cycle functioncycle x = x ++ cycle x

cycle [1,2,3] generates an infinite list 1,2,3,1,2,3,1,2,3,.... Because Haskell is lazy, applying take 10 to that list actually yields an finite list ``.

type, type class

a type class is like a Java interface a type, a type is like a concrete Java class implementing an interface,

We use data to define a type.

data Point = Point Int Int deriving ShowPoint 1 2

The Show typeclass defines a show function which is used to print a data type. We can redefine this function as follows:

data Point = Point Int Intinstance Show Point where    show (Point x y) = (show x) ++ "," ++ (show y)

type constructor

data Maybe a = Nothing | Just a

We call Maybe a type constructor. Depending on the type of a, this constructor can end up creating a Maybe Int, Maybe Car, or Nothing.

functors

The Maybe is an instance of Functor type class. A Functor is simply something that you can map on.

class Functor f where    fmap :: (a -> b) -> f a -> f b-- lists are instances of functorsinstance Functor [] where    fmap = map-- maybe's are instances of functorsinstance Functor Maybe where    fmap f (Just x) = Just (f x)    fmap f Nothing = Nothingfmap inc (Just 1)

It’s easy (or not so easy) to see that Lists and Maybe's are instances of Functors.

pattern matching and erlang-like case statements

As seen above, pattern matching can be used in function arguments. It can also be used in case statements and let statements.

let (Point x y) = (Point 1 2) in    x + yhead' :: [a] -> ahead' xs = case xs of [] -> error "No head for empty lists!"                      (x:_) -> x

:kind :info :type

  • :kind shows type information on types and type classes
  • :info shows detailed information on types and type classes.
  • :type shows type for instances of types, and type classes for types.