Skip to content

Latest commit

 

History

History
209 lines (178 loc) · 2.65 KB

ch02.adoc

File metadata and controls

209 lines (178 loc) · 2.65 KB

2 Hello, Haskell!

2.4 Functions

Intermission: Exercises

let half x = x / 2

let square x = x * x
dpi x = 3.14 * (x * x)

2.5 Infix

Associativity and precedence

Higher precedence is applied first. Scale is 0-9.

Prelude> :info (^)
(^) :: (Num a, Integral b) => a -> b -> a   -- Defined in ‘GHC.Real’
infixr 8 ^

Left associative

2 * 3 * 4
-- is evaluated as if it was
(2 * 3) * 4

Right associative

Prelude> 2 ^ 3 ^ 4
2417851639229258349412352
Prelude> 2 ^ (3 ^ 4)
2417851639229258349412352
Prelude> (2 ^ 3) ^ 4
4096

Intermission: Exercises

they differ

8 + 7 * 9 = 71
(8 + 7) * 9 = 135

they are the same

perimeter x y = (x * 2) + (y * 2)
perimeter x y = x * 2 + y * 2

they differ

f x = x / 2 + 9
f x = x / (2 + 9)

2.6 Declaring values

fix mistakes

let area x = 3. 14 * (x * x)
let area x = 3.14 * (x * x)
let double x = b * 2
let double x = x * 2
link:ch02_2.6_1.hs[role=include]

Prelude> :l ch02_2.6_1.hs
[1 of 1] Compiling Main             ( ch02_2.6_1.hs, interpreted )

ch02_2.6_1.hs:2:4: parse error on input ‘=’
Failed, modules loaded: none.

-- fixed
x = 7
y = 10
f = x + y

2.12 Let and where

Intermission: Exercises

let x = 5 in x
-- 5
let x = 5 in x * x
-- 25
let x = 5; y = 6 in x * y
-- 30
let x = 3; y = 1000 in x + 3
-- 6

let rewritten to where

f1 = x
    where x = 5

f2 = x * x
    where x = 5

f3 = x * y
    where x = 5
          y = 6

f4 = x + 3
   where x = 3
         y = 1000

More exercises!

let rewritten to where

link:ch02_2.12_1.hs[role=include]

2.13 Chapter Exercises

Parenthesization

  1. 2 + 2 * 3 - 1 == 2 + (2 * 3) - 1

  2. (^) 10 $ 1 + 1 == (^) 10 (1 + 1)

  3. 2 ^ 2 * 4 ^ 5 + 1 == ( (2 ^ 2) * (4 ^ 5) ) + 1

Equivalent expressions

  1. 1 + 1 == 2

  2. 10 ^ 2 == 10 + 9 * 10

  3. 400 - 37 /= (-) 37 400

  4. 100 `div` 3 /= 100 / 3

  5. 2 * 5 + 18 /= 2 * (5 + 18)

More fun with functions

z = 7
x = y ^ 2
waxOn = x * 5
y = z + 8

to REPL

let z = 7
let y = z + 8
let x = y ^ 2
let waxOn = x * 5

waxOn == 1125

  1. results of

    10 + waxOn
    -- 1135
    (+10) waxOn
    -- 1135
    (-) 15 waxOn
    -- -1110
    (-) waxOn 15
    -- 1110
  2. triple

    let triple x = x * 3
    triple waxOn
    -- 3375
  3. where waxOn

    link:ch02_2.12_2.hs[role=include]
  4. rest

    link:ch02_2.12_3.hs[role=include]

    waxOff is triple
    waxOff waxOn is triple waxOn