From: Thomas M. Hermann
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <d34d5702-035c-4ec3-bac8-9b851efe0ea5@h23g2000prf.googlegroups.com>
On Nov 12, 11:09 am, Francogrex <······@grex.org> wrote:
> Here's a simple example, it's very likely not the best in terms of
> syntax, but I give it as an illustration of the problem I have:
>
> (setf z (loop for i from 1 to 100 collect i))
> (setf test nil)
> (dotimes (i (length z))
>   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
>
> The problem is that the output of 'test' on my lisp (I use ECL with
> Emacs) is introducing newlines and they also show when I print it to a
> file; example:
> (99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61 59 57 55
> 53 51 49
>  47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1)
>
> See, it cut to a newline after the value 49; How I can do so that it
> print/outputs on only one line? Is this something that is
> implementation specific or is it realted to emacs? thanks

I bestow upon you the award for the most redundant steps per line of
code. First, let's generate your list of odd numbers correctly.

(loop for number from 1 to 100
   when (oddp number) collect number)

If you actually want the numbers in reverse order, reverse the 100 and
the 1.

Now, for output, you need to read the section in your favorite lisp
reference on FORMAT.

From: Rob Warnock
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <256dnaIiJ9ssF4bUnZ2dnUVZ_uydnZ2d@speakeasy.net>
Thomas M. Hermann <··········@gmail.com> wrote:
+---------------
| Francogrex <······@grex.org> wrote:
| > (setf z (loop for i from 1 to 100 collect i))
| > (setf test nil)
| > (dotimes (i (length z))
| >   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
...
| I bestow upon you the award for the most redundant steps per line
| of code. First, let's generate your list of odd numbers correctly.
| 
| (loop for number from 1 to 100
|    when (oddp number) collect number)
+---------------

Oh, *puh-LEEZE!*  You can do better than that!  ;-}  ;-}

   (loop for number from 1 to 100 by 2 collect number)

+---------------
| If you actually want the numbers in reverse order,
| reverse the 100 and the 1.
+---------------

And either change FROM to DOWNFROM or TO to DOWNTO
[since CLHS 6.1.2.1.1 doesn't allow negative BY increments].
And since 100 isn't ODDP, you need to start from 99:

   (loop for number from 99 downto 1 by 2 collect number)

or:

   (loop for number downfrom 99 to 1 by 2 collect number)

+---------------
| Now, for output, you need to read the section in your favorite lisp
| reference on FORMAT.
+---------------

Well, not FORMAT per se, but rather *PRINT-PRETTY*. Binding it
to NIL should get rid of the OP's "spurious" newlines, which
can also be done with the :PRETTY keyword arg to WRITE:

    > (setf *print-right-margin* 30) ; Distinguish REPL value from WRITE output

    30
    > (write (loop for number from 1 to 100 by 2 collect number)
	     :pretty nil)
    (1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99)
    (1 3 5 7 9 11 13 15 17 19 21
     23 25 27 29 31 33 35 37 39
     41 43 45 47 49 51 53 55 57
     59 61 63 65 67 69 71 73 75
     77 79 81 83 85 87 89 91 93
     95 97 99)
    > 

Since pretty-printing is typically a serious performance hog anyway
[see many past threads here on that issue], you'll generally want to
disable it if you're doing any sort of massive textual output not
intended for humans to read, such as HTML or JavaScript generation.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Thomas M. Hermann
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <560023c1-9146-419a-9c4f-8753e71c5dc4@41g2000yqf.googlegroups.com>
On Nov 12, 8:02 pm, ····@rpw3.org (Rob Warnock) wrote:
> Thomas M. Hermann <··········@gmail.com> wrote:
> +---------------| Francogrex <······@grex.org> wrote:
>
> | > (setf z (loop for i from 1 to 100 collect i))
> | > (setf test nil)
> | > (dotimes (i (length z))
> | >   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
> ...
> | I bestow upon you the award for the most redundant steps per line
> | of code. First, let's generate your list of odd numbers correctly.
> |
> | (loop for number from 1 to 100
> |    when (oddp number) collect number)
> +---------------
>
> Oh, *puh-LEEZE!*  You can do better than that!  ;-}  ;-}
>
>    (loop for number from 1 to 100 by 2 collect number)

Excellent.

