]> gitweb.factorcode.org Git - factor.git/commitdiff
Rewriting llength in terms of folds
authorJames Cash <james.nvc@gmail.com>
Thu, 5 Jun 2008 18:32:43 +0000 (14:32 -0400)
committerJames Cash <james.nvc@gmail.com>
Thu, 5 Jun 2008 18:32:43 +0000 (14:32 -0400)
extra/lists/lists.factor

index a04a728ffce435a45eb7b562ba7fd163a2ced090..30a234214bda33431517c6fc66b3753f3da209f0 100644 (file)
@@ -51,12 +51,6 @@ M: object nil? drop f ;
 
 : lnth ( n list -- elt )
     swap [ cdr ] times car ;
-
-: (llength) ( list acc -- n )
-    over nil? [ nip ] [ [ cdr ] dip 1+ (llength) ] if ;
-
-: llength ( list -- n )
-    0 (llength) ;
     
 : (leach) ( list quot -- cdr quot )
     [ [ car ] dip call ] [ [ cdr ] dip ] 2bi ; inline
@@ -74,13 +68,13 @@ M: object nil? drop f ;
         [ [ cdr ] 2dip foldr ] [ nip [ car ] dip ] 3bi
         call
     ] if ; inline
+
+: llength ( list -- n )
+    0 [ drop 1+ ] foldl ;
     
 : lreverse ( list -- newlist )    
     nil [ swap cons ] foldl ;
     
-: lappend ( list1 list2 -- newlist )
-     ;
-    
 : seq>list ( seq -- list )    
     <reversed> nil [ swap cons ] reduce ;