home Grundlagen der Funktionalen Programmierung: Funktionsdefinitionen Prof. Dr. Uwe Schmidt FH Wedel

Funktionsdefinitionen

weiter

weiter

Beispiele
weiter
isDigit :: Char -> Bool
isDigit c = c >= '0' && c <= '9'
merke
Diese Funktion ist vordefiniert in Data.Char
weiter
even :: Integral a => a -> Bool
even n = n `mod` 2 == 0
merke
Diese Funktion ist im Prelude vordefiniert
weiter
splitAt :: Int -> [a] -> ([a], [a])
splitAt n xs = (take n xs, drop n xs)
merke
Diese Funktion ist im Prelude vordefiniert
weiter
Verzweigungen
Syntax
if e1 then e2 else e3
abs :: (Ord a, Num a) => a -> a
abs n = if n >= 0 then n else negate n
 
-- mehrzeilig mit Einrückung
 
abs n =
  if n >= 0
  then  n
  else -n  -- negate n
weiter
signum :: Num a => a -> a
signum n =
  if n < 0
  then -1
  else
    if n > 0
    then 1
    else 0
merke
Unübersichtlich: Geschachtelte Verzweigungen
weiter
Wächter
Alternative Syntax für Verzweigungen
gut
Regelartige Syntax
weiter
abs n
  | n >= 0    =  n
  | otherwise = -n
 
signum n
  | n <  0 = -1
  | n >  0 =  1
  | n == 0 =  0
weiter
Mehrweg-Verzweigungen
durch case-Ausdrücke
data Ordering = LT | EQ | GT
 
compare :: Ord a => a -> a -> Ordering
 
signum n =
  case compare n 0 of
    LT -> -1
    EQ ->  0
    GT ->  1
weiter
Verzweigungen
durch Mustervergleich (pattern matching)
not :: Bool -> Bool
 
-- .1
not x = if x then False else True
 
-- .2
not False = True
not True  = False
 
-- .3
not False = True
not _     = False
weiter
(&&) :: Bool -> Bool -> Bool
 
True  && True  = True
True  && False = False
False && True  = False
False && False = False
 
-- oder
 
True  && True  = True
_     && _     = False
 
-- oder
 
True  && b     = b
False && _     = False
 
-- oder
 
a && b = if a then b else False
weiter
Muster
für Tupel
fst :: (a, b) -> a
fst (x, _) = x
 
snd :: (a, b) -> b
snd (_, y) = y
weiter
Muster
für Listen
null :: [a] -> Bool
null [] = True
null _  = False
 
head :: [a] -> a
head (x : _) = x
 
test1 :: [Char] -> ...
test1 ['a']                  = ...
test1 ['a', 'b', 'c']        = ...
test1 "abc"                  = ...
test1 ('a' : 'b' : 'c' : cs) = ...
test1 [_, 'b', _]            = ...
test1 (c1 : 'b' : c3 : [])   = ...
test1 xs                     = ...
weiter
Zwischenergebnisse
mit where-Klauseln
odds :: Int -> [Int]
odds n =
  map f [.. n1]
  where
    n1  = n - 1
    f x = 2 * x + 1
 
map :: (a -> b) -> [a] -> [b]  -- vordefiniert

weiter

Beispiele zum Kapitel

   1module FctDef
   2where
   3
   4import Prelude hiding ( splitAt
   5                      , recip
   6                      , abs
   7                      , signum
   8                      , not
   9                      , fst, snd
  10                      , null, head, tail
  11                      , const
  12                      , even
  13                      )
  14
  15import qualified Prelude as P
  16
  17-- Funktionsdefinitionen
  18
  19isDigit :: Char -> Bool
  20isDigit c
  21    = c >= '0' && c <= '9'
  22
  23even :: Integral a => a -> Bool
  24even n
  25    = n `mod` 2 == 0
  26
  27splitAt :: Int -> [a] -> ([a], [a])
  28splitAt n xs
  29    = (take n xs, drop n xs)
  30
  31recip :: Fractional a => a -> a
  32recip x = 1 / x
  33
  34-- Verzweigungen
  35
  36abs n
  37    = if n >= 0
  38      then n
  39      else -n
  40
  41signum n
  42    = if n < 0
  43      then -1
  44      else if n == 0
  45           then 0
  46           else 1
  47
  48-- Waechter (guards)
  49
  50abs' n
  51    | n >= 0    = n
  52    | otherwise = -n
  53
  54signum' n
  55    | n <  0 = -1
  56    | n == 0 =  0
  57    | n >  0 =  1
  58
  59-- Typen von abs, signum ?
  60
  61-- Mustervergleiche (pattern matching)
  62
  63not :: Bool -> Bool
  64not x
  65    = if x then False else True
  66
  67not' False = True
  68not' True  = False
  69
  70not'' False = True
  71not'' _     = False
  72
  73(&&&) :: Bool -> Bool -> Bool
  74False &&& False = False
  75False &&& True  = False
  76True  &&& False = False
  77True  &&& True  = True
  78
  79(.&&.) :: Bool -> Bool -> Bool
  80True .&&. True = True
  81_    .&&. _    = False
  82
  83(<&&>) :: Bool -> Bool -> Bool
  84True  <&&> b = b
  85_     <&&> _ = False
  86
  87(&&&&) :: Bool -> Bool -> Bool
  88a &&&& b = if a
  89           then b
  90           else False
  91
  92fst (x, _) = x
  93
  94snd (_, y) = y
  95
  96-- Typen von fst und snd?
  97
  98test1 ['a', _, _] = True
  99test1 _           = False
 100
 101-- Typ von test1?
 102
 103null :: [t] -> Bool
 104null [] = True
 105null _  = False
 106
 107head :: [t] -> t
 108head (x : _) = x
 109head [] = error "hahahahahah"
 110
 111tail :: [t] -> [t]
 112tail (_ : xs) = xs
 113
 114-- lambda-Ausdruecke
 115
 116l1 = \ x -> x + x
 117
 118l2 = (\ x -> x + x) 2
 119
 120l3 = (\ x -> x + x) (+ 3)
 121
 122l4 = \ x -> (\ y -> x + y)
 123
 124
 125-- Funktionen als Resultat
 126
 127const :: a -> b -> a
 128const c _x = c
 129
 130const' :: a -> (b -> a)
 131const' c = \ _x -> c
 132
 133-- Funktionen als Argument
 134
 135odds :: Int -> [Int]
 136odds n
 137    = map f [0..n-1]
 138      where
 139        f x = 2 * x + 1
 140
 141-- :t map ?
 142
 143odds' n
 144    = map (\ x -> 2 * x + 1) [0..n-1]
 145
 146-- :t odds' ?
 147
 148-- :t (1+)
 149-- :t (*2)
 150-- :t (== 1)
 151-- :t (1 <)

weiter

Die Quelle

FctDef.hs

Letzte Änderung: 24.11.2020
© Prof. Dr. Uwe Schmidt
Prof. Dr. Uwe Schmidt FH Wedel