###### Navigationspfad
Sie sind hier: Haskell Code 02

# Haskell Code 02

Haskell Code aus der VL Lec2.hs — Haskell source code, 1 KB (1414 bytes)

## Dateiinhalt

```import Prelude hiding (Maybe(..),filter,map)

import Debug.Trace

type Solution = [Int]

test :: Solution
test = ([] :: [Int])

data Maybe a
= Nothing {}
| Just { fromJust :: a }
deriving Show

fromJust' :: Maybe a -> a
fromJust' (Just a) = a
fromJust' Nothing  = error "fromJust' applied to Nothing"

isJust :: Maybe a -> Bool
isJust Nothing = False
isJust Just{}  = True

filter :: Show a => (a -> Bool) -> [a] -> [a]
filter p l = trace ("filter invoked with list l = " ++ show l) \$ loop l where
loop l = case l of
[] -> []
(a : as) | p a -> a : loop as
| otherwise -> loop as

map :: (a -> b) -> [a] -> [b]
map f l = foldr op [] l
where -- op :: a -> [b] -> [b]
op a bs = f a : bs
-- foldr op v [a1,a2,..,an] = a1 `op` (a2 `op` ... `op` (an `op v))
--   = f a1 : (f a2 : ... (f an : []))
--   = [f a1, f a2, ..., f an]

data Complex = Complex Float Float
deriving (Eq)

instance Num Complex where
(Complex r1 i1) + (Complex r2 i2) = Complex (r1 + r2) (i1 + i2)

instance Show Complex where
show (Complex r i) = show r ++ " + " ++ show i ++ "j"

approxEq :: Float -> Float -> Bool
approxEq x y = abs (x - y) < 0.1

newtype MyFloat = MyFloat { myFloat :: Float }

instance Eq MyFloat where
MyFloat x == MyFloat y = approxEq x y

test1 = MyFloat 0.35 == MyFloat 0.33

newtype WeekDay = WeekDay Int
deriving (Eq, Show, Num)

newtype WorkDays = WorkDays [WeekDay]

```

Artikelaktionen