For data types
link:ch11_11.4_0.hs[role=include]
-
Doggies
is type constructor -
kind of
Doggies
is* -> *
-
kind of
Doggies String
is*
-
type of
Husky 10
isNum a ⇒ Doggies a
-
type of
Husky (10 :: Integer)
isDoggies Integer
-
type of
Mastiff "Scooby Doo"
isDoggies String
-
DogueDeBordeaux
is both type and data constructor -
type of
DogueDeBordeaux
isdoge → DogueDeBordeaux doge
-
type of
DogueDeBordeaux "doggie!"
isDogueDeBordeaux String
link:ch11_11.5_0.hs[role=include]
-
type of
myCar
isVehicle
-
define functions
link:ch11_11.5_1.hs[role=include]
-
define function (error on non Car)
link:ch11_11.5_2.hs[role=include]
-
Plane Airline
forgetManu
error isNon-exhaustive patterns in function getManu
. You can add all catching pattern, but it would have to end up in error anyway, as there is noManufacturer
for non-Car value. -
add size for
Plane
, can be done similary asPrice
withDouble
instead ofInteger
link:ch11_11.5_3.hs[role=include]
-
cardinality 1
data PugType = PugData
-
cardinality 3
data Airline = PapuAir | CatapultsR'Us | TakeYourChancesUnited
-
Int16
has cardinality65535
-
Int
is instance ofBounded
, therefore cardinality can be determined.Integer
does not have bounds -
Int8
has256
values, which corresponds to2 ^ 8 = 256
data Example = MakeExample deriving Show
-
MakeExample
has typeExample
.:t Example
gives errorNot in scope: data constructor ‘Example’
-
:i Example
shows that it is instance ofShow
typeclass -
:t MakeExample
givesInt → Example
type synonym does not work
link:ch11_11.7_0.hs[role=include]
GHC pragma GeneralizedNewtypeDeriving
works for tooMany (Goats 45)
even without declaring an instance. Reusing Int
instance.
link:ch11_11.7_1.hs[role=include]
-
without
newtype
link:ch11_11.7_2.hs[role=include]
with
newtype
link:ch11_11.7_3.hs[role=include]
-
TooMany
instance for(Int, Int)
, sum the valueslink:ch11_11.7_4.hs[role=include]
-
TooMany
instance for(Num a, TooMany a) ⇒ (a, a)
NoteTooMany a
as class constraint means we know what to do withtoomany x
. If we use(n + n') > 42
as the implementation, then we get error as there is noOrd
forTooMany
.link:ch11_11.7_5.hs[role=include]
Warning
|
All of the following breaks. Ambiguous type or could not deduce class constraint. |
link:ch11_11.7_6.hs[role=include]
-
cardinality is 4.
Bool
is 2 and sum type addsdata BigSmall = Big Bool | Small Bool deriving (Eq, Show)
-
cardinality is 258.
Int8
is 256,Bool
is 2, sum type adds.let myNumba = Numba (-128)
breaks, details in https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/syntax-extns.htmlNegativeLiterals
. Number out ofInt8
range gives errorLiteral 1000 is out of the Int8 range -128..127
for1000 :: Int8
import Data.Int data NumberOrBool = Numba Int8 | BoolyBool Bool deriving (Eq, Show)
link:ch11_11.10_2.hs[role=include]
BookType
gone
link:ch11_11.10_3.hs[role=include]
-
4 + 4 = 8
data Quad = One | Two | Three | Four deriving (Eq, Show) eQuad :: Either Quad Quad eQuad = ???
-
4 * 4 = 16
prodQuad :: (Quad, Quad)
-
4 ^ 4 = 256
funcQuad :: Quad -> Quad
-
2 * 2 * 2 = 16
prodTBool :: (Bool, Bool, Bool)
-
(2 ^ 2) ^ 2 = 16
gTwo :: Bool -> Bool -> Bool
-
(4 ^ 4) ^ 2 = 65536
fTwo :: Bool -> Quad -> Quad
Simple solution
link:ch11_11.15_1.hs[role=include]
Eventually cooler and better solution
link:ch11_11.15_1_0.hs[role=include]
The big shift was to realize that the folding function needs to take 3 arguments, not the usual 2 as seen in folds so far.
link:ch11_11.15_2.hs[role=include]
The book gives this signature foldTree :: (a → b → b) → b → BinaryTree a → b
though :/, let’s do as the book says.
link:ch11_11.15_2_0.hs[role=include]
Warning
|
But this way you cannot recreate the original tree!!! |
-
a)
Weekday
is a type with five data constructorsdata Weekday = Monday | Tuesday | Wednesday | Thursday | Friday
-
c) type of
f Friday = "Miller Time"
isf :: Weekday → String
-
b) Types defined with the
data
keyword must begin with a capital letter -
c) The function
g xs = xs !! (length xs - 1)
delivers the final element ofxs
-
isSubsequenceOf
link:ch11_11.16_1.hs[role=include]
-
capitalizeWords
link:ch11_11.16_2.hs[role=include]