]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/rosetta-code/y-combinator/y-combinator.factor
factor: trim using lists
[factor.git] / extra / rosetta-code / y-combinator / y-combinator.factor
index c32cf7209f2829ea1ebafebf2fde193349487441..5e75afa7eb0b5ef0ab411384080d0475ffbcbc06 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 kernel math ;
 IN: rosetta-code.y-combinator
 
 ! http://rosettacode.org/wiki/Y_combinator
@@ -24,8 +24,20 @@ IN: rosetta-code.y-combinator
 : Y ( quot -- quot )
     '[ [ dup call call ] curry @ ] dup call ; inline
 
+! factorial sequence
 : almost-fac ( quot -- quot )
-    '[ dup zero? [ drop 1 ] [ dup 1 - _ call * ] if ] ;
+    '[ dup zero? [ drop 1 ] [ dup 1 - @ * ] if ] ;
 
+! fibonacci sequence
 : almost-fib ( quot -- quot )
-    '[ dup 2 >= [ 1 2 [ - _ call ] bi-curry@ bi + ] when ] ;
+    '[ 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
+    ] ;