-1

So I'm trying to print out a list that looks a little bit something like this (setq lst (list '- '- '- '- '-)) and in the past I used the print command to print out the whole list, however, when printing the whole list there is parenthesis on each side which I do not want to see. I want to use something like (format t) to print every bit of my list and I have something like this set up.

(loop for item from 0 to 4
     do (progn
             (format t "~X" (nth item lst))
        )
)

This code prints out the list perfectly fine like this, ----- but as a mentioned, I want it to print spaces between each element so that it is output like this - - - - -. I used the conditional "~X" because I looked up how to output spaces with the format command and you are apparently supposed to use "~X" but it does not work so if anybody knows how I could put spaces between elements that would be greatly appreciated.

DartAce345
  • 47
  • 6

3 Answers3

4

Why not just use the features provided by format:

CL-USER> (defvar *my-list* '(- - - -))
*MY-LIST*
CL-USER> (format nil "~{~A~^  ~}" *my-list*)
"-  -  -  -"
CL-USER> (format t "~{~A~^  ~}" *my-list*)
-  -  -  -
NIL

Here the first call to format outputs to a string to show where the spaces are placed. ~{ is an iteration directive that takes a list as its argument. The directives between the opening ~{ and closing ~} are used repeatedly as a format string for the elements of the input list. The ~^ directive causes an early escape from the iteration context when there are no more arguments; this prevents a trailing space from being added.

The second call to format just outputs to *standard-output*.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • The reason why I didn't do it like this is because I need to be able to index each item in a list and running functions on a string is something that I do not know how to do. – DartAce345 Jul 03 '21 at 22:07
  • 1
    Well, now I'm not exactly sure what you are trying to accomplish. Why do you need to index each list element? [Why are you using nested lists "in reality"?](https://stackoverflow.com/questions/68240118/how-would-i-print-out-a-list-with-spaces-in-between-elements/68240236#comment120605251_68240118) I answered wrt "_how would I print a list with spaces between elements_," but the new requirements are not clear; a good [MCVE] would help here. – ad absurdum Jul 04 '21 at 01:33
1

Regarding your update, that you posted in the answers to your own post:

  • First of all, you should edit your post to show us that you found a solution, rather than having us look through all the answers to see how much progress you made on your initial problem.
  • As it was already mentioned in another answer, you can iterate through the elements of a list using format built-in syntax with ~{~} and ~^ (see the documentation !)
  • In your own solution, when you iterate over the list using loop, you can put a space at the end of the format string rather than calling format twice ...
  • You can use loop for <elt> in <list> rather than iterating with the indices, and calling nth at each step - which is slower, and also more verbose.
  • The loop ... do <stuff> already wraps the <stuff> in what we call an implicit progn, i.e. you do not need to wrap yourself all your instructions in a progn, the loop macro does that for you.
  • There also exists the macro dolist, which is (arguably) simpler to use in those case when you simply want to iterate over a list.

To be fair, it looks like you are a Common Lisp beginner. In this case, I suggest you read the excellent Practical Common Lisp book, which covers in details the loop macro, the format function, and a lot of basic principles. It is available for free online, and is often recommended to beginners, for good reasons !

Numbra
  • 620
  • 4
  • 8
-3

Ok I came up with an ingenius solution to my problem which I definitely should've seen before.

(loop for item from 0 to 4
     do (progn
             (format t "~X" (nth item lst))
             (format t " ")
        )
)

I didn't realize I could print a space like that but it works perfectly fine. Sorry for wasting you all's time but hopefully someone else can see this if they are having a brain fart like me and thanks to everyone who tried to help.

DartAce345
  • 47
  • 6