Idiomdrottning’s homepage

Simple Haskell examples expressed in brev

map

Haskell:

petMap :: (a -> b) -> [a] -> [b]
petMap f [] = []
petMap f (x:xs) = f x : petMap f xs

Brev:

(define (pet-map f '()) '())
(define (pet-map f (x . xs)) (cons (f x) (pet-map f xs)))

Member

Haskell:

petMember :: (Eq a) => a -> [a] -> Bool
petMember x [] = False
petMember x (y:ys) | x==y = True
                | otherwise = petMember x ys

Brev:

(define (pet-member x (y . ys)) (pet-member x ys))
(define (pet-member x (x . ys)) #t)
(define (pet-member x '()) #f)

Fold1

Haskell:

fold1:: (a ->a -> a) -> a -> [a] -> a
fold1 f x [] = x
fold1 f x (y : ys) = fold1 f (f x y) ys

Brev:

(define (fold1 kons acc '()) acc)
(define (fold1 kons acc (x . xs))
  (fold1 kons (kons acc x) xs))