-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@739 ab42f6e0-554d-0410-b580-99e487e6eeb2
- Loading branch information
1 parent
9d7d7a6
commit b1ca5b8
Showing
3 changed files
with
35 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
all: | ||
runhaskell fixedPoint.hs | ||
|
||
clean: | ||
rm -f *~ *.hi *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |