Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

coding style in LOOP

47 views
Skip to first unread message

Jinsong Zhao

unread,
Mar 20, 2011, 5:22:46 AM3/20/11
to
Hi there,

I have a simple question about coding style in LOOP.

I have seen the LOOP with the following style in many books and
webpages, and I think it may be a standard style...

(defun myran (n lim)
(loop for x = (random lim)
repeat n
collect x))

But, today, I find the following style, which I don't see in any books...

(defun myran (n lim)
(loop :for x := (random lim)
:repeat n
:collect x))

In Vim, the second style will have all keyword highlighted.

Which do you prefer?

Thanks!

Jinsong

Pascal J. Bourguignon

unread,
Mar 20, 2011, 6:20:02 AM3/20/11
to
Jinsong Zhao <jsz...@yeah.net> writes:

In emacs too.

http://groups.google.com/group/comp.lang.lisp/msg/639035b352021859?hl=en


> Which do you prefer?

The second.


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Teemu Likonen

unread,
Mar 20, 2011, 6:23:34 AM3/20/11
to
* 2011-03-20 17:22 (+0800), Jinsong Zhao wrote:

> (defun myran (n lim)
> (loop for x = (random lim)
> repeat n
> collect x))

> (defun myran (n lim)
> (loop :for x := (random lim)
> :repeat n
> :collect x))

> Which do you prefer?

Some prefer this and some prefer that. Some don't care. Not very
interesting discussion. The question is about syntax highlighting, maybe
sometimes about indentation and about having those LOOP symbols interned
either in the current package or in the KEYWORDS package.

WJ

unread,
Mar 20, 2011, 8:16:25 AM3/20/11
to
Pascal J. Bourguignon wrote:

> Jinsong Zhao <jsz...@yeah.net> writes:
>
> > Hi there,
> >
> > I have a simple question about coding style in LOOP.
> >
> > I have seen the LOOP with the following style in many books and
> > webpages, and I think it may be a standard style...
> >
> > (defun myran (n lim)
> > (loop for x = (random lim)
> > repeat n
> > collect x))
> >
> > But, today, I find the following style, which I don't see in any books...
> >
> > (defun myran (n lim)
> > (loop :for x := (random lim)
> > :repeat n
> > :collect x))
> >
> > In Vim, the second style will have all keyword highlighted.
>
> In emacs too.
>
> http://groups.google.com/group/comp.lang.lisp/msg/639035b352021859?hl=en
>
>
> > Which do you prefer?
>
> The second.

Neither. Both are CoboL (CL).

Scheme:

guile> (map (lambda _ (random 6)) (iota 22))
(3 2 5 3 0 1 4 2 1 5 4 2 2 5 0 2 2 3 2 5 5 1)

MatzLisp:

(1..22).map{ rand 6 }
==>[4, 2, 0, 1, 1, 3, 4, 2, 5, 4, 0, 4, 1, 1, 5, 0, 2, 3, 0, 1, 4, 5]

kodifik

unread,
Mar 21, 2011, 5:54:03 AM3/21/11
to
On Mar 20, 1:16 pm, "WJ" <w_a_x_...@yahoo.com> wrote:
> Neither. Both are CoboL (CL).

You are right to an extent you may not even suspect:
The loop macro is an abstraction intended to bring closer a
programming language (Common Lisp) to a human language (English).
It does it well enough that it could conceivably be used as a general
way to express in English any computing loop in an unambiguous manner.
(Therefore useful to later program that given loop in any programming
language.)

So the loop macro is one of those points where Common Lisp really
trascends, providing a tool that could be used outside Common Lisp.

Tim Bradshaw

unread,
Mar 21, 2011, 7:28:03 AM3/21/11
to
On 2011-03-21 09:54:03 +0000, kodifik said:

> You are right to an extent you may not even suspect:
> The loop macro is an abstraction intended to bring closer a
> programming language (Common Lisp) to a human language (English).

Remember: that's a bad thing. Programming is *not meant to be easy*
and it's important to make sure that it is as cryptic as possible
otherwise people other than cult members might be able to understand
it. Of course, you also need to make sure it's *pure*, because
otherwise cult members will laughingly throw you into a pit full of
spikes and the rotting remains of other heretics.

For instance, you can't be writing this sort of thing:

(defun ss (n)
(let ((s 0) (i 0))
(tagbody
loop
(when (> i n) (go done))
(setf s (+ s (* i i))
i (+ i 1))
(go loop)
done
(return-from ss s))))

This is just terrible code. Non cult members may well be able to
understand it, and the cultists will have you in the pit before you
know it.

