Permalink
Browse files

fixed point computation

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@739 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information...
grammarware committed Jan 17, 2010
1 parent 9d7d7a6 commit b1ca5b80b0c7a8255b68133ef312455fec4e6ebf
Showing with 35 additions and 4 deletions.
  1. +6 −4 topics/exercises/README.txt
  2. +5 −0 topics/exercises/fixed/Makefile
  3. +24 −0 topics/exercises/fixed/fixedPoint.hs
@@ -1,5 +1,6 @@
b2/hutton - examples from Graham Hutton's "Programming in Haskell"
*/hutton - examples from Graham Hutton's "Programming in Haskell"
haskell1 - simple exercises solved in Haskell
fixed - fixed point computation by iteration in Haskell
while1 - a DCG parser for a fragment of While
while2 - a DCG parser for While
@@ -9,20 +10,21 @@ while5 - typed While in Prolog
while6 - While evaluation in Haskell (incomplete)
while7 - While evaluation in Haskell (complete, big step operational)
while8 - While execution in Haskell (complete, denotational)
ewhile - Extended While execution in Haskell (complete, denotational)
xml1 - a DCG parser for XML subset (elements only)
xml2 - a DCG parser for XML subset (elements and attributes)
b1 - abstract syntax for B in Prolog
b2 - parsing B in Haskell
b3 - folding over expressions for B in Haskell
nb1 - abstract syntax for NB in Prolog
nb2 - semantics and types for NB in Prolog
nb1 - abstract syntax for NB in Prolog
nb2 - semantics and types for NB in Prolog
nb3 - parsing and folding NB in Haskell
lambda1 - lambda calculus abstract and concrete syntax in Prolog
lambda2 - lambda caclulus with Church numbers
lambda3 - lambda calculus abstract syntax, free variables, substitution, evaluation
lambda4 - untyped lambda calculus with alpha conversion & fixed point combinator
lambda5 - typed lambda calculus with alpha conversion
lambda5 - typed lambda calculus with alpha conversion in Prolog
lambda6 - typed lambda calculus with alpha conversion & fixed point operator
@@ -0,0 +1,5 @@
all:
runhaskell fixedPoint.hs
clean:
rm -f *~ *.hi *.o
@@ -0,0 +1,24 @@
-- Explicitly recursive functorial function
fac x = if x == 0 then 1 else x * fac (x-1)
-- Recursion by means of fixed point combinator
fac' = fix f
where
-- Functional for factorial
f g x = if x == 0 then 1 else x * g (x-1)
-- Fixed point combinator based on fixed point condition
fix f = f (fix f)
-- Fixed point computation based on iteration
fac'' x = head (dropWhile (==Nothing) [ f'i n x | n <- [0..] ])
where
f g x = if x == 0 then Just 1 else maybe Nothing (Just . (x*)) (g (x-1))
f'i 0 = const Nothing
f'i n = f (f'i (n-1))
-- Test functions
main =
do
print $ fac 5 -- prints 120
print $ fac' 5 -- ditto
print $ fac'' 5 -- prints Just 120

0 comments on commit b1ca5b8

Please sign in to comment.