more syntax/types slides

1 parent 5324e85 commit 0eb93d31f5123ead0346c2d36dbdf440d3bdcd0f astro committed Aug 28, 2012
Showing with 84 additions and 7 deletions.
1. +9 −0 astro.hs
2. +75 −7 index.html
9 astro.hs
 @@ -24,3 +24,12 @@ fac''' n = facs = 1 : go 2 where go :: Integer -> [Integer] go n = n : map (* n) (go (n + 1)) + +fib :: Integer -> Integer +fib 0 = 0 +fib 1 = 1 +fib n = + let r = fib \$ n - 1 + r' = fib \$ n - 2 + in r + r' +
82 index.html
 @@ -106,7 +106,7 @@

Listen

-
\$ ghci
+
\$ ghci

REPL

Prelude> 2 : [] [2] Prelude> 23 : [] @@ -118,30 +118,98 @@ Prelude> :t (:) (:) :: a -> [a] -> [a]
+
Prelude> :t map
+map :: (a -> b) -> [a] -> [b]
+Prelude> :t filter
+filter :: (a -> Bool) -> [a] -> [a]
+Prelude> :t foldl
+foldl :: (a -> b -> a) -> a -> [b] -> a
+

-

Listen

+

let

+
fib :: Integer -> Integer
+fib 0 = 0
+fib 1 = 1
+fib n =
+  let r = fib \$ n - 1
+      r' = fib \$ n - 2
+  in r + r'
+
+ +

where

facs = 1 : go 2
-  where go :: Integer -> [Integer]
+  where go :: Integer -> [Integer]  “Unterfunktion”
go n = n : map (* n) (go \$ n + 1)

-Prelude> facs !! 500
+Prelude> :t facs  Inferred type
+facs :: [Integer]
+Prelude> facs !! 500   Unendlich, lazy
611288549821546144419320631496946510053040745744399613938399207781308531950094880800353720198603844213369821855051249995665852739761166777440873387784823972024431693541604632490253390402221485016448489094881275898497140756808655499992463957536774753658273522343143352081610678258299859781290947510064133164625417126214683287364306533202043771696934819148200603488701298578593288295101675375228475039194518540926501811512211084741146359555616051391364512171815202852428391973770531819587555590747725222707870915089204322125602187745638954304887403405330317436980872692673627514602895425139803517823839455807896878364709108235036658336397561844224198282288969461157203049314666899393600517284430144216183492154249948251192663554129489210224225335784718743202172831724716080857069568307715110065035130707514179942202121569852186583424945734289322385681601382444331381680307765142985006245859526451817213228740320341074735777885270328149239431528071389670544598095262337701383222299067569757970109034425702794542448640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+

(\$)

+

Lambdas

+

Num: Interface für (+), (*)

+
Prelude> :t (\n -> n + 1)
+(\n -> n + 1) :: Num a => a -> a
+Prelude> (\n -> n + 1) 22
+23
+
+
Prelude> :t (\a b c -> a * b * c)
+(\a b c -> a * b * c) :: Num a => a -> a -> a -> a
+
+
Prelude> :t (\a -> \b -> \c -> a * b * c)
+(\a -> \b -> \c -> a * b * c) :: Num a => a -> a -> a -> a
+
+

Umkehrschluß: Currying

+
+

Currying

+
:t map (\a -> a + 1)
+map (\a -> a + 1) :: Num b => [b] -> [b]
+
+ +

Sections

+
Prelude> :t (+ 1)
+(+ 1) :: Num a => a -> a
+Prelude> map (+ 1) [1..10]
+[2,3,4,5,6,7,8,9,10,11]
+
+
+ + +
+

data

+

Statt NULL-Pointer

+
data Maybe a = Nothing
+             | Just a
+
+
Prelude> :t Nothing   Constructors
+Nothing :: Maybe a   Passt auf alle a
+Prelude> :t Just
+Just :: a -> Maybe a
+
+
data [] a = []
+          | a : [a]
+

newtype

+
newtype Name = Name String  Typename = Ctor
+
+
• Darf nur 1 Feld haben
• +
• Billig zur Laufzeit
• +
• Typsicher zur Compile-Zeit
• +
- + +