From b1ca5b80b0c7a8255b68133ef312455fec4e6ebf Mon Sep 17 00:00:00 2001 From: grammarware Date: Sun, 17 Jan 2010 22:50:16 +0000 Subject: [PATCH] fixed point computation git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@739 ab42f6e0-554d-0410-b580-99e487e6eeb2 --- topics/exercises/README.txt | 10 ++++++---- topics/exercises/fixed/Makefile | 5 +++++ topics/exercises/fixed/fixedPoint.hs | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 topics/exercises/fixed/Makefile create mode 100644 topics/exercises/fixed/fixedPoint.hs diff --git a/topics/exercises/README.txt b/topics/exercises/README.txt index 2e45e9df..b48988ca 100644 --- a/topics/exercises/README.txt +++ b/topics/exercises/README.txt @@ -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,6 +10,7 @@ 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) @@ -16,13 +18,13 @@ 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 diff --git a/topics/exercises/fixed/Makefile b/topics/exercises/fixed/Makefile new file mode 100644 index 00000000..1e32fe1a --- /dev/null +++ b/topics/exercises/fixed/Makefile @@ -0,0 +1,5 @@ +all: + runhaskell fixedPoint.hs + +clean: + rm -f *~ *.hi *.o diff --git a/topics/exercises/fixed/fixedPoint.hs b/topics/exercises/fixed/fixedPoint.hs new file mode 100644 index 00000000..72312432 --- /dev/null +++ b/topics/exercises/fixed/fixedPoint.hs @@ -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