module Numl where import Char ---------- sumProd b j k = b * j + k unfoldr p f x = if p x then [] else y : unfoldr p f r where (r,y) = f x unfoldl p f x = g x [] where g x a = if p x then a else g r (y:a) where (r,y) = f x ----- int b = foldl (sumProd b) 0 . map digitToInt str b = map intToDigit . unfoldl (==0) (`divMod` b) ----- int' b = foldr (flip (sumProd b)) 0 . reverse . map digitToInt str' b = map intToDigit . reverse . unfoldr (==0) (`divMod` b) ----- [(bin,unbin), (dec,undec), (hex,unhex)] = map (\b -> (str b, int b)) [2,10,16] ---------- data Expr a b = Lit a | Bop b (Expr a b) (Expr a b) | Var folde f g v (Lit a) = f a folde f g v (Bop o l r) = g o (folde f g v l) (folde f g v r) folde f g v Var = v data Aopr = Add | Sub | Mul folda a s m Add = a folda a s m Sub = s folda a s m Mul = m ----- sem = folda (+) (-) (*) syn = folda "+" "-" "*" infx exp = folde show (inpar . syn) "x" exp inpar o l r = concat ["(",l,o,r,")"] eval = flip (folde id sem) -- eval t n = folde id sema n t ----- [(!+),(!-),(!*)] = map Bop [Add,Sub,Mul] expr' = (Var !+ Lit 2) !* ((Lit 6 !- Var) !+ (Lit 3 !* Var)) -- 2x^2 + 10x + 12