1
Welcome to Functional Programming!
A language that doesn't affect the way you think about programming, is not worth knowing. Alan Perlis
Four main families of languages …
1. Procedural (C is the ultimate survivor …)
2. Object-Oriented (Java, C#, Smalltalk, Eiffel …)
3. Functional (Haskell, Lisp, F# …)
4. Logic (Prolog …)
Most straddle families, e.g. C++, F#
Scripting languages are usually a hybrid mix (php, Perl, Python, Ruby)
When is a language functional?
A purely functional language has no variables, no statements, and no assignments!
Work gets done by using the arguments passed to a function, and by creating a new result value for the function.
An example from school …
f x = 3*(x^2) + 2*x + 8 f 2 24
Two recursive examples … Factorial
N’th Fibonacci number fact 0 = 1
fact n = n * fact(n-1)
fib 0 = 0 fib 1 = 1
fib n = fib(n-1) + fib(n-2)
Components of a Haskell Function
fact :: Integer -> Integer fact n = if n==0 then
1 else
n * fact (n-1)
Type signature
Function definition
2 Points to note
No Variables - Only Functions!!
No begins or ends, rather uses layout. (like Python) No parentheses for parameters of functions.
Omit when calling a function too.
minimalist syntax philosophy.
Operators follow C#, C++, Java conventions, i.e. use ==
to test for equality, &&for logical AND, ||for logical OR, and so on.
Comments: {- nestableblock comments -} or -- comment rest current line.
Language is case sensitive.
Basic Types
Int Integer Double Float Char Bool functions operators
1024
123456789101112 3.1415926 2.78 'a', 'b',..
True, False sin, cos, sqrt +, :, % ...
Two built-in data structuring mechanisms …
1.
Lists e.g. [12, 15, 23]
Monomorphic(every element has to be of the same type, unlike Python)
Variable length
2.
Tuples e.g. ("Joe", 21, 1.94)
A “record”. Each “field” can be a different type.
Types in Haskell
Haskell is strongly typed. You generally cannot mix types, and there is minimal support for type casts and coercion.
The type of every Haskell value can be determined.
Type signatures express the type of a value.
Some type signatures:
123 :: Int 3.14159 :: Double 'a' :: Char [2,3,5,7] :: [Int]
odd :: Int -> Bool
Say “of type”
Pronounce as “maps to” or "into" or "to"
Lists
[] is the empty list
: called “cons” (construction / concatenation) operator to make a new list by adding an extra element onto those from an existing list.
(1 : (2 : (3 : [ ]))) builds a 3 element list.
The cons operator is right-associative, meaning we can drop the parentheses.
1 : 2 : 3 : [ ]
The shorthand [1, 2, 3] means exactlythe same.
3 Strings are a shorthand
type String = [Char]
"Hello" is a list of 5 characters.
Same as 'h' : 'e' : 'l' : 'l' : 'o' : [ ] or ... ['h','e','l','l','o']
Taking a list apart:
the accessor functions ...
head (h:t) returns h, the first element of a
non-empty list.
tail (h:t) returns t, the list behind the
head of a non-empty list.
It is an error to apply heador tailto [ ]
•null es returns True(if esis empty) or False
Is a list empty?
Examples...
head "train"
tail [1,2,3]
head [ ]
head (tail (tail [0,1,2,3,4]))
= head (tail [1,2,3,4])
= head [2,3,4]
= 2
't'
[2,3]
ERROR!!!!
The list is a recursive data structure...
A list is either empty, or it is a head element followed by a list of elements of the same type.
Exercises
Write a function which returns the length of a string.
Write a function which returns the sum of a list of Integer
The identities that relate the functions head, tail, and (:)
head(h:t) = h tail(h:t) = t
Notice that we referred to the cons operatoras a function. Are operators and functions different?
In FP speak, an operator is just a function that is written between its operands - i.e. an operator is an infix function.
4