From: John Thingstad on
Den Tue, 27 Oct 2009 12:46:41 -0400, skrev xach:

> Willem Broekema <metawilm(a)gmail.com> writes:
>
>> On 27 okt, 17:17, Bigos <ruby.obj...(a)googlemail.com> wrote:
>>> There's one thing that puzzles me most. What is best practice to group
>>> several functions together? For example in body of a loop.
>>>
>>> (dotimes (x 3)
>>>   (tagbody
>>>      (format t "one ~a~%" x )
>>>      (format t "two ~a~%" x)
>>>      (format t "three ~a~%" x) ) )
>>
>> PROGN is what you're looking for, though DOTIMES has an implicit PROGN
>> so in this example you don't need a grouping statement.
>
> Actually, DOTIMES (and some other constructs) have an implicit TAGBODY,
> not an implicit PROGN.
>
> Zach

Yes, looping constructs generally use tagbody and go under the hood.
(This might be a good time to look into 'macroexpand')
Also I feel I should mention that the tagbody here is redundant, it
doesn't need a progn either.
From: Pillsy on
On Oct 27, 12:50 pm, Bigos <ruby.obj...(a)googlemail.com> wrote:

> On 27 Oct, 16:31, Willem Broekema <metaw...(a)gmail.com> wrote:
[...]
> thanks everyone the code below does the trick, later I will also check
> dotimes

> (progn
>   (format t "one~%")
>   (format t "two~%")
>   3 )

I'm going to second Tamas' advice about reading a Common Lisp book at
this point. Since you have experience programming in other languages,
I think _Practical Common Lisp_, available at http://www.gigamonkeys.com/book/,
is the best choice. PROGN and TAGBODY both have their place, but PROGN
is needed pretty rarely and I can count the number of times I've used
TAGBODY on my thumbs. A decent book[1] will make the differences clear
and put you well on your way to writing idiomatic CL.

Cheers,
Pillsy

[1] PCL is excellent; its only shortcoming IMO is its lack of
exercises.

From: Bigos on

> I'm going to second Tamas' advice about reading a Common Lisp book at
> this point. Since you have experience programming in other languages,
> I think _Practical Common Lisp_, available athttp://www.gigamonkeys.com/book/,
> is the best choice. PROGN and TAGBODY both have their place, but PROGN
> is needed pretty rarely and I can count the number of times I've used
> TAGBODY on my thumbs. A decent book[1] will make the differences clear
> and put you well on your way to writing idiomatic CL.
>
> Cheers,
> Pillsy
>
> [1] PCL is excellent; its only shortcoming IMO is its lack of
> exercises.

I have read first 3 chapters. But the problem is, I can't learn while
staring at the book. I need to experiment, and feel how things
work.Perhaps, as you say, the book would be better with some
exercises.

Without help from the list I wouldn't know what to look for, so I'm
very grateful for your assistance.

now googling for: progn site:http://www.gigamonkeys.com gives me
information I need.

Now I'm going back to reading the book, then will experiment a bit,
and if I'm stuck I will post something again.
From: Slobodan Blazeski on
On Oct 27, 5:17 pm, Bigos <ruby.obj...(a)googlemail.com> wrote:
> There's one thing that puzzles me most. What is best practice to group
> several functions together? For example in body of a loop.
>
> (dotimes (x 3)
>   (tagbody
>      (format t "one ~a~%" x )
>      (format t "two ~a~%" x)
>      (format t "three ~a~%" x) ) )
>
> Is tagbody the right way to do it or even for one loop I need to
> define a function.
>
> (defun mytagbody (x)
>   (format t "one ~a~%" x )
>   (format t "two ~a~%" x)
>   (format t "three ~a~%" x))
> (dotimes (x 3)
>   (mytagbody x))
>
> I am puzzled because in Lisp code I have seen so far I didn't see
> tagbody being being used, although in other languages there are
> constructs to group statements together.

As other posters stated you don't need a grouping statement
(dotimes (x 3)
  (format t "one ~a~%" x)
  (format t "two ~a~%" x)
  (format t "three ~a~%" x))
However every time you write blockish code like above there's a big
chance you're doing something odd. How about below:
(dotimes (x 3)
  (mapc (lambda (s) (format t s x))
'("one ~a~%" "two ~a~%" "three ~a~%")))

cheers
Bobi

From: xach on
Slobodan Blazeski <slobodan.blazeski(a)gmail.com> writes:

> However every time you write blockish code like above there's a big
> chance you're doing something odd. How about below:
> (dotimes (x 3)
>   (mapc (lambda (s) (format t s x))
> '("one ~a~%" "two ~a~%" "three ~a~%")))

That is pretty odd too. What about this?

(dotimes (x 3)
(dotimes (y 3)
(format t "~R ~D~%" (1+ y) x)))

Zach
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7
Prev: Berlin Lispers Meetup: November 3, 2008
Next: why REVAPPEND?