2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 06:18:05 +02:00

Haskell-Beispiele hinzugefügt

This commit is contained in:
Martin Thoma 2014-03-18 19:31:04 +01:00
parent 137a44cd5e
commit 8e73ed0910
6 changed files with 69 additions and 2 deletions

View file

@ -122,6 +122,9 @@ in etwa folgendem Haskell-Code:
\texttt{tail "ABCDEF"} gibt \texttt{"BCDEF"} zurück.
\end{itemize}
\subsection{Let und where}\xindex{let}\xindex{where}%
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/let-where-bindung.hs}
\section{Typen}
\subsection{Standard-Typen}
Haskell kennt einige Basis-Typen:
@ -206,8 +209,18 @@ sich das ganze sogar noch kürzer schreiben:
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=fibonacci-zip.hs]{haskell}{scripts/haskell/fibonacci-zip.hs}
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=fibonacci-pattern-matching.hs]{haskell}{scripts/haskell/fibonacci-pattern-matching.hs}
\subsection{Quicksort}
TODO
\subsection{Polynome}\xindex{Polynome}\xindex{zipWith}\xindex{foldr}\xindex{tail}%
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=polynome.hs]{haskell}{scripts/haskell/polynome.hs}
\subsection{Hirsch-Index}\xindex{Hirsch-Index}\xindex{Ord}\xindex{Num}%
\textbf{Parameter}: Eine Liste $L$ von Zahlen aus $\mdn$\\
\textbf{Rückgabe}: $\max \Set{n \in \mdn | n \leq \|\Set{i \in L | i \geq n}\|}$
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=hirsch-index.hs]{haskell}{scripts/haskell/hirsch-index.hs}
\subsection{Lauflängencodierung}\xindex{Lauflängencodierung}\xindex{splitWhen}\xindex{group}\xindex{concat}%
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=lauflaengencodierung.hs]{haskell}{scripts/haskell/lauflaengencodierung.hs}
\subsection{Funktionen höherer Ordnung}\xindex{Folds}\xindex{foldl}\xindex{foldr}\label{bsp:foldl-und-foldr}
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=folds.hs]{haskell}{scripts/haskell/folds.hs}

View file

@ -0,0 +1,12 @@
import Data.List --sort und reverse
hIndex :: (Num a, Ord a) => [a] -> a
hIndex l = helper (reverse (sort l)) 0
where helper [] acc = acc
helper (z:ls) acc
| z > acc = helper ls (acc + 1)
| otherwise = acc
-- Alternativ
hindex1 = length . takeWhile id . zipWith (<=) [1..] . reverse . sort
hindex2 = length . takeWhile (\(i, n) -> n >= i) . zip [1..] . reverse . sort

View file

@ -0,0 +1,21 @@
splitWhen :: (a -> Bool) -> [a] -> ([a], [a])
splitWhen _ [] = ([], [])
splitWhen p (x:xs)
| p x = ([], x:xs)
| otherwise = let (ys, zs) = splitWhen p xs
in (x:ys, zs)
-- >>> splitWhen even [1,2,3]
-- ([1],[2,3])
group :: Eq a => [a] -> [[a]]
group [] = []
group (x:xs) = let (group1, rest) = splitWhen (/=x) xs
in (x:group1) : group rest
encode :: Eq a => [a] -> [(a, Int)]
encode xs = map (\x -> (head x, length x)) (group xs)
decode [] = []
decode ((x,n):xs) = replicate n x ++ decode xs
-- alternativ
decode = concat . (map (\(x,n)->replicate n x))

View file

@ -0,0 +1,5 @@
>>> let f = 3; g = f where f = 7
>>> f
3
>>> g
7

View file

@ -0,0 +1,16 @@
type Polynom = [Double]
add :: Polynom -> Polynom -> Polynom
add a [] = a
add [] a = a
add (x:xs) (y:ys) = (x+y) : add xs ys
eval :: Polynom -> Double -> Double
eval [] x = 0
eval (p:ps) x = p + x * (eval ps x)
-- alternativ:
eval p x = foldr (\element rest ->element+x*rest) 0 p
deriv :: Polynom -> Polynom
deriv [] = []
deriv p = zipWith (*) [1..] (tail p)