Skip to content

Commit

Permalink
fixed point computation
Browse files Browse the repository at this point in the history
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 b1ca5b8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions topics/exercises/README.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions topics/exercises/fixed/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
all:
runhaskell fixedPoint.hs

clean:
rm -f *~ *.hi *.o
24 changes: 24 additions & 0 deletions topics/exercises/fixed/fixedPoint.hs
Original file line number Diff line number Diff line change
@@ -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.