-- $Id: FoldR.hs,v 1.1 2013/02/20 15:27:57 leavens Exp leavens $
module FoldR where
import Prelude hiding (foldr, sum, product)
-- Some examples of full recursion over flat lists
sum :: Num a => [a] -> a
sum [] = 0
sum (x:xs) = x + sum xs
product :: Num a => [a] -> a
product [] = 1
product (x:xs) = x * product xs
-- After identifying the common parts and differing parts,
-- we pass the differing parts as parameters to the following abstraction.
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr op z [] = z
foldr op z (x:xs) = x `op` (foldr op z xs)
-- see FoldRTests for how to write sum and product using foldr