Date post: | 15-Apr-2017 |
Category: |
Software |
Upload: | luca-molteni |
View: | 1,159 times |
Download: | 0 times |
• Functions are first-class, that is, functions are values which can be used in exactly the same ways as any other sort of value.
• The meaning of Haskell programs is focused around evaluating expressions rather than executing instructions.
“An expression is said to be referentially transparent if it can be replaced with its value
without changing the behavior of a program (in other words, yielding a program that has the same effects and output on the same input).”
Referential transparency
“Because a referentially transparent expression can be evaluated at any time, it is not
necessary to define sequence points nor any guarantee of the order of evaluation at all.
Programming done without these considerations is called
purely functional programming.”
Referential transparency
totalArea r1 r2 r3
= pi * r1 ^ 2 +
pi * r2 ^ 2 +
pi * r3 ^ 2
Sum of the areas of three circles with radii r1, r2, r3
circleArea r = pi * r ^ 2
totalArea r1 r2 r3
= circleArea r1 +
circleArea r2 +
circleArea r3
Sum of the areas of three circles with radii r1, r2, r3
totalArea :: [Float] -> Float
totalArea [] = 0
Sum of the areas of three circles with radii r1, r2, r3
totalArea :: [Float] -> Float
totalArea [] = 0
totalArea (x : xs) =
circleArea x + totalArea xs
Sum of the areas of three circles with radii r1, r2, r3
totalArea :: [Float] -> [Float]
totalArea = map circleArea
Sum of the areas of three circles with radii r1, r2, r3
totalArea :: [Float] -> FloattotalArea = sum . map circleArea
Sum of the areas of three circles with radii r1, r2, r3
simple :: Int -> Int -> Int -> Int
simple 5 :: Int -> Int -> Int
simple 5 2 :: Int -> Int
simple 5 2 3 :: Int
Partial Application
Point free programming
Tacit programming (point-free programming) is a programming paradigm in which a function definition does not include information regarding its arguments, using combinators and function composition [...] instead of variables.
Point free programming
map (\x -> increment x) [2,3,4]
[3,4,5]
map increment [2,3,4]
[3,4,5]
map (\x -> x + 1) [2,3,4]
[3,4,5]
map (+1) [2,3,4]
[3,4,5]
increment :: Int -> Int
increment x = x + 1
Point free programming
mf criteria operator list = filter criteria (map operator list)
mf = (. map) . (.) . filter