>
> +---------------
> | If you actually want the numbers in reverse order,
> | reverse the 100 and the 1.
> +---------------
>
> And either change FROM to DOWNFROM or TO to DOWNTO
> [since CLHS 6.1.2.1.1 doesn't allow negative BY increments].
> And since 100 isn't ODDP, you need to start from 99:
>
>    (loop for number from 99 downto 1 by 2 collect number)
>
> or:
>
>    (loop for number downfrom 99 to 1 by 2 collect number)

Thanks.

>
> +---------------
> | Now, for output, you need to read the section in your favorite lisp
> | reference on FORMAT.
> +---------------
>
> Well, not FORMAT per se, but rather *PRINT-PRETTY*. Binding it
> to NIL should get rid of the OP's "spurious" newlines, which
> can also be done with the :PRETTY keyword arg to WRITE:
>
>     > (setf *print-right-margin* 30) ; Distinguish REPL value from WRITE output
>
>     30
>     > (write (loop for number from 1 to 100 by 2 collect number)
>              :pretty nil)
>     (1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99)
>     (1 3 5 7 9 11 13 15 17 19 21
>      23 25 27 29 31 33 35 37 39
>      41 43 45 47 49 51 53 55 57
>      59 61 63 65 67 69 71 73 75
>      77 79 81 83 85 87 89 91 93
>      95 97 99)
>     >
>
> Since pretty-printing is typically a serious performance hog anyway
> [see many past threads here on that issue], you'll generally want to
> disable it if you're doing any sort of massive textual output not
> intended for humans to read, such as HTML or JavaScript generation.
>

This is what confused me about the question. I generally use FORMAT
for output. What are the situations where one would rely on the Lisp
printer for output other than working with the REPL? The only one I
had read about was writing out Lisp data to be read back in with READ.
From: Rob Warnock
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <gPydnQPLE7TnlIHUnZ2dnUVZ_v3inZ2d@speakeasy.net>
Thomas M. Hermann <··········@gmail.com> wrote:
+---------------
| ····@rpw3.org (Rob Warnock) wrote:
| > Since pretty-printing is typically a serious performance hog anyway
| > [see many past threads here on that issue], you'll generally want to
| > disable it if you're doing any sort of massive textual output not
| > intended for humans to read, such as HTML or JavaScript generation.
| 
| This is what confused me about the question. I generally use FORMAT
| for output. What are the situations where one would rely on the Lisp
| printer for output other than working with the REPL?
+---------------

FORMAT uses the pretty-printer internally if *PRINT-PRETTY* is true,
see CLHS 22.3.4 "FORMAT Printer Operations" (especially 22.3.4.3
"Tilde W: Write") and 22.3.5 "FORMAT Pretty Printer Operations",
etc., etc. So if you're using FORMAT you *are* using "the Lisp printer"
(pretty-printer).

And, yes, that also implies that even FORMAT'd output can be sped up
tremendously by binding *PRINT-PRETTY* to NIL. [Well, assuming you
don't *need* the pretty-printing functions in that situation...]

The CLHS non-normative issue writeup FORMAT-PRETTY-PRINT:YES
clarified the relationship between the behavior of FORMAT and
various output functions such as PRINC & PRIN1. In particular,
FORMAT-PRETTY-PRINT:YES contains a handy reference of which
FORMAT controls bind which pretty-printer variables to what
values (though it's only partial, e.g., it doesn't mention
that ~:W binds *PRINT-PRETTY* to true). And this little bit
indirectly addresses your question above:

    A major advantage of this proposal is that the following two
    expressions are guaranteed to have exactly the same effect:

    (FORMAT stream "~S" object)

    (PRIN1 object stream)

    Thus use or non-use of FORMAT becomes a purely stylistic matter.

[Ditto FORMAT/~A and PRINC.]

Thus while most people do indeed use FORMAT most of the time, some
prefer to use PRINC, PRIN1, WRITE (plus keywords), etc., because
it feels "more Lisp-y" to them. And sometimes you just need to do
something that simply isn't convenient or even possible with FORMAT,
so you handle it by wrapping however complex CL code you need around
the functional printer interface PRINC/PRIN1/WRITE [and, in extremis,
WRITE-CHAR], possibly even using the PPRINT-xxx functions explicitly.

+---------------
| The only one I had read about was writing out Lisp data to be
| read back in with READ.
+---------------

See above re FORMAT/~S vs. PRIN1.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Thomas M. Hermann
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <38b74ca0-e19c-4dd8-a7e5-ef1ffc4b23f8@h23g2000prf.googlegroups.com>
On Nov 13, 5:03 am, ····@rpw3.org (Rob Warnock) wrote:
> Thomas M. Hermann <··········@gmail.com> wrote:
> +---------------
> | ····@rpw3.org (Rob Warnock) wrote:
> | > Since pretty-printing is typically a serious performance hog anyway
> | > [see many past threads here on that issue], you'll generally want to
> | > disable it if you're doing any sort of massive textual output not
> | > intended for humans to read, such as HTML or JavaScript generation.
> |
> | This is what confused me about the question. I generally use FORMAT
> | for output. What are the situations where one would rely on the Lisp
> | printer for output other than working with the REPL?
> +---------------
>
> FORMAT uses the pretty-printer internally if *PRINT-PRETTY* is true,
> see CLHS 22.3.4 "FORMAT Printer Operations" (especially 22.3.4.3
> "Tilde W: Write") and 22.3.5 "FORMAT Pretty Printer Operations",
> etc., etc. So if you're using FORMAT you *are* using "the Lisp printer"
> (pretty-printer).

Those are 2 dots I hadn't connected.

> And, yes, that also implies that even FORMAT'd output can be sped up
> tremendously by binding *PRINT-PRETTY* to NIL. [Well, assuming you
> don't *need* the pretty-printing functions in that situation...]

Thanks, that may have an actual impact on stuff I'm doing now.

> The CLHS non-normative issue writeup FORMAT-PRETTY-PRINT:YES
> clarified the relationship between the behavior of FORMAT and
> various output functions such as PRINC & PRIN1. In particular,
> FORMAT-PRETTY-PRINT:YES contains a handy reference of which
> FORMAT controls bind which pretty-printer variables to what
> values (though it's only partial, e.g., it doesn't mention
> that ~:W binds *PRINT-PRETTY* to true). And this little bit
> indirectly addresses your question above:
>
>     A major advantage of this proposal is that the following two
>     expressions are guaranteed to have exactly the same effect:
>
>     (FORMAT stream "~S" object)
>
>     (PRIN1 object stream)
>
>     Thus use or non-use of FORMAT becomes a purely stylistic matter.
>
> [Ditto FORMAT/~A and PRINC.]
>
> Thus while most people do indeed use FORMAT most of the time, some
> prefer to use PRINC, PRIN1, WRITE (plus keywords), etc., because
> it feels "more Lisp-y" to them. And sometimes you just need to do
> something that simply isn't convenient or even possible with FORMAT,
> so you handle it by wrapping however complex CL code you need around
> the functional printer interface PRINC/PRIN1/WRITE [and, in extremis,
> WRITE-CHAR], possibly even using the PPRINT-xxx functions explicitly.
>

The correspondence between FORMAT/~S and PRIN1, FORMAT~A and PRINC
should have made the correspondence between connection between FORMAT
and the pretty printer more obvious, but it wasn't. Thanks for the
info.

Tom
From: Kaz Kylheku
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <20081112201513.458@gmail.com>
On 2008-11-13, Rob Warnock <····@rpw3.org> wrote:
> Thomas M. Hermann <··········@gmail.com> wrote:
> +---------------
>| Francogrex <······@grex.org> wrote:
>| > (setf z (loop for i from 1 to 100 collect i))
>| > (setf test nil)
>| > (dotimes (i (length z))
>| >   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
> ...
>| I bestow upon you the award for the most redundant steps per line
>| of code. First, let's generate your list of odd numbers correctly.
>| 
>| (loop for number from 1 to 100
>|    when (oddp number) collect number)
> +---------------
>
> Oh, *puh-LEEZE!*  You can do better than that!  ;-}  ;-}
>
>    (loop for number from 1 to 100 by 2 collect number)

Ooh, that requires proving. 

Base case: 1 is odd, by inspection.

Inductive hypothesis: if k is odd, then k + 2 is odd.

Oddity is defined as having a residue modulo 2. Numbers that differ by two are
congruent modulo 2; if one is odd, the other must be.

Right, good! I vouch for the obfuscated rewrite.
From: William James
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <3b2ac125-d93b-4d3e-813e-cf53cc5f21a0@x16g2000prn.googlegroups.com>
On Nov 12, 10:23 pm, Kaz Kylheku <········@gmail.com> wrote:
> On 2008-11-13, Rob Warnock <····@rpw3.org> wrote:
>
>
>
>
>
> > Thomas M. Hermann <··········@gmail.com> wrote:
> > +---------------
> >| Francogrex <······@grex.org> wrote:
> >| > (setf z (loop for i from 1 to 100 collect i))
> >| > (setf test nil)
> >| > (dotimes (i (length z))
> >| >   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
> > ...
> >| I bestow upon you the award for the most redundant steps per line
> >| of code. First, let's generate your list of odd numbers correctly.
> >|
> >| (loop for number from 1 to 100
> >|    when (oddp number) collect number)
> > +---------------
>
> > Oh, *puh-LEEZE!*  You can do better than that!  ;-}  ;-}
>
> >    (loop for number from 1 to 100 by 2 collect number)
>
> Ooh, that requires proving.
>
> Base case: 1 is odd, by inspection.
>
> Inductive hypothesis: if k is odd, then k + 2 is odd.
>
> Oddity is defined as having a residue modulo 2. Numbers that differ by two are
> congruent modulo 2; if one is odd, the other must be.
>
> Right, good! I vouch for the obfuscated rewrite.

Even the easiest things are hard for those who
perversely becloud their minds with COBOL Lisp.
From: William James
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <c6a6b7f6-2536-4aaa-8860-865c8e8423de@d36g2000prf.googlegroups.com>
On Nov 12, 8:02 pm, ····@rpw3.org (Rob Warnock) wrote:
> Thomas M. Hermann <··········@gmail.com> wrote:
> +---------------| Francogrex <······@grex.org> wrote:
>
> | > (setf z (loop for i from 1 to 100 collect i))
> | > (setf test nil)
> | > (dotimes (i (length z))
> | >   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
> ...
> | I bestow upon you the award for the most redundant steps per line
> | of code. First, let's generate your list of odd numbers correctly.
> |
> | (loop for number from 1 to 100
> |    when (oddp number) collect number)
> +---------------
>
> Oh, *puh-LEEZE!*  You can do better than that!  ;-}  ;-}
>
>    (loop for number from 1 to 100 by 2 collect number)

