[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: counting list items in FORMAT statement

William James

6/7/2015 8:44:00 AM

Janis Dzerins wrote:

> edi@agharta.de (Dr. Edmund Weitz) writes:
>
> > Hi!
> >
> > I have a variable *PLACES* that holds a list of lists, like so:
> >
> > * (defparameter *places* '((1 2 3) nil nil (4 6) (8 5) (0) nil (7)))
> > *PLACES*
> >
> > I want each element of *PLACES* to be printed in reverse and on a line
> > of its own while preceded by its position in *PLACES*, i.e. I want
> >
> > 0: 3 2 1
> > 1:
> > 2:
> > 3: 6 4
> > 4: 5 8
> > 5: 0
> > 6:
> > 7: 7
> >
> > The following statement works fine, and I could just use it and be
> > happy with it:
> >
> > * (dotimes (i (length *places*))
> > (format t "~A:~{ ~A~}~%" i (reverse (nth i *places*))))
> > 0: 3 2 1
> > 1:
> > 2:
> > 3: 6 4
> > 4: 5 8
> > 5: 0
> > 6:
> > 7: 7
> > NIL
> >
> > But... - just out of curiosity - I'm asking myself if it would be
> > possible to get rid of the DOTIMES loop and have the positions
> > automatically be printed by a FORMAT statement that'll take *PLACES*
> > as its only format argument.
>
> DOLIST would be a better start.
>
> And extended LOOP:
>
> (loop
> for i upfrom 0
> for list in *places*
> do (format t "~&~A:~{ ~A~}~%" i (reverse list)))

Gauche Scheme:

(define places '((1 2 3) () () (4 6) (8 5) (0) () (7)))

(use srfi-42 :only (do-ec))

(do-ec (: list (index i) places)
(print i ":" (fold (pa$ format " ~a~a") "" list)))

===>
0: 3 2 1
1:
2:
3: 6 4
4: 5 8
5: 0
6:
7: 7

--
Under the disguise of liberalism, humanism, and democracy, Europeans have been
persuaded to commit racial suicide -- a race that has achieved so much and has
survived so much has been tricked into welcoming its own downfall and to take
active measures in order to become a stranger on its own soil.
www.kolumbus.fi/aquilon/moscowspeech2010.htm
2 Answers

William James

6/7/2015 9:51:00 AM

0

WJ wrote:

> Janis Dzerins wrote:
>
> > edi@agharta.de (Dr. Edmund Weitz) writes:
> >
> > > Hi!
> > >
> > > I have a variable PLACES that holds a list of lists, like so:
> > >
> > > * (defparameter places '((1 2 3) nil nil (4 6) (8 5) (0) nil (7)))
> > > PLACES
> > >
> > > I want each element of PLACES to be printed in reverse and on a line
> > > of its own while preceded by its position in PLACES, i.e. I want
> > >
> > > 0: 3 2 1
> > > 1:
> > > 2:
> > > 3: 6 4
> > > 4: 5 8
> > > 5: 0
> > > 6:
> > > 7: 7
> > >
> > > The following statement works fine, and I could just use it and be
> > > happy with it:
> > >
> > > * (dotimes (i (length places))
> > > (format t "~A:~{ ~A~}~%" i (reverse (nth i places))))
> > > 0: 3 2 1
> > > 1:
> > > 2:
> > > 3: 6 4
> > > 4: 5 8
> > > 5: 0
> > > 6:
> > > 7: 7
> > > NIL
> > >
> > > But... - just out of curiosity - I'm asking myself if it would be
> > > possible to get rid of the DOTIMES loop and have the positions
> > > automatically be printed by a FORMAT statement that'll take PLACES
> > > as its only format argument.
> >
> > DOLIST would be a better start.
> >
> > And extended LOOP:
> >
> > (loop
> > for i upfrom 0
> > for list in places
> > do (format t "~&~A:~{ ~A~}~%" i (reverse list)))
>
> Gauche Scheme:
>
> (define places '((1 2 3) () () (4 6) (8 5) (0) () (7)))
>
> (use srfi-42 :only (do-ec))
>
> (do-ec (: list (index i) places)
> (print i ":" (fold (pa$ format " ~a~a") "" list)))
>
> ===>
> 0: 3 2 1
> 1:
> 2:
> 3: 6 4
> 4: 5 8
> 5: 0
> 6:
> 7: 7

Here's a way to do it with no looping:

(use text.tree :only (write-tree))

(write-tree
(map
(^(i x) (list i ": " (intersperse " " (reverse x)) "\n"))
(lrange 0)
places))

0: 3 2 1
1:
2:
3: 6 4
4: 5 8
5: 0
6:
7: 7

--
For two years ... he was held in solitary confinement in the Toronto West
Detention Centre, on the pretext that he is a threat to national security....
[A] court in Mannheim sentenced him to five years imprisonment for the crime of
"popular incitement" under Germany's notorious "Holocaust denial" statute.
http://www.revisionists.com/revisionists/z...


William James

11/28/2015 6:33:00 AM

0

WJ wrote:

> Janis Dzerins wrote:
>
> > edi@agharta.de (Dr. Edmund Weitz) writes:
> >
> > > Hi!
> > >
> > > I have a variable PLACES that holds a list of lists, like so:
> > >
> > > * (defparameter places '((1 2 3) nil nil (4 6) (8 5) (0) nil (7)))
> > > PLACES
> > >
> > > I want each element of PLACES to be printed in reverse and on a line
> > > of its own while preceded by its position in PLACES, i.e. I want
> > >
> > > 0: 3 2 1
> > > 1:
> > > 2:
> > > 3: 6 4
> > > 4: 5 8
> > > 5: 0
> > > 6:
> > > 7: 7
> > >
> > > The following statement works fine, and I could just use it and be
> > > happy with it:
> > >
> > > * (dotimes (i (length places))
> > > (format t "~A:~{ ~A~}~%" i (reverse (nth i places))))
> > > 0: 3 2 1
> > > 1:
> > > 2:
> > > 3: 6 4
> > > 4: 5 8
> > > 5: 0
> > > 6:
> > > 7: 7
> > > NIL
> > >
> > > But... - just out of curiosity - I'm asking myself if it would be
> > > possible to get rid of the DOTIMES loop and have the positions
> > > automatically be printed by a FORMAT statement that'll take PLACES
> > > as its only format argument.
> >
> > DOLIST would be a better start.
> >
> > And extended LOOP:
> >
> > (loop
> > for i upfrom 0
> > for list in places
> > do (format t "~&~A:~{ ~A~}~%" i (reverse list)))

Ocaml:

open Printf;;
open List;;

[[1;2;3];[];[];[4;6];[8;5];[0];[];[7]] |>
iteri (fun i xs -> printf "%d:" i;
xs |> rev |> iter (printf " %d");
printf "\n");;

0: 3 2 1
1:
2:
3: 6 4
4: 5 8
5: 0
6:
7: 7

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... They've already said that everybody from
Syria can come to Sweden.... [T]hey are actually thinking of commandeering
people's vacation homes because they need more housing for immigrants.
--- Dr. Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)