given data type, answer
data Mood = Blah | Woot deriving Show
-
type constructor is
Mood
-
values are
Blah
andWoot
-
:: Mood → Woot
to:: Mood → Mood
, asWoot
is value, not type -
change mood
link:ch04_4.2_0.hs[role=include]
-
done :)
awesome = ["Papuchon", "curry", "Haskell"]
alsoAwesome = ["Quake", "The Simons"]
allAwesome = [awesome, alsoAwesome]
-
length
-
type signature
:: [a] -> Int
-
takes one argument
-
result is
Int
-
-
results
-
length [1, 2, 3, 4, 5] == 5
-
length [(1, 2), (2, 3), (3, 4)] == 3
-
length allAwesome == 2
-
length (concat allAwesome) == 5
-
-
second breaks
-- works 6 / 3 -- both values need to be Fractional, length returns Int 6 / length [1, 2, 3]
-
fixed previous example
6 `div` length [1, 2, 3]
-
2 + 3 == 5
type isBool
and result isTrue
as5 == 5
-
type is
Bool
, result isFalse
as8 /= 5
Prelude> let x = 5 Prelude> x + 3 == 5
-
as follows
Prelude> length allAwesome == 2 -- works - True Prelude> length [1, 'a', 3, 'b'] -- breaks on types, list cannot be heterogenic Prelude> length allAwesome + length awesome -- works - 5 Prelude> (8 == 8) && ('b' < 'a') -- works - False Prelude> (8 == 8) && 9 -- breaks - 9 is not a Bool
-
palidrome function
link:ch04_4.7_0.hs[role=include]
-
return absolute value
link:ch04_4.7_1.hs[role=include]
-
tuple function
link:ch04_4.7_2.hs[role=include]
-
length + 1
link:ch04_4.7_3.hs[role=include]
-
identity
\x -> x
-
head
\ (x:xs) -> x
-
fst
f (a, b) = a