mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
Funktionsapplikation
This commit is contained in:
parent
564dbc9790
commit
bb674b307b
7 changed files with 44 additions and 1 deletions
|
@ -133,6 +133,18 @@ in etwa folgendem Haskell-Code:
|
|||
\subsection{Let und where}\xindex{let}\xindex{where}%
|
||||
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/let-where-bindung.hs}
|
||||
|
||||
\subsection{Funktionskomposition}\xindex{.}\xindex{Funktionskomposition}%
|
||||
In Haskell funktioniert Funktionskomposition mit einem Punkt:
|
||||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/function-composition.hs}
|
||||
|
||||
Dabei ergibt \texttt{h (-3)} in der mathematischen Notation
|
||||
\[(g \circ f) (-3) = f(g(-3)) = f(-4) = 16\]
|
||||
und \texttt{i (-3)} ergibt
|
||||
\[(f \circ g) (-3) = g(f(-3)) = g(9) = 8\]
|
||||
Es ist also anzumerken, dass die Reihenfolge \underline{nicht} der mathematischen
|
||||
konvention entspricht.
|
||||
|
||||
\section{Typen}
|
||||
\subsection{Standard-Typen}
|
||||
Haskell kennt einige Basis-Typen:
|
||||
|
@ -245,6 +257,25 @@ wird wie folgt erzeugt:
|
|||
\subsection{Chruch-Zahlen}
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=church.hs]{haskell}{scripts/haskell/church.hs}
|
||||
|
||||
\subsection{Trees}\xindex{tree}\xindex{map!tree}%
|
||||
Einen Binärbaum kann man in Haskell so definieren:
|
||||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/binary-tree.hs}
|
||||
|
||||
Einen allgemeinen Baum so:
|
||||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/general-tree.hs}
|
||||
|
||||
Hier ist \texttt{t} der polymorphe Typ des Baumes. \texttt{t} gibt also an welche
|
||||
Elemente der Baum enthält.
|
||||
|
||||
Man kann auf einem solchen Baum auch eine Variante von \texttt{map} und
|
||||
\texttt{reduce} definieren,
|
||||
also eine Funktion \texttt{mapT}, die eine weitere Funktion \texttt{f} auf jeden
|
||||
Knoten anwendet:
|
||||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/mapt.hs}
|
||||
|
||||
\subsection{Standard Prelude}
|
||||
Hier sind die Definitionen eininger wichtiger Funktionen:
|
||||
\xindex{zipWith}\xindex{zip}
|
||||
|
|
Binary file not shown.
|
@ -105,7 +105,7 @@ Dieses skript soll man \texttt{swipl -f test.pl} aufrufen. Dann erhält man:
|
|||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/splits.sh}
|
||||
|
||||
\subsection{Delete}
|
||||
\subsection{Delete}\xindex{remove}\xindex{delete}%
|
||||
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/delete.pl}
|
||||
|
||||
\subsection{Zebrarätsel}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
data Tree a = Empty | Node a (Tree a) (Tree a)
|
||||
deriving (Show)
|
|
@ -0,0 +1,4 @@
|
|||
f x = x * x
|
||||
g x = x - 1
|
||||
h = (f . g)
|
||||
i = (g . f)
|
|
@ -0,0 +1 @@
|
|||
data Tree t = Node t[Tree t]
|
5
documents/Programmierparadigmen/scripts/haskell/mapt.hs
Normal file
5
documents/Programmierparadigmen/scripts/haskell/mapt.hs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mapT :: (t -> s) -> Tree t -> Tree s
|
||||
mapT f (Node x ts) = Node (f x) (map (mapT f) ts)
|
||||
|
||||
reduceT :: (t -> t -> t) -> Tree t -> t
|
||||
reduceT f (Node x ts) = foldl f x (map (reduceT f) ts)
|
Loading…
Add table
Add a link
Reference in a new issue