💾 Archived View for idiomdrottning.org › hs2brev captured on 2023-04-26 at 13:22:14. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-03-20)

-=-=-=-=-=-=-

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))