Learn Haskell

My journey to the heart of Haskell

Defining functions

Basic definition

next x = x + 1

Here we define a function that returns a number that is one greater than its argument.

  • The name of the function - next
  • The argument - x
add x y = x + y
hello x = "Hello " ++ x

A function can return multiple values.

helloNext x = (next x, hello (show(next x)) )

Pattern matching

firstName "Me" = "Hello, Me"
firstName "Dun" = "HI"
firstName x= hello x

Branching conditionals

Haskell uses If-then-else example

 f x = if even x then putStrLn "even" else  putStrLn "old"