more syntax slides

1 parent eb823bd commit 9536d80a40e38e897a7e551df269ca034626aa64 astro committed Aug 27, 2012
Showing with 78 additions and 5 deletions.
1. +26 −0 astro.hs
2. +52 −5 index.html
 @@ -0,0 +1,26 @@ +module Main where + +fac :: Integer -> Integer +fac 1 = 1 +fac n = n * fac (n - 1) + +fac' :: Integer -> Integer +fac' n = + case n of + 1 -> 1 + _ -> n * fac (n - 1) + +fac'' :: Integer -> Integer +fac'' n + | n <= 1 = 1 + | otherwise = n * fac (n - 1) + +fac''' :: Integer -> Integer +fac''' n = + if n <= 1 + then 1 + else n * fac (n - 1) + +facs = 1 : go 2 + where go :: Integer -> [Integer] + go n = n : map (* n) (go (n + 1))
 @@ -66,30 +66,77 @@

Funktionen

-
fac :: Integer -> Integer
+
fac :: Integer -> Integer
fac 1 = 1
fac n = n * fac (n - 1)

Fakultät

-> Impliziert

Pattern matching des Parameters

Integer is bignum

+ +

case

+
fac' :: Integer -> Integer
+fac' n =
+  case n of
+    1 -> 1
+    _ -> n * fac (n - 1)
+
+

Pattern matching

+

Anonyme Variable

Guards

-
fac :: Integer -> Integer
-fac n
+
fac'' :: Integer -> Integer
+fac'' n
| n <= 1    = 1
| otherwise = n * fac (n - 1)

Boolean expression

+ +

if then else

+
fac''' :: Integer -> Integer
+fac''' n =
+  if n <= 1
+  then 1
+  else n * fac (n - 1)
+
+

Boolean expression

+
+ +
+

Listen

+
\$ ghci
+Prelude> 2 : []
+[2]
+Prelude> 23 : []
+[23]
+Prelude> 23 : 42 : []
+[23,42]
+Prelude> 23 : 42 : 5 : []
+[23,42,5]
+Prelude> :t (:)
+(:) :: a -> [a] -> [a]
+
+

+
+ +
+

Listen

+
facs = 1 : go 2
+  where go :: Integer -> [Integer]
+        go n = n : map (* n) (go \$ n + 1)
+
+Prelude> facs !! 500
+611288549821546144419320631496946510053040745744399613938399207781308531950094880800353720198603844213369821855051249995665852739761166777440873387784823972024431693541604632490253390402221485016448489094881275898497140756808655499992463957536774753658273522343143352081610678258299859781290947510064133164625417126214683287364306533202043771696934819148200603488701298578593288295101675375228475039194518540926501811512211084741146359555616051391364512171815202852428391973770531819587555590747725222707870915089204322125602187745638954304887403405330317436980872692673627514602895425139803517823839455807896878364709108235036658336397561844224198282288969461157203049314666899393600517284430144216183492154249948251192663554129489210224225335784718743202172831724716080857069568307715110065035130707514179942202121569852186583424945734289322385681601382444331381680307765142985006245859526451817213228740320341074735777885270328149239431528071389670544598095262337701383222299067569757970109034425702794542448640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+
+

(\$)

Currying