Duncan Warukira
warukira's notes

warukira's notes

Learn Haskell

My journey to the heart of Haskell

Duncan Warukira's photo
Duncan Warukira
·Nov 9, 2021·

1 min read

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"
 
Share this