{- * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. -} module FctDef where import Prelude hiding ( splitAt , recip , abs , signum , not , fst, snd , null, head, tail , const , even ) import qualified Prelude as P -- Funktionsdefinitionen isDigit :: Char -> Bool isDigit c = c >= '0' && c <= '9' even :: Integral a => a -> Bool even n = n `mod` 2 == 0 splitAt :: Int -> [a] -> ([a], [a]) splitAt n xs = (take n xs, drop n xs) recip :: Fractional a => a -> a recip x = 1 / x -- Verzweigungen abs n = if n >= 0 then n else -n signum n = if n < 0 then -1 else if n == 0 then 0 else 1 -- Waechter (guards) abs' n | n >= 0 = n | otherwise = -n signum' n | n < 0 = -1 | n == 0 = 0 | n > 0 = 1 -- Typen von abs, signum ? -- Mustervergleiche (pattern matching) not :: Bool -> Bool not x = if x then False else True not' False = True not' True = False not'' False = True not'' _ = False (&&&) :: Bool -> Bool -> Bool False &&& False = False False &&& True = False True &&& False = False True &&& True = True (.&&.) :: Bool -> Bool -> Bool True .&&. True = True _ .&&. _ = False (<&&>) :: Bool -> Bool -> Bool True <&&> b = b _ <&&> _ = False (&&&&) :: Bool -> Bool -> Bool a &&&& b = if a then b else False fst (x, _) = x snd (_, y) = y -- Typen von fst und snd? test1 ['a', _, _] = True test1 _ = False -- Typ von test1? null :: [t] -> Bool null [] = True null _ = False head :: [t] -> t head (x : _) = x head [] = error "hahahahahah" tail :: [t] -> [t] tail (_ : xs) = xs -- lambda-Ausdruecke l1 = \ x -> x + x l2 = (\ x -> x + x) 2 l3 = (\ x -> x + x) (2 + 3) l4 = \ x -> (\ y -> x + y) -- Funktionen als Resultat const :: a -> b -> a const c _x = c const' :: a -> (b -> a) const' c = \ _x -> c -- Funktionen als Argument odds :: Int -> [Int] odds n = map f [0..n-1] where f x = 2 * x + 1 -- :t map ? odds' n = map (\ x -> 2 * x + 1) [0..n-1] -- :t odds' ? -- :t (1+) -- :t (*2) -- :t (== 1) -- :t (1 <)