From: defn noob
Subject: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <4b530b58-96d4-4749-960f-3a5330448626@m36g2000hse.googlegroups.com>
I am trying to learn lisp macros and I generally find
listcomprehensions to be elegant and they dont exist in lisp.
This has to be a macro right? Cant be a function?

But how would I get started when it needs the arguments inside its
name?

In Python:
>>> [x**2 for x in [12,31,2,33] if x > 20]
[961, 1089]
>>>


[expr loop &optional cond]

Could I perhaps make a macro [ that triggers another macro?

(defmacro [ ()
    ((let listcomp (read until next ])
      (filter (cond) (mapcar expr loop)))

From: Thomas A. Russ
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <ymiwsi8u0gv.fsf@blackcat.isi.edu>
defn noob <············@yahoo.se> writes:

> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?

Not sure exactly what a listcomphrehension is.

But looking below, you need to be able to separate the syntactic
concerns from the actual function or semantics of what you want to do.

For new control structure, you generally want or need to use macros in
lisp.  Once you have the construct that you want, using standard lisp
syntax, then you can worry about how to change the surface syntax.  That
will generally involve modifying the readtable and using reader macros.

> But how would I get started when it needs the arguments inside its
> name?
> 
> In Python:
> >>> [x**2 for x in [12,31,2,33] if x > 20]
> [961, 1089]
> >>>

The standar way to do this in lisp would involve using one of the
built-in macros, namely the loop or dolist construct:

(loop for x in '(12 31 2 33)
      when (> x 20) collect (expt x 2))

(let ((result nil))
  (dolist (x '(12 31 2 33) (nreverse result))
    (when (> x 20) (push (expt x 2) result))))

Or alternatively, you could use one of the mapping constructs, but that
is a bit trickier to manage the splicing since you don't necessarily
want all values.

(mapcan #'(lambda (x) (when (> x 20) (list (expt x 2))))
        '(12 31 2 33))


So, it doesn't seem like you really need to add much of anything to the
language.

> [expr loop &optional cond]

So, you could perhaps write a macro that takes the arguments that you
specify.  It might be more convenient to re-arrange the order perhaps,
to take better advantage of the lambda list argument types.

Remember not to get too hung up on the syntax of the language.  You want
to have an equivalent construct, even if it doesn't look the same.
Otherwise why bother changing languages?

> Could I perhaps make a macro [ that triggers another macro?
> 
> (defmacro [ ()
>     ((let listcomp (read until next ])
>       (filter (cond) (mapcar expr loop)))



-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Benjamin Teuber
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <7fa79503-6d89-4dc0-bcac-12e327de7b66@m44g2000hsc.googlegroups.com>
> (defmacro [ ()
>     ((let listcomp (read until next ])
>       (filter (cond) (mapcar expr loop)))

To do something like this, the thing you're looking for are reader
macros. But beware, they are already quite advanced and far from
normal macros...
So maybe you should simplify your task for now and use a lispy syntax
for your comprehensions instead of python syntax:

(comprehend-or-whatever-name-you-choose (* x x)
  (for x '(12 31 2 33))
  (if (> x 20)))

But even this is quite difficult as there are a lot of cases to catch
(with/without if/unless etc.), so you might as well find different
quests for now =)

I strongly recommend Graham's "On Lisp" (free download available on
his website) for learning macros.

Cheers,
Benjamin
From: Rainer Joswig
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <joswig-68093F.09510523082008@news-europe.giganews.com>
In article 
<····································@m44g2000hsc.googlegroups.com>,
 Benjamin Teuber <······@web.de> wrote:

> > (defmacro [ ()
> > � � ((let listcomp (read until next ])
> > � � � (filter (cond) (mapcar expr loop)))
> 
> To do something like this, the thing you're looking for are reader
> macros. But beware, they are already quite advanced and far from
> normal macros...
> So maybe you should simplify your task for now and use a lispy syntax
> for your comprehensions instead of python syntax:
> 
> (comprehend-or-whatever-name-you-choose (* x x)
>   (for x '(12 31 2 33))
>   (if (> x 20)))
> 
> But even this is quite difficult as there are a lot of cases to catch
> (with/without if/unless etc.), so you might as well find different
> quests for now =)
> 
> I strongly recommend Graham's "On Lisp" (free download available on
> his website) for learning macros.
> 
> Cheers,
> Benjamin

Here is a very simple implementation (that I didn't write):

(defmacro comp ((e &rest qs) l2)
  (if (null qs)
    `(cons ,e ,l2)
    (let ((q1 (car qs))
          (q (cdr qs)))
      (if (not (eq (cadr q1) '<-))
        `(if ,q1
           (comp (,e . ,q) ,l2) ,l2)
        (let ((v (car q1))
              (l1 (third q1))
              (h (gensym "H-"))
              (us (gensym "US-"))
              (us1 (gensym "US1-")))
          `(labels ((,h (,us)
                      (if (null ,us)
                        ,l2
                        (let ((,v (car ,us))
                              (,us1 (cdr ,us)))
                          (comp (,e . ,q) (,h ,us1))))))
             (,h ,l1)))))))

(defun open-bracket (stream ch)
  (do ((l nil)
       (c (read stream t nil t) (read stream t nil t)))
      ((eq c '|]|)
       `(comp ,(reverse l) ()))
    (push c l)))

(defun closing-bracket (stream ch)
  '|]|)

(set-macro-character #\[ #'open-bracket)
(set-macro-character #\] #'closing-bracket)



Then you can write code like this:

(let ((xs '(1 2 3 4 5 6 7 8)))
  [x (x <- xs) (oddp x)])

or this (with an ugly append, I know):

(defun qsort (ax)
  (and ax
       (let ((a (car ax))
             (x (cdr ax)))
         (append (qsort [y (y <- x) (< y a)])
                 (list a)
                 (qsort [y (y <- x) (>= y a)])))))



Now, homework: how does the above macro work?

-- 
http://lispm.dyndns.org/
From: David Golden
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <mmHrk.26807$j7.472728@news.indigo.ie>
defn noob wrote:

> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?
> 

You might want to look at the COLLECT macro
http://user.it.uu.se/~svenolof/Collect/

>  [x**2 for x in [12,31,2,33] if x > 20]
> [961, 1089]


(collect list ((expt x 2)) (in x '(12 31 2 33)) (when (> x 20)))
=> (961 1089)

vectors work too...

(collect vector ((expt x 2)) (in x #(12 31 2 33)) (when (> x 20)))
=> #(961 1089)
From: William James
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <016e911c-7b40-42fa-a3c6-98886af4d758@m3g2000hsc.googlegroups.com>
On Aug 22, 5:47 pm, David Golden <············@oceanfree.net> wrote:
> defn noob wrote:
> > I am trying to learn lisp macros and I generally find
> > listcomprehensions to be elegant and they dont exist in lisp.
> > This has to be a macro right? Cant be a function?
>
> You might want to look at the COLLECT macrohttp://user.it.uu.se/~svenolof/Collect/
>
> >  [x**2 for x in [12,31,2,33] if x > 20]
> > [961, 1089]
>
> (collect list ((expt x 2)) (in x '(12 31 2 33)) (when (> x 20)))
> => (961 1089)

Ruby:
[12,31,2,33].select{|n| n>20}.map{|n| n**2}
    ==>[961, 1089]
From: Pascal J. Bourguignon
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <7c4p4gh3tw.fsf@pbourguignon.anevia.com>
William James <·········@yahoo.com> writes:

> On Aug 22, 5:47 pm, David Golden <············@oceanfree.net> wrote:
>> defn noob wrote:
>> > I am trying to learn lisp macros and I generally find
>> > listcomprehensions to be elegant and they dont exist in lisp.
>> > This has to be a macro right? Cant be a function?
>>
>> You might want to look at the COLLECT macrohttp://user.it.uu.se/~svenolof/Collect/
>>
>> >  [x**2 for x in [12,31,2,33] if x > 20]
>> > [961, 1089]
>>
>> (collect list ((expt x 2)) (in x '(12 31 2 33)) (when (> x 20)))
>> => (961 1089)
>
> Ruby:
> [12,31,2,33].select{|n| n>20}.map{|n| n**2}
>     ==>[961, 1089]


Ruby:
irb(main):009:0> x=33331 ; if (x == ( ( x / 3 ) * 3 )) then :ok else :bad end
:bad

Lisp:
C/USER[1]> (let ((x 33331)) (if (= x (* (/ x 3) 3)) :ok :bad))
:OK

Come back when your language know how to compute.

-- 
__Pascal Bourguignon__
From: André Thieme
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <gavj2h$kmq$1@registered.motzarella.org>
William James schrieb:
> On Aug 22, 5:47 pm, David Golden <············@oceanfree.net> wrote:
>> defn noob wrote:
>>> I am trying to learn lisp macros and I generally find
>>> listcomprehensions to be elegant and they dont exist in lisp.
>>> This has to be a macro right? Cant be a function?
>> You might want to look at the COLLECT macrohttp://user.it.uu.se/~svenolof/Collect/
>>
>>>  [x**2 for x in [12,31,2,33] if x > 20]
>>> [961, 1089]
>> (collect list ((expt x 2)) (in x '(12 31 2 33)) (when (> x 20)))
>> => (961 1089)
> 
> Ruby:
> [12,31,2,33].select{|n| n>20}.map{|n| n**2}
>     ==>[961, 1089]

Yes, also in Ruby one can have a nice solution as you showed.
But still, the Lisp solution shown is the preferrable one, because it
is the shorter program. Not character wise. But that doesn�t count.
If the function is called slct or select or if it had a 14-char name,
it won�t matter. It�s all just one point in the program, and therewith
a complexity unit.

The Lisp code has these:
collect, list, (), expt, x, 2, in, x, ', (), 12, 31, 2, 33, when, >, x, 20
That gives us 18 points.

But your Ruby code has 19:
[], 12, 31, 2, 33, select, {}, ||, n, n, >, 20, map, {}, ||, n, n, **, 2

Lisp wins, although this time only by a small margin, compared to your
other examples which often be solved only in a noticable more complex
way than in Lisp.

Please don�t forget that advances in programming languages
should help us to remove complexity and make things easier.


If you wanted you could do exactly the same thing in Lisp as in
[12, 31, 2, 33].select{|n| n>20}.map{|n| n**2}

like this:
(mapcar (lambda (n) (expt n 2))
         (select '(12 31 2 33)
                 (lambda (n) (< n 20))))

Because Lisp uses lengthier names the solution looks longer when counting
chars. But algorithmically it is the same.
If you had to write tons of such small functions you would introduce a
macro and do:
(map [�2] (select '(12 31 2 33) [< _ 20]))

Here we renamed functions to reflect the preferrence of short names.
We have the function �2 which squares its argument. We curry away the
variable and introduce anonymous currying for the lambda to select.
Think about how you can add implicit currying to Ruby without touching
the C code which it is implemented in, but instead doing it with a pure
Ruby solution.


Andr�
-- 
From: Tobias C. Rittweiler
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <87skswpqnf.fsf@freebits.de>
defn noob <············@yahoo.se> writes:

> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?

I'd recommend to abstain from trying to judge what's missing and what
should be fixed on the language until you passed "noob" status.

> In Python:
>   [x**2 for x in [12,31,2,33] if x > 20]

Take a look at LOOP.
 

> Could I perhaps make a macro [ that triggers another macro?
>
> (defmacro [ ()
>     ((let listcomp (read until next ])
>       (filter (cond) (mapcar expr loop)))

This is syntactically so wrong that I suggest to spend some time
actually learning the language before trying to extend it in non-trivial
ways. I can recommend the book Practical Common Lisp which is even
available online for free.

  -T.
From: Frank Buss
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <lxbwkg8afbfm.1o1mj1sxzbroi.dlg@40tude.net>
defn noob wrote:

> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?

Yes, it has to be a macro, if you need it fast and if you want your own
syntax, like used for list comprehensions in other languages like Haskell.
A nice example is this one, which was presented at ILC2007:

http://www.iro.umontreal.ca/~latendre/publications/listCompFinal.pdf

Maybe you should take a look at Liskell, too. Liskell is a new Lisp syntax
frontend for Haskell, so I assume it implements list comprehensions and it
might help to find a good Lisp-like syntax.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Grant Rettke
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <a131192a-350a-4baf-a4be-06526b45ac50@l42g2000hsc.googlegroups.com>
On Aug 22, 5:16 pm, defn noob <············@yahoo.se> wrote:
> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?

For inspiration here is one option for list comprehensions in Scheme:
http://srfi.schemers.org/srfi-42/srfi-42.html

Or PLT Scheme in particular: http://docs.plt-scheme.org/reference/for.html
From: Marco Antoniotti
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <6410dbf9-a288-4e70-8589-9dac442b9ff7@m73g2000hsh.googlegroups.com>
On Aug 23, 9:50 am, Grant Rettke <·······@gmail.com> wrote:
> On Aug 22, 5:16 pm, defn noob <············@yahoo.se> wrote:
>
> > I am trying to learn lisp macros and I generally find
> > listcomprehensions to be elegant and they dont exist in lisp.
> > This has to be a macro right? Cant be a function?
>
> For inspiration here is one option for list comprehensions in Scheme:http://srfi.schemers.org/srfi-42/srfi-42.html
>
> Or PLT Scheme in particular:http://docs.plt-scheme.org/reference/for.html

Vintage 2001.  Inspiration: SETL

http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/e84f21a6fdabc3c5/f1461d383f8f7e32?lnk=gst&q=SETL#f1461d383f8f7e32

ping me for the code. :)

Cheers
--
Marco
From: Rainer Joswig
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <joswig-3866C4.17291923082008@news-europe.giganews.com>
In article 
<····································@m73g2000hsh.googlegroups.com>,
 Marco Antoniotti <·······@gmail.com> wrote:

> On Aug 23, 9:50�am, Grant Rettke <·······@gmail.com> wrote:
> > On Aug 22, 5:16�pm, defn noob <············@yahoo.se> wrote:
> >
> > > I am trying to learn lisp macros and I generally find
> > > listcomprehensions to be elegant and they dont exist in lisp.
> > > This has to be a macro right? Cant be a function?
> >
> > For inspiration here is one option for list comprehensions in Scheme:http://srfi.schemers.org/srfi-42/srfi-42.html
> >
> > Or PLT Scheme in particular:http://docs.plt-scheme.org/reference/for.html
> 
> Vintage 2001.  Inspiration: SETL
> 
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/e84f21a6fdabc3c5/f1461d383f8f7e32?lnk=gst&q=SETL#f1461d383f8f7e32
> 
> ping me for the code. :)
> 
> Cheers
> --
> Marco


How about NESL?

http://www-2.cs.cmu.edu/~scandal/nesl.html

http://www.cs.cmu.edu/~scandal/nesl/nesl3.1.html#lightweight

RJMBP:neslseq joswig$ cmucl
CMU Common Lisp Snapshot 2008-07 (19E), running on RJMBP.local
With core: /usr/local/lib/cmucl/lib/lisp.core
Dumped on: Sat, 2008-06-28 13:31:54+02:00 on macmini
See <http://www.cons.org/cmucl/> for support information.
Loaded subsystems:
    Python 1.1, target Intel x86
    CLOS based on Gerd's PCL 2004/04/14 03:32:47
    CLX X Library Telent CLX 0.7.3 + CMUCL mods, based on MIT R5.02
    Hemlock 3.5

...

* (nesl)

% NESL version Sequential 1.0 (June 18, 1995) %

% Use (nesl) when an error aborts you out of the interpreter. %
% Type help; for a list of the top level commands. %

[Nesl] {a + 1: a in [2, 3, 4]};
it = [3, 4, 5] : [int]

[Nesl]

-- 
http://lispm.dyndns.org/
From: Marco Antoniotti
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <7a3e673e-20cf-4941-80c2-36f9e5579dbf@34g2000hsh.googlegroups.com>
On Aug 23, 11:29 am, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@m73g2000hsh.googlegroups.com>,
>  Marco Antoniotti <·······@gmail.com> wrote:
>
>
>
> > On Aug 23, 9:50 am, Grant Rettke <·······@gmail.com> wrote:
> > > On Aug 22, 5:16 pm, defn noob <············@yahoo.se> wrote:
>
> > > > I am trying to learn lisp macros and I generally find
> > > > listcomprehensions to be elegant and they dont exist in lisp.
> > > > This has to be a macro right? Cant be a function?
>
> > > For inspiration here is one option for list comprehensions in Scheme:http://srfi.schemers.org/srfi-42/srfi-42.html
>
> > > Or PLT Scheme in particular:http://docs.plt-scheme.org/reference/for.html
>
> > Vintage 2001.  Inspiration: SETL
>
> >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/e84f2...
>
> > ping me for the code. :)
>
> > Cheers
> > --
> > Marco
>
> How about NESL?
>
> http://www-2.cs.cmu.edu/~scandal/nesl.html
>
> http://www.cs.cmu.edu/~scandal/nesl/nesl3.1.html#lightweight
>
> RJMBP:neslseq joswig$ cmucl
> CMU Common Lisp Snapshot 2008-07 (19E), running on RJMBP.local
> With core: /usr/local/lib/cmucl/lib/lisp.core
> Dumped on: Sat, 2008-06-28 13:31:54+02:00 on macmini
> See <http://www.cons.org/cmucl/> for support information.
> Loaded subsystems:
>     Python 1.1, target Intel x86
>     CLOS based on Gerd's PCL 2004/04/14 03:32:47
>     CLX X Library Telent CLX 0.7.3 + CMUCL mods, based on MIT R5.02
>     Hemlock 3.5
>
> ...
>
> * (nesl)
>
> % NESL version Sequential 1.0 (June 18, 1995) %
>
> % Use (nesl) when an error aborts you out of the interpreter. %
> % Type help; for a list of the top level commands. %
>
> [Nesl] {a + 1: a in [2, 3, 4]};
> it = [3, 4, 5] : [int]
>
> [Nesl]
>
> --http://lispm.dyndns.org/

SETL is much older.  As an aside the first certified Ada compiler (for
Ada 83) was written in SETL.

My little library can be loaded in a running CL, and can be used
without the readtable hacks.

Cheers
--
Marco
From: verec
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <48afc567$0$628$5a6aecb4@news.aaisp.net.uk>
On 2008-08-22 23:16:27 +0100, defn noob <············@yahoo.se> said:

> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?
> 
> But how would I get started when it needs the arguments inside its
> name?
> 
> In Python:
>>>> [x**2 for x in [12,31,2,33] if x > 20]
> [961, 1089]
>>>> 
> 
> 
> [expr loop &optional cond]
> 
> Could I perhaps make a macro [ that triggers another macro?
> 
> (defmacro [ ()
>     ((let listcomp (read until next ])
>       (filter (cond) (mapcar expr loop)))

Before reaching for lisp macro guru status, maybe you
might want to consider a plain old function?

(defun retain (pred transform list)
  (mapcan
   #'(lambda(x)
       (when (funcall pred x)
         (list (funcall transform x))))
    list))

CL-USER 57 > (retain #'(lambda(x) (> x 20)) #'(lambda(x) (* x x)) '(12 
31 2 33))
(961 1089)

Macro-ising and getting rid of the five letter repeated noise left as 
an exrecise :-)
--
JFB
From: DeverLite
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <e3edef66-1dd8-4ae0-baa1-f9c1e0f31312@r66g2000hsg.googlegroups.com>
On Aug 22, 5:16 pm, defn noob <············@yahoo.se> wrote:
> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?
>
> But how would I get started when it needs the arguments inside its
> name?
>
> In Python:
>
> >>> [x**2 for x in [12,31,2,33] if x > 20]
> [961, 1089]
>
> [expr loop &optional cond]
>
> Could I perhaps make a macro [ that triggers another macro?
>
> (defmacro [ ()
>     ((let listcomp (read until next ])
>       (filter (cond) (mapcar expr loop)))

Nobody has yet mentioned the Lisp series package: http://series.sourceforge.net/.

I think this does what you're looking for... and a little bit more.
But I agree with the other comments: get more comfortable with lisp's
syntax and learn how to think in lisp before trying to make macro's
and change the syntax around.
From: namekuseijin
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <01c2e40a-35bd-4302-be1d-548b370902bf@j22g2000hsf.googlegroups.com>
On Aug 22, 7:16 pm, defn noob <············@yahoo.se> wrote:
> I am trying to learn lisp macros and I generally find
> listcomprehensions to be elegant and they dont exist in lisp.
> This has to be a macro right? Cant be a function?
>
> But how would I get started when it needs the arguments inside its
> name?
>
> In Python:
>
> >>> [x**2 for x in [12,31,2,33] if x > 20]
> [961, 1089]

I like my own Scheme idiom

(define (sqr n) (* n n))

((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons)

; could be macroized for getting rid of (lambda (x) (> x 20)) but is
pretty good and flexible already

;;;; given:

; fold list ls with initial result r and reducer f
(define (fold ls r f)
  (if (null? ls) r
      (fold (cdr ls) (f (car ls) r) f)))

; returns an iterator from from to to, optionally incremented by step
(define (range from to . step)
  (let* ((step (if (null? step) 1 (car step)))
         (inc (if (> from to)
                 (lambda (n) (- n step))
                 (lambda (n) (+ n step))))
        (> (if (> from to) < >)))
    (lambda (init f)
      (let lp ((i from) (r init))
        (if (> i to) r
            (lp (inc i) (f i r)))))))

; very useful: filtered operation!  Collects result of op if ?
(define (filtered ? op) (lambda (i r) (if (? i) (op i r) r)))

; fantastic idiom for comprehensions in Scheme!
; final is the final form of the collected items, like, all squared!
; in-range can be either a list or a (range from to) call
; given is the condition items in the list have to obey.  Optional
(define (all final in-range . given)
  (lambda (ini doit)
    (let ((reducer (if (null? given)
                       (lambda (i r) (doit (final i) r))
                       (lambda (i r)
                         ((filtered (car given) (lambda (i r) (doit
(final i) r)))
                          i r)))))
      (if (procedure? in-range)
          (in-range ini reducer)
          (fold in-range ini reducer)))))

; exs:

((all (lambda (i) (* 2 i)) (range 1 10) odd?) '() cons)
;=> (18 14 10 6 2)

; non-filtered
((all sqr (range 1 10)) '() cons)
;=> (100 81 64 49 36 25 16 9 4 1)

; reverse
((all sqr (range 10 1)) '() cons)
;=> (1 4 9 16 25 36 49 64 81 100)

; prints 1 9 25
((all sqr '(1 2 3 4 5) odd?) #f (lambda (i r) (display i)(newline)r))
From: ······@corporate-world.lisp.de
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <f90e5fc7-abd2-413b-b2c4-a15d9efc9ce1@x41g2000hsb.googlegroups.com>
On Sep 16, 8:57 pm, namekuseijin <············@gmail.com> wrote:
> On Aug 22, 7:16 pm, defn noob <············@yahoo.se> wrote:
>
> > I am trying to learn lisp macros and I generally find
> > listcomprehensions to be elegant and they dont exist in lisp.
> > This has to be a macro right? Cant be a function?
>
> > But how would I get started when it needs the arguments inside its
> > name?
>
> > In Python:
>
> > >>> [x**2 for x in [12,31,2,33] if x > 20]
> > [961, 1089]
>
> I like my own Scheme idiom
>
> (define (sqr n) (* n n))
>
> ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons)
>
> ; could be macroized for getting rid of (lambda (x) (> x 20)) but is
> pretty good and flexible already
>
> ;;;; given:
>
> ; fold list ls with initial result r and reducer f
> (define (fold ls r f)
>   (if (null? ls) r
>       (fold (cdr ls) (f (car ls) r) f)))
>
> ; returns an iterator from from to to, optionally incremented by step
> (define (range from to . step)
>   (let* ((step (if (null? step) 1 (car step)))
>          (inc (if (> from to)
>                  (lambda (n) (- n step))
>                  (lambda (n) (+ n step))))
>         (> (if (> from to) < >)))
>     (lambda (init f)
>       (let lp ((i from) (r init))
>         (if (> i to) r
>             (lp (inc i) (f i r)))))))
>
> ; very useful: filtered operation!  Collects result of op if ?
> (define (filtered ? op) (lambda (i r) (if (? i) (op i r) r)))
>
> ; fantastic idiom for comprehensions in Scheme!
> ; final is the final form of the collected items, like, all squared!
> ; in-range can be either a list or a (range from to) call
> ; given is the condition items in the list have to obey.  Optional
> (define (all final in-range . given)
>   (lambda (ini doit)
>     (let ((reducer (if (null? given)
>                        (lambda (i r) (doit (final i) r))
>                        (lambda (i r)
>                          ((filtered (car given) (lambda (i r) (doit
> (final i) r)))
>                           i r)))))
>       (if (procedure? in-range)
>           (in-range ini reducer)
>           (fold in-range ini reducer)))))
>
> ; exs:
>
> ((all (lambda (i) (* 2 i)) (range 1 10) odd?) '() cons)
> ;=> (18 14 10 6 2)
>
> ; non-filtered
> ((all sqr (range 1 10)) '() cons)
> ;=> (100 81 64 49 36 25 16 9 4 1)
>
> ; reverse
> ((all sqr (range 10 1)) '() cons)
> ;=> (1 4 9 16 25 36 49 64 81 100)
>
> ; prints 1 9 25
> ((all sqr '(1 2 3 4 5) odd?) #f (lambda (i r) (display i)(newline)r))

That's nicely obfuscated write-only code implementing a strange custom
iteration construct
with the results in reverse order!
From: namekuseijin
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <718bbed8-79be-47f1-acb5-ea6f97a954d8@d45g2000hsc.googlegroups.com>
On Sep 16, 4:19 pm, ·······@corporate-world.lisp.de" <······@corporate-
world.lisp.de> wrote:
> On Sep 16, 8:57 pm, namekuseijin <············@gmail.com> wrote:
>
>
>
> > On Aug 22, 7:16 pm, defn noob <············@yahoo.se> wrote:
>
> > > I am trying to learn lisp macros and I generally find
> > > listcomprehensions to be elegant and they dont exist in lisp.
> > > This has to be a macro right? Cant be a function?
>
> > > But how would I get started when it needs the arguments inside its
> > > name?
>
> > > In Python:
>
> > > >>> [x**2 for x in [12,31,2,33] if x > 20]
> > > [961, 1089]
>
> > I like my own Scheme idiom
>
> > (define (sqr n) (* n n))
>
> > ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons)
>
> > ; could be macroized for getting rid of (lambda (x) (> x 20)) but is
> > pretty good and flexible already
>
> > ;;;; given:
>
> > ; fold list ls with initial result r and reducer f
> > (define (fold ls r f)
> >   (if (null? ls) r
> >       (fold (cdr ls) (f (car ls) r) f)))
>
> > ; returns an iterator from from to to, optionally incremented by step
> > (define (range from to . step)
> >   (let* ((step (if (null? step) 1 (car step)))
> >          (inc (if (> from to)
> >                  (lambda (n) (- n step))
> >                  (lambda (n) (+ n step))))
> >         (> (if (> from to) < >)))
> >     (lambda (init f)
> >       (let lp ((i from) (r init))
> >         (if (> i to) r
> >             (lp (inc i) (f i r)))))))
>
> > ; very useful: filtered operation!  Collects result of op if ?
> > (define (filtered ? op) (lambda (i r) (if (? i) (op i r) r)))
>
> > ; fantastic idiom for comprehensions in Scheme!
> > ; final is the final form of the collected items, like, all squared!
> > ; in-range can be either a list or a (range from to) call
> > ; given is the condition items in the list have to obey.  Optional
> > (define (all final in-range . given)
> >   (lambda (ini doit)
> >     (let ((reducer (if (null? given)
> >                        (lambda (i r) (doit (final i) r))
> >                        (lambda (i r)
> >                          ((filtered (car given) (lambda (i r) (doit
> > (final i) r)))
> >                           i r)))))
> >       (if (procedure? in-range)
> >           (in-range ini reducer)
> >           (fold in-range ini reducer)))))
>
> > ; exs:
>
> > ((all (lambda (i) (* 2 i)) (range 1 10) odd?) '() cons)
> > ;=> (18 14 10 6 2)
>
> > ; non-filtered
> > ((all sqr (range 1 10)) '() cons)
> > ;=> (100 81 64 49 36 25 16 9 4 1)
>
> > ; reverse
> > ((all sqr (range 10 1)) '() cons)
> > ;=> (1 4 9 16 25 36 49 64 81 100)
>
> > ; prints 1 9 25
> > ((all sqr '(1 2 3 4 5) odd?) #f (lambda (i r) (display i)(newline)r))
>
> That's nicely obfuscated write-only code implementing a strange custom
> iteration construct
> with the results in reverse order!

You can always do:
(reverse ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons))

and it can be used in a multitude of ways, not simply to return a
list:
((all sqr '(12 31 2 33) (lambda (x) (> x 20))) 0 +)
((all sqr '(12 31 2 33) (lambda (x) (and (> x 20) (odd? x))) 1 *)

Order is not important in many applications.

I don't think it's write-only, nor obfuscated, but then again, I'm the
father of the child. :P  I'm used to writing these from scratch when
I'm not with my personal libs around, so I guess they're not that
complicated...

The important thing to have in mind is that I wanted a fold without
lists, then I got range, which returns an iterator for the range.
Then all either uses range or fold depending on the in-range
ardument.  all itself returns the iterator, so that you may apply the
operation and initializer you want.  That's applicative programming at
its best, I guess...
From: ······@corporate-world.lisp.de
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <d14ca9c0-dda6-4f6f-a7f7-8b61e116809c@x41g2000hsb.googlegroups.com>
On 16 Sep., 21:38, namekuseijin <············@gmail.com> wrote:
> On Sep 16, 4:19 pm, ·······@corporate-world.lisp.de" <······@corporate-
>
>
>
> world.lisp.de> wrote:
> > On Sep 16, 8:57 pm, namekuseijin <············@gmail.com> wrote:
>
> > > On Aug 22, 7:16 pm, defn noob <············@yahoo.se> wrote:
>
> > > > I am trying to learn lisp macros and I generally find
> > > > listcomprehensions to be elegant and they dont exist in lisp.
> > > > This has to be a macro right? Cant be a function?
>
> > > > But how would I get started when it needs the arguments inside its
> > > > name?
>
> > > > In Python:
>
> > > > >>> [x**2 for x in [12,31,2,33] if x > 20]
> > > > [961, 1089]
>
> > > I like my own Scheme idiom
>
> > > (define (sqr n) (* n n))
>
> > > ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons)
>
> > > ; could be macroized for getting rid of (lambda (x) (> x 20)) but is
> > > pretty good and flexible already
>
> > > ;;;; given:
>
> > > ; fold list ls with initial result r and reducer f
> > > (define (fold ls r f)
> > >   (if (null? ls) r
> > >       (fold (cdr ls) (f (car ls) r) f)))
>
> > > ; returns an iterator from from to to, optionally incremented by step
> > > (define (range from to . step)
> > >   (let* ((step (if (null? step) 1 (car step)))
> > >          (inc (if (> from to)
> > >                  (lambda (n) (- n step))
> > >                  (lambda (n) (+ n step))))
> > >         (> (if (> from to) < >)))
> > >     (lambda (init f)
> > >       (let lp ((i from) (r init))
> > >         (if (> i to) r
> > >             (lp (inc i) (f i r)))))))
>
> > > ; very useful: filtered operation!  Collects result of op if ?
> > > (define (filtered ? op) (lambda (i r) (if (? i) (op i r) r)))
>
> > > ; fantastic idiom for comprehensions in Scheme!
> > > ; final is the final form of the collected items, like, all squared!
> > > ; in-range can be either a list or a (range from to) call
> > > ; given is the condition items in the list have to obey.  Optional
> > > (define (all final in-range . given)
> > >   (lambda (ini doit)
> > >     (let ((reducer (if (null? given)
> > >                        (lambda (i r) (doit (final i) r))
> > >                        (lambda (i r)
> > >                          ((filtered (car given) (lambda (i r) (doit
> > > (final i) r)))
> > >                           i r)))))
> > >       (if (procedure? in-range)
> > >           (in-range ini reducer)
> > >           (fold in-range ini reducer)))))
>
> > > ; exs:
>
> > > ((all (lambda (i) (* 2 i)) (range 1 10) odd?) '() cons)
> > > ;=> (18 14 10 6 2)
>
> > > ; non-filtered
> > > ((all sqr (range 1 10)) '() cons)
> > > ;=> (100 81 64 49 36 25 16 9 4 1)
>
> > > ; reverse
> > > ((all sqr (range 10 1)) '() cons)
> > > ;=> (1 4 9 16 25 36 49 64 81 100)
>
> > > ; prints 1 9 25
> > > ((all sqr '(1 2 3 4 5) odd?) #f (lambda (i r) (display i)(newline)r))
>
> > That's nicely obfuscated write-only code implementing a strange custom
> > iteration construct
> > with the results in reverse order!
>
> You can always do:
> (reverse ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons))

Sure, if you return a list. But if I input (1 2 3) into some
machine squaring them, I expect (1 4 9) to come out.

Personally I either use small functional building blocks
which do one thing: remove, reverse, map, reduce and so on
or I use something that is declarative like LOOP.

So either:

(mapcar #'sqr (remove-if-not (lambda (x) (> x 20)) '(12 31 2 33)))

or

(loop for i in '(12 31 2 33)
      when (> i 20) collect (sqr i))

>
> and it can be used in a multitude of ways, not simply to return a
> list:

That makes it worse. It should do one thing right or do
multiple things but be descriptive. From looking
at the forms I would never guess what it does.

> ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) 0 +)
> ((all sqr '(12 31 2 33) (lambda (x) (and (> x 20) (odd? x))) 1 *)
>
> Order is not important in many applications.

Haha. Right, that's the Scheme way.
>
> I don't think it's write-only, nor obfuscated, but then again, I'm the
> father of the child. :P  I'm used to writing these from scratch when
> I'm not with my personal libs around, so I guess they're not that
> complicated...
>
> The important thing to have in mind is that I wanted a fold without
> lists, then I got range, which returns an iterator for the range.
> Then all either uses range or fold depending on the in-range
> ardument.  all itself returns the iterator, so that you may apply the
> operation and initializer you want.  That's applicative programming at
> its best, I guess...

I think its applicative programming how it should not be used:

* the user interface is confused and adhoc
* the code is debugger-resistent
* code using that is not self-descriptive
* there is no simple model behind it (say like streams in SICP)

You can make it worse, though. By rewriting it as a set of macros.
From: namekuseijin
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <fbb489a6-fe2b-4251-99f4-5b494986f263@d1g2000hsg.googlegroups.com>
On Sep 16, 4:56 pm, ·······@corporate-world.lisp.de" <······@corporate-
world.lisp.de> wrote:
> On 16 Sep., 21:38, namekuseijin <············@gmail.com> wrote:
>
>
>
> > On Sep 16, 4:19 pm, ·······@corporate-world.lisp.de" <······@corporate-
>
> > world.lisp.de> wrote:
> > > On Sep 16, 8:57 pm, namekuseijin <············@gmail.com> wrote:
>
> > > > On Aug 22, 7:16 pm, defn noob <············@yahoo.se> wrote:
>
> > > > > I am trying to learn lisp macros and I generally find
> > > > > listcomprehensions to be elegant and they dont exist in lisp.
> > > > > This has to be a macro right? Cant be a function?
>
> > > > > But how would I get started when it needs the arguments inside its
> > > > > name?
>
> > > > > In Python:
>
> > > > > >>> [x**2 for x in [12,31,2,33] if x > 20]
> > > > > [961, 1089]
>
> > > > I like my own Scheme idiom
>
> > > > (define (sqr n) (* n n))
>
> > > > ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons)
>
> > > > ; could be macroized for getting rid of (lambda (x) (> x 20)) but is
> > > > pretty good and flexible already
>
> > > > ;;;; given:
>
> > > > ; fold list ls with initial result r and reducer f
> > > > (define (fold ls r f)
> > > >   (if (null? ls) r
> > > >       (fold (cdr ls) (f (car ls) r) f)))
>
> > > > ; returns an iterator from from to to, optionally incremented by step
> > > > (define (range from to . step)
> > > >   (let* ((step (if (null? step) 1 (car step)))
> > > >          (inc (if (> from to)
> > > >                  (lambda (n) (- n step))
> > > >                  (lambda (n) (+ n step))))
> > > >         (> (if (> from to) < >)))
> > > >     (lambda (init f)
> > > >       (let lp ((i from) (r init))
> > > >         (if (> i to) r
> > > >             (lp (inc i) (f i r)))))))
>
> > > > ; very useful: filtered operation!  Collects result of op if ?
> > > > (define (filtered ? op) (lambda (i r) (if (? i) (op i r) r)))
>
> > > > ; fantastic idiom for comprehensions in Scheme!
> > > > ; final is the final form of the collected items, like, all squared!
> > > > ; in-range can be either a list or a (range from to) call
> > > > ; given is the condition items in the list have to obey.  Optional
> > > > (define (all final in-range . given)
> > > >   (lambda (ini doit)
> > > >     (let ((reducer (if (null? given)
> > > >                        (lambda (i r) (doit (final i) r))
> > > >                        (lambda (i r)
> > > >                          ((filtered (car given) (lambda (i r) (doit
> > > > (final i) r)))
> > > >                           i r)))))
> > > >       (if (procedure? in-range)
> > > >           (in-range ini reducer)
> > > >           (fold in-range ini reducer)))))
>
> > > > ; exs:
>
> > > > ((all (lambda (i) (* 2 i)) (range 1 10) odd?) '() cons)
> > > > ;=> (18 14 10 6 2)
>
> > > > ; non-filtered
> > > > ((all sqr (range 1 10)) '() cons)
> > > > ;=> (100 81 64 49 36 25 16 9 4 1)
>
> > > > ; reverse
> > > > ((all sqr (range 10 1)) '() cons)
> > > > ;=> (1 4 9 16 25 36 49 64 81 100)
>
> > > > ; prints 1 9 25
> > > > ((all sqr '(1 2 3 4 5) odd?) #f (lambda (i r) (display i)(newline)r))
>
> > > That's nicely obfuscated write-only code implementing a strange custom
> > > iteration construct
> > > with the results in reverse order!
>

> > You can always do:
> > (reverse ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) '() cons))
>
> Sure, if you return a list.

That's what you're doing by '() cons at the end.

> But if I input (1 2 3) into some
> machine squaring them, I expect (1 4 9) to come out.

Right, it works in reverse order.  You could probably do normal order
at the expense of a more complex implementation and reliance on
mutation.

My main purpose was eliminating slow list processing from my coding,
relying primarily on the iterator-building range.  Thus, when I need
some specific order, I simply invert the terms to range.  I range over
vectors and strings as well.

Though I also give small lists a chance by permitting them in the
input.  Reversing the output list may be more efficient, since you're
likely filtering elements out.

> Personally I either use small functional building blocks
> which do one thing: remove, reverse, map, reduce and so on
> or I use something that is declarative like LOOP.
>
> So either:
>
> (mapcar #'sqr (remove-if-not (lambda (x) (> x 20)) '(12 31 2 33)))

In that small list argument, it's not evident, but mapping over large
sequences sucks.  That's one of the reasons I decided for range.

> (loop for i in '(12 31 2 33) when (> i 20) collect (sqr i))

Then, what you do with the collected data?  Run some function over it
to produce a final result, right?
((all sqr '(12 31 2 33) (lambda (x) (> x 20))) init function)

Done, without producing intermediary lists or anything.  It's also
almost the same written size as the macroed code above.

> > and it can be used in a multitude of ways, not simply to return a
> > list:
>
> That makes it worse.

This coming from a CLisper! :)

> It should do one thing right or do
> multiple things but be descriptive.

ah, lack of keywords... no problem when the interface is always the
same:  reducers are always of the form (lambda (i r) ...) where i is
the current item from the iterator, r the current result and reducer
itself produces the next result to be fed recursively.

> * the user interface is confused and adhoc

It's not adhoc, like I said above:  all and range return an iterator
and receive 2 arguments -- an initial value for the reducer and the
reducer.  That's all.

It's always (iterator init f) and it's result is always passed around
as the r argument and as the final value.  It's the same as fold/
reduce, except for the lack of a sequence/list argument.

You can also freely ignore the result, though, and then you have a
simple imperative construct, like in the print example.

> * the code is debugger-resistent

There's nothing to debug there.  It's perfect! ;)

> * code using that is not self-descriptive

Hmm, perhaps you should break out of CL's loops and have a look at
more applicative programming?

> * there is no simple model behind it (say like streams in SICP)

It looks pretty simple and consistent to me.

> You can make it worse, though. By rewriting it as a set of macros.

Not rewriting a set of macros, but simply wrapping it behind a macro.
Like:
(all (sqr x) (x in (range 1 100)) (and (> x 20) (odd? x)))

Which would equaly return an iterator.

> From looking
> at the forms I would never guess what it does.

Probably you should give Scheme another chance. ;)

> > ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) 0 +)
> > ((all sqr '(12 31 2 33) (lambda (x) (and (> x 20) (odd? x))) 1 *)
>
> > Order is not important in many applications.
>
> Haha. Right, that's the Scheme way.

That's functional programming way.
From: ······@corporate-world.lisp.de
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <9dc6e914-ffe4-4973-8c75-cd9380f04f50@y38g2000hsy.googlegroups.com>
On Sep 16, 10:38 pm, namekuseijin <············@gmail.com> wrote:
> On Sep 16, 4:56 pm, ·······@corporate-world.lisp.de" <······@corporate-

...

> My main purpose was eliminating slow list processing from my coding,
> relying primarily on the iterator-building range.  Thus, when I need
> some specific order, I simply invert the terms to range.  I range over
> vectors and strings as well.

How do you know it was slow? A bunch of functions calling each other
is not necessarily faster.

In Common Lisp one gets rid of the intermediate lists
by using LOOP/ITERATE/SERIES/.... and stays descriptive.

> Though I also give small lists a chance by permitting them in the
> input.  Reversing the output list may be more efficient, since you're
> likely filtering elements out.
>
> > Personally I either use small functional building blocks
> > which do one thing: remove, reverse, map, reduce and so on
> > or I use something that is declarative like LOOP.
>
> > So either:
>
> > (mapcar #'sqr (remove-if-not (lambda (x) (> x 20)) '(12 31 2 33)))
>
> In that small list argument, it's not evident, but mapping over large
> sequences sucks.  That's one of the reasons I decided for range.

Really? Does it matter? Often?

> > (loop for i in '(12 31 2 33) when (> i 20) collect (sqr i))
>
> Then, what you do with the collected data?  Run some function over it
> to produce a final result, right?

Sometimes. Often not. Then I do something else.

> ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) init function)
>
> Done, without producing intermediary lists or anything.  It's also
> almost the same written size as the macroed code above.

Yeah, but without any clue what the terms do and how it works.
Especially
a function that gets a function and returns a function and then gets
called
with another function. That's a rube-goldberg-device.

> > > and it can be used in a multitude of ways, not simply to return a
> > > list:
>
> > That makes it worse.
>
> This coming from a CLisper! :)
>
> > It should do one thing right or do
> > multiple things but be descriptive.
>
> ah, lack of keywords... no problem when the interface is always the
> same:  reducers are always of the form (lambda (i r) ...) where i is
> the current item from the iterator, r the current result and reducer
> itself produces the next result to be fed recursively.
>
> > * the user interface is confused and adhoc
>
> It's not adhoc, like I said above:  all and range return an iterator
> and receive 2 arguments -- an initial value for the reducer and the
> reducer.  That's all.

Yeah, and from reading source code using this constructs one
could not even guess what it does.

> It's always (iterator init f) and it's result is always passed around
> as the r argument and as the final value.  It's the same as fold/
> reduce, except for the lack of a sequence/list argument.
>
> You can also freely ignore the result, though, and then you have a
> simple imperative construct, like in the print example.
>
> > * the code is debugger-resistent
>
> There's nothing to debug there.  It's perfect! ;)

I'm talking about code that uses such constructs. Bad. Very bad.

> > * code using that is not self-descriptive
>
> Hmm, perhaps you should break out of CL's loops and have a look at
> more applicative programming?

See below.

> > * there is no simple model behind it (say like streams in SICP)
>
> It looks pretty simple and consistent to me.

Reread the chapter on streams and accumulation in SICP.

> > You can make it worse, though. By rewriting it as a set of macros.
>
> Not rewriting a set of macros, but simply wrapping it behind a macro.
> Like:
> (all (sqr x) (x in (range 1 100)) (and (> x 20) (odd? x)))
>
> Which would equaly return an iterator.
>
> > From looking
> > at the forms I would never guess what it does.
>
> Probably you should give Scheme another chance. ;)

Actually I have written much more complicated stuff in Scheme than
what you
have posted here. I prefer Common Lisp because it is much more
descriptive
and much better to use interactively.

> > > ((all sqr '(12 31 2 33) (lambda (x) (> x 20))) 0 +)
> > > ((all sqr '(12 31 2 33) (lambda (x) (and (> x 20) (odd? x))) 1 *)
>
> > > Order is not important in many applications.
>
> > Haha. Right, that's the Scheme way.
>
> That's functional programming way.

No, your code is the obfuscated way of functional programming.
Reread SICP to get back on the right way.
From: namekuseijin
Subject: Re: Listcomprehension-macro? Macros with args inside macro-name?
Date: 
Message-ID: <56807b6d-8ad4-433f-b6eb-d5c412059458@a70g2000hsh.googlegroups.com>
On 16 set, 18:51, ·······@corporate-world.lisp.de" <······@corporate-
world.lisp.de> wrote:
> On Sep 16, 10:38 pm, namekuseijin <············@gmail.com> wrote:
> > On Sep 16, 4:56 pm, ·······@corporate-world.lisp.de" <······@corporate-
>
> > My main purpose was eliminating slow list processing from my coding,
> > relying primarily on the iterator-building range.  Thus, when I need
> > some specific order, I simply invert the terms to range.  I range over
> > vectors and strings as well.
>
> How do you know it was slow?

mapcar is not much used for serious large dataset prossessing.  You
guys are known to abuse of loop.

> A bunch of functions calling each other
> is not necessarily faster.

It's really the application of just 2 function.

> In Common Lisp one gets rid of the intermediate lists
> by using LOOP/ITERATE/SERIES/.... and stays descriptive.

There's none of that in Scheme and even if had, I'd not use them any
more than I'd use C/C++...

Besides, is this
(all sqr '(12 31 2 33) (lambda (x) (> x 20)))

really so different from this?
[x**2 for x in [12,31,2,33] if x > 20]

In all, it takes 3 arguments: a final step function to process the
current item, a list or an iterator for a sequence and an optional
predicate.  Is there really any complication here?  Does it need more
description than the order and naming of the arguments?  I
purposefully put the order of the arguments in the same order they
appear in the popular comprehensions in Haskell or Python exactly to
match them.

The only difference is that my function returns an iterator while the
Python one directly returns a list.  I'd argue my function is better
since I can directly apply the resultant iterator to any function I
want together with its initial argument.

> > > (mapcar #'sqr (remove-if-not (lambda (x) (> x 20)) '(12 31 2 33)))
>
> > In that small list argument, it's not evident, but mapping over large
> > sequences sucks.  That's one of the reasons I decided for range.
>
> Really? Does it matter? Often?

Only when iterating large data sets.

> > > (loop for i in '(12 31 2 33) when (> i 20) collect (sqr i))
>
> > Then, what you do with the collected data?  Run some function over it
> > to produce a final result, right?
>
> Sometimes. Often not. Then I do something else.

Like printing it?
((all sqr (range 1 10000) (lambda (x) (prime? x))) #f
   (lambda (i r) (display i)(newline) r))
Passing it to another function?
((all sqr (range 1 10000) (lambda (x) (prime? x))) init something-
else)

Perhaps you're just mad because it makes Lisp look a bit like
Forth? ;)

> > Done, without producing intermediary lists or anything.  It's also
> > almost the same written size as the macroed code above.
>
> Yeah, but without any clue what the terms do and how it works.

I can't use any one function without knowing its arguments, its
interface.  Its the contract between the creator and the user.  The
interface for my function is pretty simple, self-evident and pretty
much the same as in the other popular comprehensions of other
languages.

There should also be no doubt for a Lisp programmer:  anything at the
head position of a list is a function to be evaluated.  So (all self
ls) should necessarily return a function.  It shouldn't also come as
surprise to most functional programmers used to fold.

> Especially
> a function that gets a function and returns a function and then gets
> called
> with another function. That's a rube-goldberg-device.

No, that's functional programming.

> > It's not adhoc, like I said above:  all and range return an iterator
> > and receive 2 arguments -- an initial value for the reducer and the
> > reducer.  That's all.
>
> Yeah, and from reading source code using this constructs one
> could not even guess what it does.

Then again, why am I discussing code from my personal libs with you?
It fits my thinking and I feel warm and cosy with that. :)

> Reread the chapter on streams and accumulation in SICP.

SICP is a lovely book, but I have my own way of doing things, like
everyone else.  There are many stream and even comprehension libs
available for Scheme out there, including the ones from SRFI.  I just
find mine more sexy. ;)

> No, your code is the obfuscated way of functional programming.

I like to call it myscm. :)

I thought one of the professed pro-Lisp arguments is that it's a
language that suits to your style of programming, not otherwise.  So
please, don't laugh at my preferences...

> Reread SICP to get back on the right way.

Just because I read Shakespeare I don't feel a sudden urge to speak in
old English... :P