Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #20488

Factor vs. CL

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Factor vs. CL
Date 2013-03-09 07:29 +0000
Organization A noiseless patient Spider
Message-ID <kheod1$nc3$1@dont-email.me> (permalink)

Show all headers | View raw


> For a long time, I preferred to write 
> 
>  (do ((l list (cdr l)) 
>       (result '() (cons (f (car l)) result))) 
>      ((null l) (nreverse result))) 
> 
> But though this has the usefulness of paralleling Scheme's tail recursive 
> 
>  (let foo ((l list) 
>            (result '())) 
>    (if (null? l) 
>        (reverse! result) 
>        (foo (cdr l) (cons (f (car l)) result)))) 
> 
> in the end, I couldn't justify to new users why they shouldn't just write 
> the more obvious: 
> 
>  (loop for item in list 
>        collect (f item)) 
> 
> And sure, you could also write 
> 
>  (mapcar #'f list) 

Factor:

{ } 9 iota [ sq suffix ] each .
{ 0 1 4 9 16 25 36 49 64 }

> 
> but the point is that if you want to change the accumulation from collect 
> to max or min, you can't make a trivial edit to fix that, whereas with the 
> loop one you can do 
> 
>  (loop for item in list 
>        maximize (f item))

0 9 iota [ sq max ] each .
64


> The ability in loop to do even complex things like: 
> 
>  (loop for x in '(1 2 3 4 5 6 7) 
>     when (evenp x) 
>      collect x into evens 
>     else 
>      collect x into odds 
>     finally 
>      (return (values evens odds))) 
>  => (2 4 6), (1 3 5 7) 

Factor:

8 iota [ even? ] partition

--- Data stack:
V{ 0 2 4 6 }
V{ 1 3 5 7 }


> 
> is considerably easier and clearer than some other alternatives. 
> 
> Also, being able to vary the collection is handy.  Consider: 
> 
>  (loop for x in '(a b (c d) e f) 
>        when (atom x) 
>         collect x 
>        else 
>         append x) 
>  => (A B C D E F)

{ } { "a" "b" { "c" "d" } "e" "f" }
[ dup string? [ suffix ] [ append ] if ] each .

{ "a" "b" "c" "d" "e" "f" }

Back to comp.lang.forth | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Factor vs. CL "WJ" <w_a_x_man@yahoo.com> - 2013-03-09 07:29 +0000
  Re: Factor vs. CL "WJ" <w_a_x_man@yahoo.com> - 2013-06-14 15:07 +0000

csiph-web