From: Joachim Rektenwald
Subject: Print statement is executed twice
Date: 
Message-ID: <e00920f5-e0d5-43a4-a7ce-1e81efd68163@e4g2000hsg.googlegroups.com>
Hello,

first of all: this is my first LISP program ever, I have started
learning LISP a few hours ago, so my question may sound very simple. I
hope you can help me :)

I have written a program which is supposed to recursively call a
function which accepts a list of integer numbers, and returns true if
all the numbers in the list are odd ones, if there is one or more even
numbers, it returns NIL.

My program seems to work, but I am wondering why my LISP IDE (Ufasoft
Common LISP IDE, you are welcome to propose other free LISP IDEs for
Windows to me) prints the last expression twice. Below is my program
listing, and the output I see in my terminal, maybe you can help me
what's wrong with that print-statement.

My program:
============================================
(setq myList (list 3 5 7 9 11 15 80 111 127))

(defun rec-oddp(x)
 (print x)
 (if x
       (if (evenp (pop x)) NIL (rec-oddp x))
       t
 )
)

(if (rec-oddp myList) (print "only odd numbers") (print "even numbers
present") )
============================================

My Terminal output:
============================================
(3 5 7 9 11 15 80 111 127)
[5]>
REC-ODDP
[6]>
(3 5 7 9 11 15 80 111 127)
(5 7 9 11 15 80 111 127)
(7 9 11 15 80 111 127)
(9 11 15 80 111 127)
(11 15 80 111 127)
(15 80 111 127)
(80 111 127)
"even numbers present"
"even numbers present"
[7]>
============================================

From: Slobodan Blazeski
Subject: Re: Print statement is executed twice
Date: 
Message-ID: <1b965500-f624-4441-b45a-d04f0a730a6d@w73g2000hsf.googlegroups.com>
On Nov 15, 2:49 am, Joachim Rektenwald <············@gmx.de> wrote:
> Hello,
>
> first of all: this is my first LISP program ever, I have started
> learning LISP a few hours ago, so my question may sound very simple. I
> hope you can help me :)
>
> I have written a program which is supposed to recursively call a
> function which accepts a list of integer numbers, and returns true if
> all the numbers in the list are odd ones, if there is one or more even
> numbers, it returns NIL.
>
> My program seems to work, but I am wondering why my LISP IDE (Ufasoft
> Common LISP IDE, you are welcome to propose other free LISP IDEs for
> Windows to me)
Take a look at this thread:
http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/9d0080bd3be9ddea/9e0ab129d28fb21b?lnk=gst&q=shameless+selfquoting+slobodan#9e0ab129d28fb21b
>prints the last expression twice.
It doesn't print it it twice:
First one is the real printing - the side effect, the second one is
the return value.
Take a look at above thread and pick some good book. Also there are
some video tutorials.
I don't know Ansi compatible is Ufasoft, it might be good.But rather
choose some implementation that is up to date .

happy lisping
Slobodan

>Below is my program
> listing, and the output I see in my terminal, maybe you can help me
> what's wrong with that print-statement.
>
> My program:
> ============================================
> (setq myList (list 3 5 7 9 11 15 80 111 127))
>
> (defun rec-oddp(x)
>  (print x)
>  (if x
>        (if (evenp (pop x)) NIL (rec-oddp x))
>        t
>  )
> )
>
> (if (rec-oddp myList) (print "only odd numbers") (print "even numbers
> present") )
> ============================================
>
> My Terminal output:
> ============================================
> (3 5 7 9 11 15 80 111 127)
> [5]>
> REC-ODDP
> [6]>
> (3 5 7 9 11 15 80 111 127)
> (5 7 9 11 15 80 111 127)
> (7 9 11 15 80 111 127)
> (9 11 15 80 111 127)
> (11 15 80 111 127)
> (15 80 111 127)
> (80 111 127)
> "even numbers present"
> "even numbers present"
> [7]>
> ============================================
From: Pascal Costanza
Subject: Re: Print statement is executed twice
Date: 
Message-ID: <5q2p3lFt0saaU1@mid.individual.net>
Joachim Rektenwald wrote:
> Hello,
> 
> first of all: this is my first LISP program ever, I have started
> learning LISP a few hours ago, so my question may sound very simple. I
> hope you can help me :)

What book are you using?

> I have written a program which is supposed to recursively call a
> function which accepts a list of integer numbers, and returns true if
> all the numbers in the list are odd ones, if there is one or more even
> numbers, it returns NIL.
> 
> My program seems to work, but I am wondering why my LISP IDE (Ufasoft
> Common LISP IDE, you are welcome to propose other free LISP IDEs for
> Windows to me) prints the last expression twice. Below is my program
> listing, and the output I see in my terminal, maybe you can help me
> what's wrong with that print-statement.
> 
> My program:
> ============================================
> (setq myList (list 3 5 7 9 11 15 80 111 127))
> 
> (defun rec-oddp(x)
>  (print x)
>  (if x
>        (if (evenp (pop x)) NIL (rec-oddp x))
>        t
>  )
> )
> 
> (if (rec-oddp myList) (print "only odd numbers") (print "even numbers
> present") )
> ============================================
> 
> My Terminal output:
> ============================================
> (3 5 7 9 11 15 80 111 127)
> [5]>
> REC-ODDP
> [6]>
> (3 5 7 9 11 15 80 111 127)
> (5 7 9 11 15 80 111 127)
> (7 9 11 15 80 111 127)
> (9 11 15 80 111 127)
> (11 15 80 111 127)
> (15 80 111 127)
> (80 111 127)
> "even numbers present"
> "even numbers present"

The last output is not printed twice, even if it looks like it was.

print returns whatever was printed as a value. For example, (+ 4 5) 
returns 9 as a value. (print (+ 4 5)) prints 9 _and_ returns 9 as a value.

In the listener, you always see the value of the expression you just 
evaluated. So what you see is the result of the print action, and the 
return value of the print function.

I hope this helps.

BTW, you don't need recursion to get what you want. Just do this:

(every #'oddp numbers)

This returns true when all numbers are odd, and false otherwise.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Raffael Cavallaro
Subject: Re: Print statement is executed twice
Date: 
Message-ID: <2007111510451643658-raffaelcavallaro@pasdespamsilvousplaitmaccom>
On 2007-11-15 06:33:08 -0500, Pascal Costanza <··@p-cos.net> said:

> The last output is not printed twice, even if it looks like it was.
> 
> print returns whatever was printed as a value. For example, (+ 4 5) 
> returns 9 as a value. (print (+ 4 5)) prints 9 _and_ returns 9 as a 
> value.
> 
> In the listener, you always see the value of the expression you just 
> evaluated. So what you see is the result of the print action, and the 
> return value of the print function.

The OP can even see this explicitly by executing:

? (let ((first-return-value (print "first print"))
        (second-return-value (print "second print")))
     (list 'return-values first-return-value second-return-value))

"first print"
"second print"
(RETURN-VALUES "first print" "second print")
?