[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Beginner question on destructuring bind

Anoop GR

12/31/2015 5:00:00 AM

(defmacro with-gensyms1 (names &body body)
`(let ,(mapcar #'(lambda (name) `(,name (gensym)))
names)
,@body))


(defmacro with-gensyms2 ((&rest names) &body body)
`(let ,(mapcar #'(lambda (name) `(,name (gensym)))
names)
,@body))

;;which of the above two is a better coding style in lisp and why?
;;or are they both equivalent with the 2nd one being just more verbose ?
5 Answers

Pascal J. Bourguignon

12/31/2015 5:34:00 AM

0

Anoop GR <anoopemacs@gmail.com> writes:

> (defmacro with-gensyms1 (names &body body)
> `(let ,(mapcar #'(lambda (name) `(,name (gensym)))
> names)
> ,@body))
>
>
> (defmacro with-gensyms2 ((&rest names) &body body)
> `(let ,(mapcar #'(lambda (name) `(,name (gensym)))
> names)
> ,@body))
>
> ;;which of the above two is a better coding style in lisp and why?
> ;;or are they both equivalent with the 2nd one being just more verbose ?

For the lisp implementation it doesn't make a difference, and both will
signal the same error when passed an atom instead of a list as first
argument (both mapcar and defmacro give the same error message in ccl).

But the editor, or yourself, may understand faster that you need to
write a list of names in the code with the second definition than with
the first. The editor can use this difference in formulation to indent
and auto complete better.

From the first line of with-gensyms1,

(with-gensyms1 my-names
(print hello) (terpri))

looks perfect legit.

Not so with the first line of with-gensyms2.


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Kenneth Tilton

1/6/2016 6:21:00 PM

0

On Wednesday, December 30, 2015 at 11:59:51 PM UTC-5, Anoop GR wrote:
> (defmacro with-gensyms1 (names &body body)
> `(let ,(mapcar #'(lambda (name) `(,name (gensym)))
> names)
> ,@body))
>
>
> (defmacro with-gensyms2 ((&rest names) &body body)
> `(let ,(mapcar #'(lambda (name) `(,name (gensym)))
> names)
> ,@body))
>
> ;;which of the above two is a better coding style in lisp and why?
> ;;or are they both equivalent with the 2nd one being just more verbose ?

If you do not like verbose, WJ and Paul Graham can tell you, you got the wrong language (and you should learn loop):

(defmacro with-gensyms ((&rest symbols) &body body)
`(let ,(loop for sym in symbols
collecting `(,sym (gensym ,(string sym))))
,@body))

I am pretty sure I have one macro somewhere that takes your version one approach, but it was just for yucks. It feels brittle.

-hk

William James

1/6/2016 7:27:00 PM

0

His Kennyness wrote:

> you got the wrong language

That ought to be: "you have got the wrong language".
Worshippers of CL (COBOL-Like) and LOOP have very poor
language skills.

> you should learn loop

Worshippers of CL (COBOL-Like) have no affinity for Lispy
programming, so they are irresistibly drawn to the feculence
of LOOP as a fly is drawn to a turd.


Here's an example:

Kenny Tilton wrote:

