From: Sungwoo, Lim
Subject: Q: 'cond' with duplicated result.
Date: 
Message-ID: <131220001250070620%sungwoo@cad.strath.ac.uk>
Hello,

When I run following example, I got the result of last clause twice.
-----------------------------------------------------------------
4 > (cond ((zerop k) (print "first-test") (print "yes"))
          ((= (+ k 1) 1) (print "second-test") (print "no")))

"first test" 
"yes" 
"yes"
4 > 
-----------------------------------------------------------------
This give me wrong results when I try to count an implementation number
of time. I expect the number as 'two' times, but this gives me 'three'
times.
So, how can I get only one result?
Thanks for your help.

Sungwoo

From: Dr Nick Levine
Subject: Re: Q: 'cond' with duplicated result.
Date: 
Message-ID: <3A3773F0.F2B1844A@anglia.ac.uk>
(print "yes") on its own at the top level would effectively print "yes"
twice: once from the print function and then again from the return value
(print returns its (first) argument) being printed by the
read-eval-print loop.


CL-USER 71 > (print "for example")

"for example" 
"for example"

CL-USER 72 > (trace print)
(PRINT GENERATE-BODY)

CL-USER 73 > (print "for example")
0 PRINT > ("for example")

"for example" 
0 PRINT < ("for example")
"for example"

CL-USER 74 > 



"Sungwoo, Lim" wrote:
> 
> Hello,
> 
> When I run following example, I got the result of last clause twice.
> -----------------------------------------------------------------
> 4 > (cond ((zerop k) (print "first-test") (print "yes"))
>           ((= (+ k 1) 1) (print "second-test") (print "no")))
> 
> "first test"
> "yes"
> "yes"
> 4 >
> -----------------------------------------------------------------
> This give me wrong results when I try to count an implementation number
> of time. I expect the number as 'two' times, but this gives me 'three'
> times.
> So, how can I get only one result?
> Thanks for your help.
> 
> Sungwoo
From: Friedrich Dominicus
Subject: Re: Q: 'cond' with duplicated result.
Date: 
Message-ID: <87lmtkh4eo.fsf@frown.here>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> Hello,
> 
> When I run following example, I got the result of last clause twice.
> -----------------------------------------------------------------
> 4 > (cond ((zerop k) (print "first-test") (print "yes"))
>           ((= (+ k 1) 1) (print "second-test") (print "no")))

The last value of a cond clause is returned. You just want to have the
output, that means just the side-effect, please try
(defun test (k)
      (cond ((zerop k) (print "first-test") (print "yes"))
      ((= (+ k 1) 1) (print "second-test") (print "no"))) 
    (values))


(values) is there to return multiple values and/or none if used
without parameter

BTW you will never see the second clause because it just will hold for
k = 0 which obviously is covered by the first cond clause

Regards
Friedrich
From: Sungwoo, Lim
Subject: Re: Q: 'cond' with duplicated result.
Date: 
Message-ID: <131220001416181102%sungwoo@cad.strath.ac.uk>
In article <··············@frown.here>, Friedrich Dominicus
<·····@q-software-solutions.com> wrote:


> BTW you will never see the second clause because it just will hold for
> k = 0 which obviously is covered by the first cond clause

Yes, that is what I intend to do. =)
Thanks alot.

Sungwoo
From: Frode Vatvedt Fjeld
Subject: Re: Q: 'cond' with duplicated result.
Date: 
Message-ID: <2h1yvcv4pp.fsf@dslab7.cs.uit.no>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> When I run following example, I got the result of last clause twice.
> -----------------------------------------------------------------
> 4 > (cond ((zerop k) (print "first-test") (print "yes"))
>           ((= (+ k 1) 1) (print "second-test") (print "no")))

There's a difference between "getting a result" and "printing a
result". PRINT does both; it prints and then returns its argument.

> (print "hello world")
"hello world"  <- print output
"hello world"  <- return value from the form evaluation


> (list (print "hello world"))
"hello world"   <- print output
("hello world") <- return value from the form evaluation


If you want to return the string "yes" or the string "no" in your COND
form above, remember that strings are self-evaluating objects, so just
insert the string there, without printing them:

  (cond ((zerop k) "yes")
        ((= (+ k 1) 1) "no"))

Note that this form is functionally equivalent to your origninal form,
but your form has some extra side-effects.

Finally, it would probably be in better lisp style to use symbols:

    (cond ((zerop k) 'yes)
          ((= (+ k 1) 1) 'no))

BTW, the two test-forms also seem to be equivalent, but I assume you
know that.

-- 
Frode Vatvedt Fjeld
From: Sungwoo, Lim
Subject: Re: Q: 'cond' with duplicated result.
Date: 
Message-ID: <131220001415137195%sungwoo@cad.strath.ac.uk>
In article <··············@dslab7.cs.uit.no>, Frode Vatvedt Fjeld
<······@acm.org> wrote:


Thanks, =)
Sungwoo