From: Slobodan Blazeski on
On Oct 27, 9:03 pm, Slobodan Blazeski <slobodan.blaze...(a)gmail.com>
wrote:
> 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~%")))
>
Or even something like this:
(mapcan (lambda (x)
(mapcan (lambda (s) (format t s x))
'("one ~a~%" "two ~a~%" "three ~a~%")))
(enum 3 1))



cheers
Bobi

(*)
(defun enum (count &optional (start 0) (step 1))
(loop repeat count for i from start by step collect i))
From: Slobodan Blazeski on
On Oct 27, 9:11 pm, x...(a)unnamed.xach.com wrote:
> Slobodan Blazeski <slobodan.blaze...(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)))
Good but too .. hm simple. Maybe below:

(mapcan
(lambda (n)
(apply (lambda (s x)
(format t s x)) n))
(outer '("one ~a~%" "two ~a~%" "three ~a~%") (enum 3 1)))




>
> Zach

(*)
(defun outer (x y)
(loop for i in x
collect (loop for j in y
collect (+ i j))))

(defun enum (count &optional (start 0) (step 1))
(loop repeat count for i from start by step collect i))
From: Slobodan Blazeski on
On Oct 27, 9:36 pm, Slobodan Blazeski <slobodan.blaze...(a)gmail.com>
wrote:
> On Oct 27, 9:11 pm, x...(a)unnamed.xach.com wrote:> Slobodan Blazeski <slobodan.blaze...(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)))
>
> Good but too .. hm simple. Maybe below:
>
> (mapcan
>   (lambda (n)
>     (apply (lambda (s x)
>              (format t s x)) n))
>   (outer '("one ~a~%" "two ~a~%" "three ~a~%") (enum 3 1)))
>
>
>
> > Zach
>
> (*)

Sorry for posting buggy code but something really sucks when moving
code between groups and the listener here's the working version.


(mapcan
(lambda (n)
(apply (lambda (s x) (format t s x)) n))
(outer '("one ~a~%" "two ~a~%" "three ~a~%") (enum 3 1)))

(defun outer (x y)
(loop for i in x
nconc (loop for j in y
collect (list i j))))

(defun enum (count &optional (start 0) (step 1))
(loop repeat count for i from start by step collect i))


Just to make myself clear the Zach version with nested dotimes is
probably the right choice mine was just a little exercise in
obfuscation through loop less code.

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

> Just to make myself clear the Zach version with nested dotimes is
> probably the right choice mine was just a little exercise in
> obfuscation through loop less code.

When possible, I prefer obfuscation through FORMAT.

Zach
From: Pillsy on
On Oct 27, 3:40 pm, Bigos <ruby.obj...(a)googlemail.com> wrote:
> > 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.
[...]
> > [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.

Another book to look at is David Touretzky's _Common Lisp: A Gentle
Introduction to Symbolic Programming_[1]. I remember reading it in
parallel with _PCL_, and found its exercises quite valuable. I think
it lives up to its title in terms of being gentle; you'll probably
just want to skim through the first few chapters.

HTH,
Pillsy

[1] http://www.cs.cmu.edu/~dst/LispBook/
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7
Prev: Berlin Lispers Meetup: November 3, 2008
Next: why REVAPPEND?