mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-25 22:38:04 +02:00
Viel zu Haskell ergänzt; Funktionen höherer Ordnung beschrieben
This commit is contained in:
parent
aa2454bb30
commit
78368fa6e9
17 changed files with 220 additions and 24 deletions
|
@ -1,3 +1,4 @@
|
|||
binom :: (Eq a, Num a, Num a1) => a -> a -> a1
|
||||
binom n k =
|
||||
if (k==0) || (k==n)
|
||||
then 1
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
f :: Floating a => a -> a
|
||||
f x = sin x / x
|
||||
|
||||
g :: Floating a => a -> a
|
||||
g x = x * (f (x*x))
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
fakAcc :: (Eq a, Num a) => a -> a -> a
|
||||
fakAcc n acc = if (n==0)
|
||||
then acc
|
||||
else fakAcc (n-1) (n*acc)
|
||||
|
||||
fak :: (Eq a, Num a) => a -> a
|
||||
fak n = fakAcc n 1
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
fak :: (Eq a, Num a) => a -> a
|
||||
fak n = if (n==0) then 1 else n * fak (n-1)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
fibAkk n n1 n2
|
||||
| (n == 0) = n1
|
||||
| (n == 1) = n2
|
||||
| otherwise = fibAkk (n - 1) n2 (n1 + n2)
|
||||
fib n = fibAkk n 0 1
|
|
@ -0,0 +1,3 @@
|
|||
fib 0 = 0
|
||||
fib 1 = 1
|
||||
fib n = fib (n - 1) + fib (n - 2)
|
|
@ -0,0 +1 @@
|
|||
fib = 0 : 1 : zipWith (+) fibs (tail fibs)
|
|
@ -1 +1,4 @@
|
|||
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
|
||||
fib n
|
||||
| (n == 0) = 0
|
||||
| (n == 1) = 1
|
||||
| otherwise = fib (n - 1) + fib (n - 2)
|
|
@ -0,0 +1,12 @@
|
|||
Prelude> head []
|
||||
*** Exception: Prelude.head: empty list
|
||||
Prelude> tail []
|
||||
*** Exception: Prelude.tail: empty list
|
||||
Prelude> tail [1]
|
||||
[]
|
||||
Prelude> head [1]
|
||||
1
|
||||
Prelude> null []
|
||||
True
|
||||
Prelude> null [[]]
|
||||
False
|
|
@ -0,0 +1,4 @@
|
|||
Prelude> let mylist = [1,2,3,4,5,6]
|
||||
Prelude> let test = [x | x <- mylist, x>2]
|
||||
Prelude> test
|
||||
[3,4,5,6]
|
|
@ -0,0 +1,3 @@
|
|||
qsort [] = []
|
||||
qsort (p:ps) = (qsort (filter (<=p) ps))
|
||||
++ p:(qsort (filter (> p) ps))
|
3
documents/Programmierparadigmen/scripts/haskell/qsort.hs
Normal file
3
documents/Programmierparadigmen/scripts/haskell/qsort.hs
Normal file
|
@ -0,0 +1,3 @@
|
|||
qsort [] = []
|
||||
qsort (p:ps) = (qsort (filter (\x -> x<=p) ps))
|
||||
++ p:(qsort (filter (\x -> x> p) ps))
|
|
@ -0,0 +1,35 @@
|
|||
Prelude> let x = \x -> x*x
|
||||
Prelude> :t x
|
||||
x :: Integer -> Integer
|
||||
Prelude> x(2)
|
||||
4
|
||||
Prelude> x(2.2)
|
||||
<interactive>:6:3:
|
||||
No instance for (Fractional Integer)
|
||||
arising from the literal `2.2'
|
||||
Possible fix: add an instance declaration for
|
||||
(Fractional Integer)
|
||||
In the first argument of `x', namely `(2.2)'
|
||||
In the expression: x (2.2)
|
||||
In an equation for `it': it = x (2.2)
|
||||
|
||||
|
||||
Prelude> let mult = \x y->x*y
|
||||
Prelude> mult(2,5)
|
||||
<interactive>:9:5:
|
||||
Couldn't match expected type `Integer' with
|
||||
actual type `(t0, t1)'
|
||||
In the first argument of `mult', namely `(2, 5)'
|
||||
In the expression: mult (2, 5)
|
||||
In an equation for `it': it = mult (2, 5)
|
||||
Prelude> mult 2 5
|
||||
10
|
||||
Prelude> :t mult
|
||||
mult :: Integer -> Integer -> Integer
|
||||
|
||||
Prelude> let concat = \x y -> x ++ y
|
||||
Prelude> concat [1,2,3] [3,2,1]
|
||||
[1,2,3,3,2,1]
|
||||
Prelude> :t concat
|
||||
concat :: [a] -> [a] -> [a]
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue