Skip to content

Latest commit

 

History

History
267 lines (254 loc) · 4.73 KB

ch05.adoc

File metadata and controls

267 lines (254 loc) · 4.73 KB

5 Types

5.4 Typeclass-constrained type variables

Intermission: Exercises

pair functions and signatures

  1. a) c)

    not :: Bool -> Bool
  2. b) d)

    length :: [a] -> Int
  3. c) b)

    concat :: [[a]] -> [a]
  4. d) a)

    head :: [a] -> a
  5. e) e)

    (<) :: Ord a => a -> a -> Bool

5.5 Currying

test like this

Prelude> let f :: a -> a -> a -> a; f = undefined
Prelude> :t f undefined
f undefined :: a -> a -> a
Prelude> :t f 1
f 1 :: Num a => a -> a -> a
Prelude> :t f (1 :: Int)
f (1 :: Int) :: Int -> Int -> Int

Intermission: Exercises

  1. a)

    f :: a -> a -> a -> a
    f 'a' :: Char -> Char -> Char
  2. d)

    g :: a -> b -> c -> b
    g 0 'c' "woot" :: Char
  3. d)

    h :: (Num a, Num b) => a -> b -> b
    h 1.0 2 :: Num b => b
  4. c)

    h :: (Num a, Num b) => a -> b -> b
    h 1 (5.5 :: Double) :: Double
  5. a)

    jackal :: (Ord a, Eq b) => a -> b -> a
    jackal "keyboard" "has the word jackal in it" :: [Char]
  6. e)

    jackal :: (Ord a, Eq b) => a -> b -> a
    jackal "keyboard" "has the word jackal in it" :: Eq b => b -> [Char]
  7. d)

    kessel :: (Ord a, Num b) => a -> b -> a
    kessel 1 2 :: (Num a, Ord a) => a
  8. a)

    kessel :: (Ord a, Num b) => a -> b -> a
    kessel 1 (2 :: Integer) :: (Num a, Ord a) => a
  9. c)

    kessel :: (Ord a, Num b) => a -> b -> a
    kessel (1 :: Integer) 2 :: Integer

5.6 Polymorphism

Intermission: Exercises

  1. id

    f :: a -> a
    f a = a
  2. return first or second argument

    f :: a -> a -> a
    f a b = a
    f a b = b
  3. only one implementation, like id

    f :: a -> b -> b
    f a b = b

5.7 Type inference

Intermission: Exercises

  1. [Char] → [Char]

    (++) :: [a] -> [a] -> [a]
    myConcat x = x ++ " yo"
  2. Fractional a ⇒ a → a

    (*) :: Num a => a -> a -> a
    myMult x = (x / 3) * 5
  3. Int → [Char]

    take :: Int -> [a] -> [a]
    myTake x = take x "hey you"
  4. Int → Bool

    (>) :: Ord a => a -> a -> Bool
    myCom x = x > (length [1..10])
  5. Char → Bool

    (<) :: Ord a => a -> a -> Bool
    myAlph x = x < 'z'

5.9 Chapter Exercises

Multiple choice

  1. c) value of type [a] is a list whose elements are all of some type a

  2. a) function of type [[a]] → [a] could take a list of strings as an argument

  3. b) function of type [a] → Int → a returns one element of type a from a list

  4. c) function of type (a, b) → a takes a tuple argument and returns the first value

Determine the type

  1. return value and type

    1. 54 :: Num a ⇒ a

    2. (0, "doge) :: Num t ⇒ (t, [Char])

    3. (0, "doge) :: (Integer, [Char])

    4. False :: Bool

    5. 5 :: Int

    6. False :: Bool

  2. type is Num a ⇒ a

  3. y is shadowed, type is Num a ⇒ a → a

  4. type is Fractional ⇒ a :: a

  5. type is [Char]

Does it compile?

  1. wahoo = bigNum $ 10 breaks, because bigNum is a value, not a function

  2. works fine in GHCI, should work too in file

  3. breaks on c = b 10, b has value 5, cannot apply value to a value

  4. breaks on b = 10000 * c, c is not defined

Type variable or specific type constructor?

  1. f :: zed → Zed → Blah is fully polymorphic, concrete and concrete

  2. f :: Enum b ⇒ a → b → C is fully polymorphic, constrained polymorphic and concrete

  3. f :: f → g → C is fully polymorphic, fully polymorphic and concrete

Write a type signature

  1. functionH (x:_) = x type signature functionH :: [a] → a

  2. functionC x y = if (x > y) then True else False type signature functionC :: Ord a ⇒ a → a → Bool

  3. functionS (x, y) = y type signature functionS :: (a, b) → b

Given a type, write the function

  1. i :: a → a; i a = a or id

  2. c :: a → b → a; c a _ = a

  3. c'' :: b → a → b; c :: a → b → a they are the same

  4. c' :: a → b → b; c _ b = b

  5. r :: [a] → [a] possibly r a = tail a or r a = reverse a

  6. co :: (b → c) → (a → b) → (a → c); co = (.)

  7. a :: (a → c) → a → a; a f x = x basically ignore the first argument

  8. a' :: (a → b) → a → b; a' = ($)

Fix it

link:ch05_5.9_0.hs[role=include]
link:ch05_5.9_1.hs[role=include]
link:ch05_5.9_2.hs[role=include]

Type-Kwon-Do

full example for this section, it is al only about type checking

link:ch05_5.9_3.hs[role=include]
  1. just a composition

    link:ch05_5.9_4.hs[role=include]
  2. also a composition

    link:ch05_5.9_5.hs[role=include]
  3. once u got the point, it is kinda easy

    link:ch05_5.9_6.hs[role=include]
  4. neat

    link:ch05_5.9_7.hs[role=include]