Yeah, that's about how you'd do it in BASIC.

Ruby:

(1..100).select{|n| n % 2 > 0}
From: Raffael Cavallaro
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <gflhkn$ard$1@aioe.org>
On 2008-11-14 21:06:33 -0500, William James <·········@yahoo.com> said:

> Yeah, that's about how you'd do it in BASIC.
> 
> Ruby:
> 
> (1..100).select{|n| n % 2 > 0}

Dont' you mean:

(1..100).select{|n| n % 2 > 0}%&?$ CARRIER LOST
From: Kaz Kylheku
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <20081114232402.149@gmail.com>
On 2008-11-15, Raffael Cavallaro
<················@pas-d'espam-s'il-vous-plait-mac.com> wrote:
> On 2008-11-14 21:06:33 -0500, William James <·········@yahoo.com> said:
>
>> Yeah, that's about how you'd do it in BASIC.
>> 
>> Ruby:
>> 
>> (1..100).select{|n| n % 2 > 0}
>
> Dont' you mean:
>
> (1..100).select{|n| n % 2 > 0}%&?$ CARRIER LOST

Haha, they should introduce the +++ token, with the 1 second pause before and
after. In the repl, the pauses could be encoded literally as pauses.
From: Frank Buss
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <pbi9zxb6rznb$.4hhnvn64tvq9$.dlg@40tude.net>
William James wrote:

