From: Wolfgang Wagner
Subject: COND
Date: 
Message-ID: <7nidtp$3ga$1@alpha5.wagnernet>
Hi,

Right now I'm learning Common Lisp. I have problems to
understand the COND-function.
How works the way of logical differencing of this function ?
And what result works out under different circumstances ?

Have You a teaching example ?

Thanks.

Bye...
	Wolfgang
-- 
- ·······@minima.rhein.de  --  ·······@mueller-formenbau.de
-----------------------------------------------------------
- Kleinweich - sehen - kopieren - patentieren - verkaufen - 

From: Kent M Pitman
Subject: Re: COND
Date: 
Message-ID: <sfw6736deq1.fsf@world.std.com>
Wolfgang Wagner <·······@minima.rhein.de> writes:

> Hi,
> 
> Right now I'm learning Common Lisp. I have problems to
> understand the COND-function.
> How works the way of logical differencing of this function ?
> And what result works out under different circumstances ?
> 
> Have You a teaching example ?

(COND ((f1 a) 1)
      ((f2 a) 2)
      ((f3 a) 3)
      (t 4))

is the same as

(IF (f1 a)
    1
    (IF (f2 a)
        2
        (IF (f3 a)
            3
            4)))

COND is just a way of accomodating a series of tests without
increasing indentation level.  It provides the same service
as "elseif" in other languages.  That is, in other languages
one prefers to write a series of conditionals "at the same level"
by writing:

     if f1(a) then 1
 elseif f2(a) then 2
 elseif f3(a) then 3
 else              4

rather than as nested conditionals like:

 if f1(a) 
 then 1
 else if f2(a)
         then 2
         else if f3(a) 
              then 3
              else 4

Lisp uses COND rather than IF to provide the same service
(avoiding nested IF's and allowing instead a series of
alternatives, each guarded by a test, to be tried in sequence).

Does this help?

If you want to try the above examples, you might try
defining the f1, f2, and f3 using:
 (defun f1 (x) (numberp x))
 (defun f2 (x) (null x))
 (defun f3 (x) (symbolp x))
and then set up A with different values as in
     (setq a 17)
OR   (setq a 'fred)
OR   (setq a nil)
OR   (setq a "some text")
From: Wolfgang Wagner
Subject: Re: COND
Date: 
Message-ID: <7nklom$msj$1@alpha5.wagnernet>
Kent M Pitman <······@world.std.com> wrote: 
> Wolfgang Wagner <·······@minima.rhein.de> writes:

>> Right now I'm learning Common Lisp. I have problems to
>> understand the COND-function.

> (COND ((f1 a) 1)
>       ((f2 a) 2)
>       ((f3 a) 3)
>       (t 4))

> is the same as

> (IF (f1 a)
>     1
>     (IF (f2 a)
>         2
>         (IF (f3 a)
>             3
>             4)))

Must fx be a function ?  If not, is the test a logical OR, AND, 
or a "=" ?

Thanks.

Bye...
	Wolfgang
-- 
- ·······@minima.rhein.de  --  ·······@mueller-formenbau.de
-----------------------------------------------------------
From: Pierre R. Mai
Subject: Re: COND
Date: 
Message-ID: <87k8rmuh9s.fsf@orion.dent.isdn.cs.tu-berlin.de>
Wolfgang Wagner <·······@minima.rhein.de> writes:

> Kent M Pitman <······@world.std.com> wrote: 
> > Wolfgang Wagner <·······@minima.rhein.de> writes:
> 
> >> Right now I'm learning Common Lisp. I have problems to
> >> understand the COND-function.
> 
> > (COND ((f1 a) 1)
> >       ((f2 a) 2)
> >       ((f3 a) 3)
> >       (t 4))
> 
> > is the same as
> 
> > (IF (f1 a)
> >     1
> >     (IF (f2 a)
> >         2
> >         (IF (f3 a)
> >             3
> >             4)))
> 
> Must fx be a function ?  If not, is the test a logical OR, AND, 
> or a "=" ?

Well, in the example above fx must be a function (or another operator,
like a macro or special-operator), given that each form (fx a) is an
application.  But generally, you can replace (fx a) by any form you
want.  I'll quote the relevant parts of the HyperSpec entry on cond
here.  In any case, I would highly recommend, that you download the
HyperSpec (which is a HTML-ified variant of the ANSI Common Lisp
standard), from http://www.harlequin.com/books/HyperSpec/

Taken together with a good text-book on Common Lisp (like Graham's
ANSI Common Lisp), the HyperSpec (which has very useful links at the
right places, such as the references to "form" and "progn" below,
which aren't reproduced here) will make understanding CL much easier,
IMHO.  Don't be frightened by the fact that the text of the HyperSpec
is the same as in the language standard.  The ANSI Common Lisp
standard is very readable indeed, unlike most other language
standards.

<quote>
Macro COND


Syntax:


cond {clause}* => result*



clause::= (test-form form*) 


Arguments and Values:


test-form---a form.

forms---an implicit progn.

results---the values of the forms in the first clause whose test-form yields
true, or the primary value of the test-form if there are no forms in that
clause, or else nil if no test-form yields true.


Description:


cond allows the execution of forms to be dependent on test-form.

Test-forms are evaluated one at a time in the order in which they are given in
the argument list until a test-form is found that evaluates to true.

If there are no forms in that clause, the primary value of the test-form is
returned by the cond form. Otherwise, the forms associated with this test-form
are evaluated in order, left to right, as an implicit progn, and the values
returned by the last form are returned by the cond form.

Once one test-form has yielded true, no additional test-forms are
evaluated. If no test-form yields true, nil is returned.
</quote>

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Wolfgang Wagner
Subject: Re: COND
Date: 
Message-ID: <7nnn2b$2d9$1@alpha5.wagnernet>
Pierre R. Mai <····@acm.org> wrote: 

>> >> Right now I'm learning Common Lisp. I have problems to
>> >> understand the COND-function.
> [.......]
> want.  I'll quote the relevant parts of the HyperSpec entry on cond
> here.  In any case, I would highly recommend, that you download the
> HyperSpec (which is a HTML-ified variant of the ANSI Common Lisp
> standard), from http://www.harlequin.com/books/HyperSpec/

Ok. Thanks to All for Help !

Bye...
	Wolfgang
-- 
- ·······@minima.rhein.de  --  ·······@mueller-formenbau.de
-----------------------------------------------------------