> btw, I believe the thing that got me to look at loop was that it
> provides efficiency for free, which is rare in cheats:
>
> (loop for x in them
> when (yada x)
> collect x)
>
> vs:
>
> (let (out)
> (dolist (x them (nreverse out))
> (when (yada x) (push x out)
>
> If I had learned Lisp from PCL I would have grown up with loop as a
> native sub-language and not gone seven years without it.

Seven years? Seven years? If he had had any affinity
whatsoever for Lispy programming and had not literally been an
admirer of COBOL, he would have known after seven days:

(remove-if-not #'yada them)

In Scheme:

(filter yada them)

In MatzLisp (Ruby):

(0..9).select(&:even?)
==>[0, 2, 4, 6, 8]




Paul Graham:

I consider Loop one of the worst flaws in CL, and an example
to be borne in mind by both macro writers and language designers.


[ In "ANSI Common Lisp", Graham makes the following comments: ]

The loop macro was originally designed to help inexperienced
Lisp users write iterative code. Instead of writing Lisp code,
you express your program in a form meant to resemble English,
and this is then translated into Lisp. Unfortunately, loop is
more like English than its designers ever intended: you can
use it in simple cases without quite understanding how it
works, but to understand it in the abstract is almost
impossible.
....
the ANSI standard does not really give a formal specification
of its behavior.
....
The first thing one notices about the loop macro is that it
has syntax. A loop expression contains not subexpressions but
clauses. The clauses are not delimited by parentheses;
instead, each kind has a distinct syntax. In that, loop
resembles traditional Algol-like languages. But the other
distinctive feature of loop, which makes it as unlike Algol as
Lisp, is that the order in which things happen is only
loosely related to the order in which the clauses occur.
....
For such reasons, the use of loop cannot be recommended.


Dan Weinreb, one of the designers of Common Lisp:

.... the problem with LOOP was that it turned out to be hard to
predict what it would do, when you started using a lot of
different facets of LOOP all together. This is a serious problem
since the whole idea of LOOP was to let you use many facets
together; if you're not doing that, LOOP is overkill.


Barry Margolin, 05 Apr 2001
(http://groups.google.com/group/comp.lang.lisp/msg/8a48ce...)

>(My second rule of thumb concerning LOOP would be the negative of
>Barry Margolin's: The more complex the looping, the more you need/want
>to use LOOP.)

My recommendation is based on seeing many question in the past of the form
"What happens if you use both XXX and YYY in the same LOOP?" The
unfortunate fact is that when we were writing the standard we didn't have
time to nail down all the possible interactions between different LOOP
features, so many of these are not well specified. And even if we did get
it right in the standard, it's likely to be difficult to find them and I
wouldn't trust that all implementors got it right (many of those questions
were probably from implementors, trying to figure out what they were
supposed to do). And even if they all got it right, someone reading your
code may not be able to figure it out.

So, with all those potential problems, my feeling is that if you have to
ask, it's probably better to use something other than LOOP.


Barry Margolin:

> 3. Loop is very powerful, granted, and many people are trying to
> argue that "you can do so much with loop that it's unreadable."
> This is not an argument.

But it is! Because any use of LOOP has the potential to be
unreadable, the reader must read it carefully to verify that
it's just one of the cases that doesn't require careful
reading!


Barry Margolin: (05 Apr 2002 20:57:48 GMT)

This seems like a big change just to clean up the way LOOP is described.
And LOOP will still be a wart, because it will be the only language feature
that uses "per-macro keywords". Providing this interface and giving a name
to them would encourage other macro designers to do something similar, and
we don't want more things like LOOP.


Barry Margolin (1997-01-08)

The Generators and Collectors macros described in Appendix B
of CLtL2 also provide this convenience and are much more
Lisp-like. It's too bad they weren't around when LOOP was
gaining popularity, and I think they're a better way to go.
But LOOP is what I got used to, and its popularity is why it
got elevated into the ANSI standard.


From: John Foderaro <jkf@unspamx.franz.com>
Newsgroups: comp.lang.lisp
Subject: Re: the "loop" macro
Date: Sun, 26 Aug 2001 10:51:26 -0700

I'm not trying to join a debate on loop. I just wanted to present
the other side of [the issue so that] the intelligent people can
then weigh the arguments on both sides.

I'm not suggesting that loop can be fixed either by adding
parenthesis or coming up with ways of indenting it to make it
understandable. It's a lost cause.


--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...

Kaz Kylheku

1/6/2016 8:18:00 PM

0

On 2016-01-06, WJ <w_a_x_man@yahoo.com> wrote:
> His Kennyness wrote:
>
>> you got the wrong language
>
> That ought to be: "you have got the wrong language".

Time for an ESL lesson about the verb "get".

"Get" someone to translate it to your native Retardese, if you don't
"get" any of it. "Got" it, dumb asshole?

In English, "get" takes a direct object complement. The clause is a
pefectly grammatical instance of the plain past form of the verb taking
an object.

The clause pattern "<subject> has got <object>" is grammatically and
semantically distinct from "<subject> got <object>".

"The police got the wrong man":

some time in the past, possibly ages ago, the police captured the
wrong man. He might no longer be in custody.

"The police have got the wrong man":

the police captured the wrong man, whom they still have in custody.

Moreover, you can't randomly stick "have" in front of "got";
it may be ungrammatical:

On the morning of February 3rd, the police *have got the
wrong man.

"You got the wrong language" is like "you got the wrong number".
It is perfectly correct North American English. The verb "get"
has broad semantics, both alone and in phrasal combination with various
prepositions. In sentences like these, it indicates the subject's
focus or attainment of an object or destination, meaning:
"your search for a language has momentarily yielded the wrong one" or
"you have reached the wrong number".

William James

4/23/2016 5:30:00 AM

0

WJ wrote:

> > btw, I believe the thing that got me to look at loop was that it
> > provides efficiency for free, which is rare in cheats:
> >
> > (loop for x in them
> > when (yada x)
> > collect x)
> >
> > vs:
> >
> > (let (out)
> > (dolist (x them (nreverse out))
> > (when (yada x) (push x out)
> >
> > If I had learned Lisp from PCL I would have grown up with loop as a
> > native sub-language and not gone seven years without it.
>
> Seven years? Seven years? If he had had any affinity
> whatsoever for Lispy programming and had not literally been an
> admirer of COBOL, he would have known after seven days:
>
> (remove-if-not #'yada them)
>
> In Scheme:
>
> (filter yada them)
>
> In MatzLisp (Ruby):
>
> (0..9).select(&:even?)
> ==>[0, 2, 4, 6, 8]

OCaml:

# List.filter ((<) 2) [0;2;3;8;9];;
- : int list = [3; 8; 9]

--
In Europe, police-state controls on thought and behavior intended to buttress
the ... anti-White revolution, are firmly ensconced.... [P]eople have been
investigated and in some cases arrested for Facebook and Twitter posts simply
opposing migration and the transformation of their societies.
www.theoccidentalobserver.net/2016/03/could-it-happen-here/