[Haskell-beginners] Decomposing recursion

Stephen Tetley stephen.tetley at gmail.com
Fri Nov 5 16:54:52 EDT 2010


Hello Russ

Richard Kieburtz has a slightly smaller formulation of primitive
recursion on page 13 of these slides - the fold-like argument 'h'
doesn't appear to need a change of type:

http://programatica.cs.pdx.edu/P/Programatica%20Logic1.pdf

genPR :: (a -> Bool) -> (a -> a) -> c -> (a -> c -> c) -> a -> c
genPR p b g h x = if p x then g else h x (genPR p b g h (b x))


-- factorial function:
fact :: Integer -> Integer
fact = genPR (==0) (subtract 1) 1 (*)

Ignoring changes in argument order, the type of your recursionEngine
is one term and one type variable larger:

recursionEngine :: (a -> Bool) -> (a -> c) -> (b -> c -> c) -> (a ->
b) -> (a -> a)-> a -> c

Best wishes

Stephen


More information about the Beginners mailing list