> Ruby:
> 
> (1..100).select{|n| n % 2 > 0}

It is even more readable (for mathematicians) in Haskell:

[x | x <- [1..100], odd x]

Now reverse:

reverse [x | x <- [1..100], odd x]

But you can do it without list comprehensions, too, which is more readable
for programmers and even shorter than the Ruby version:

reverse $ filter odd [1..100]

For a more Lisp-like output, in one line, you'll need some more magic. The
full program:

import List
odds = reverse $ filter odd [1..100]
line x = putStr $ concat $ intersperse " " $ map show x
main = line odds

With the help of some nice code from Haskell-cafe:

split n = unfoldr $ \xs -> case xs of
  [] -> Nothing
  _  -> Just (splitAt n xs)

you can do some pretty printing, e.g. 20 numbers per line:

putStr $ unlines $ map line $ split 20 odds
99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61
59 57 55 53 51 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21
19 17 15 13 11 9 7 5 3 1

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: André Thieme
Subject: Re: Printing introduces newlines
Date: 
Message-ID: <gh76mo$jpv$1@news.motzarella.org>
William James schrieb:
> On Nov 12, 8:02 pm, ····@rpw3.org (Rob Warnock) wrote:
>> Thomas M. Hermann <··········@gmail.com> wrote:
>> +---------------| Francogrex <······@grex.org> wrote:
>>
>> | > (setf z (loop for i from 1 to 100 collect i))
>> | > (setf test nil)
>> | > (dotimes (i (length z))
>> | >   (when (oddp (nth i z)) (setf test (append (list (nth i z)) test))))
>> ...
>> | I bestow upon you the award for the most redundant steps per line
>> | of code. First, let's generate your list of odd numbers correctly.
>> |
>> | (loop for number from 1 to 100
>> |    when (oddp number) collect number)
>> +---------------
>>
>> Oh, *puh-LEEZE!*  You can do better than that!  ;-}  ;-}
>>
>>    (loop for number from 1 to 100 by 2 collect number)
> 
> Yeah, that's about how you'd do it in BASIC.
> 
> Ruby:
> 
> (1..100).select{|n| n % 2 > 0}


In Clojure (Lisp) it�s much easier:

(filter odd? (range 100))
or even
(range 1 101 2)


Andr�
-- 
Lisp is not dead. It�s just the URL that has changed:
http://clojure.org/