]> gitweb.factorcode.org Git - factor.git/commitdiff
rosetta-code.y-combinator: add ackerman functions to demonstrate several inputs
authorJon Harper <jon.harper87@gmail.com>
Sat, 4 Aug 2012 20:31:10 +0000 (22:31 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 5 Aug 2012 00:32:33 +0000 (17:32 -0700)
extra/rosetta-code/y-combinator/y-combinator-tests.factor
extra/rosetta-code/y-combinator/y-combinator.factor

index e76e18eee9feddd6f3318805628a22d04d5fe060..08feb197c40a4e6967b75a3b4dd5485ff9180966 100644 (file)
@@ -3,4 +3,5 @@ IN: rosetta-code.y-combinator
 
 [ 120 ] [ 5 [ almost-fac ] Y call ] unit-test
 [ 8 ]   [ 6 [ almost-fib ] Y call ] unit-test
+[ 61 ]  [ 3 3 [ almost-ack ] Y call ] unit-test
 
index 96dc2d3d9acd3573844641868ddf9787b2d2c51d..0eb68c6aaebbae844847ea03d78e36002a8a90f1 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (c) 2012 Anonymous
 ! See http://factorcode.org/license.txt for BSD license.
-USING: fry kernel math ;
+USING: combinators fry kernel locals math ;
 IN: rosetta-code.y-combinator
 
 ! http://rosettacode.org/wiki/Y_combinator
@@ -31,3 +31,13 @@ IN: rosetta-code.y-combinator
 ! fibonacci sequence
 : almost-fib ( quot -- quot )
     '[ dup 2 >= [ 1 2 [ - @ ] bi-curry@ bi + ] when ] ;
+
+! Ackermann–PĂ©ter function
+:: almost-ack ( quot -- quot )
+    [
+        {
+          { [ over zero? ] [ nip 1 + ] }
+          { [ dup zero? ] [ [ 1 - ] [ drop 1 ] bi* quot call ] }
+          [ [ drop 1 - ] [ 1 - quot call ] 2bi quot call ]
+        } cond
+    ] ;