You might think this was better

(defun ss (n)
(loop for i from 0 to n
summing (* i i)))

But in fact it's far worse. Fellow cultists will definitely still be
at the laughing and pit-throwing, and the others will certainly
understand it *and laugh at you* because you don't know the closed form.

Instead, you must write this:

(define (ss n)
(let-values ([(a i l) (call/cc (lambda (c) (values 0 0 c)))])
(l (+ a (* i i))
(+ i 1)
(if (< i (- n 1))
l
(lambda (a i l) a)))))

This is almost a perfect solution. It's so achingly pure and cryptic
that you will be immediately appointed king of the cult and be able to
do your own laughing, and throw other members into pits you have first
made them dig, for which they will thank you as they slide down the
spikes. Non cult members stand essentially no chance of understanding
what it does and sniping about the whole silly closed-form thing:
certainly the only way they will be able to learn what it does is by
first joining the cult, at which point, as king, you can just throw
them straight into the pit.

It's important you understand this.

Paul Rubin

unread,
Mar 21, 2011, 8:28:35 AM3/21/11
to
Tim Bradshaw <t...@tfeb.org> writes:
> This is almost a perfect solution. It's so achingly pure and cryptic
> that you will be immediately appointed king of the cult and be able to...

http://www.willamette.edu/~fruehr/haskell/evolution.html

Marco Antoniotti

unread,
Mar 21, 2011, 10:01:25 AM3/21/11
to

Do you realize that now C.L.L. will be flooded by versions of all the
Haskell programs in Ruby and Guile, courtesy of WJ? :)

Cheers
--
MA

Tim Bradshaw

unread,
Mar 21, 2011, 11:20:20 AM3/21/11
to
On 2011-03-21 14:01:25 +0000, Marco Antoniotti said:

> Do you realize that now C.L.L. will be flooded by versions of all the
> Haskell programs in Ruby and Guile, courtesy of WJ? :)

Actually, it won't: Haskell is sufficiently obscure and left-field to
be a WJ-acceptable language ("it is a polymorphically statically typed,
lazy, purely functional language, quite different from most other
programming languages": there's nothing WJ likes more than that). The
risk (which I admit is not insignificant) is that he'll start posting
versions of various CL answers in haskell (which he will call
"currylisp").

Bakul Shah

unread,
Mar 21, 2011, 12:05:25 PM3/21/11
to

You can be lazy, pure, cryptic and creative. Sounds like a fun cult!

Marco Antoniotti

unread,
Mar 21, 2011, 12:08:17 PM3/21/11
to

Yep. I have a hunch that the average cobol-lisper is more attracted
to Haskell than, say, OCaml or F#.

Pascal J. Bourguignon

unread,
Mar 21, 2011, 12:40:27 PM3/21/11
to
Marco Antoniotti <mar...@gmail.com> writes:

Or to Erlang, which is more dynamically typed like lisp than Haskell, IIUC.

kenny

unread,
Mar 21, 2011, 4:49:19 PM3/21/11
to WJ
Ah, COBOL! Great language. PERFORM .... VARYING ... UNTIL .... and lots more, I am sure, a true DSL for iteration which is what LOOP is.

hk

WJ

unread,
May 5, 2011, 3:37:38 AM5/5/11
to
Pascal J. Bourguignon wrote:

> Jinsong Zhao <jsz...@yeah.net> writes:
>
> > Hi there,
> >
> > I have a simple question about coding style in LOOP.
> >
> > I have seen the LOOP with the following style in many books and
> > webpages, and I think it may be a standard style...
> >
> > (defun myran (n lim)
> > (loop for x = (random lim)
> > repeat n
> > collect x))
> >
> > But, today, I find the following style, which I don't see in any books...
> >
> > (defun myran (n lim)
> > (loop :for x := (random lim)
> > :repeat n
> > :collect x))
> >
> > In Vim, the second style will have all keyword highlighted.
>
> In emacs too.
>
> http://groups.google.com/group/comp.lang.lisp/msg/639035b352021859?hl=en
>
>
> > Which do you prefer?
>
> The second.

Arc:

(n-of 22 (rand 6))

==>

(4 0 3 5 1 4 3 3 5 1 5 5 3 0 0 0 0 2 2 5 4 5)

Jussi Piitulainen

unread,
May 5, 2011, 4:06:05 AM5/5/11
to
"WJ" comments on a question and answer:

[paraphrasing]

Q. Do you prefer your loop keywords with or without a colon?

A. With.

C. (n-of 22 (rand 6))

Made me laugh. (Thank you.)

0 new messages