[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

A function with 'and' , 'not' , 'null' , 'car' and 'cdr'

gengyangcai

10/7/2015 7:18:00 AM

What's this ?


(defun enigma (x)
(and (not (null x))
(or (null (car x))
(enigma (cdr x)))))




"I suppose I should learn Lisp, but it seems so foreign."

- Paul Graham, Nov 1983
6 Answers

Helmut Eller

10/7/2015 8:34:00 AM

0

On Wed, Oct 07 2015, CAI GENGYANG wrote:

> What's this ?
>
>
> (defun enigma (x)
> (and (not (null x))
> (or (null (car x))
> (enigma (cdr x)))))

Bad taste? It returns T if the list X contains nil as an element. It
would be clearer to write (some #'null x).

Helmut

Pascal J. Bourguignon

10/7/2015 12:29:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> What's this ?
>
> (defun enigma (x)
> (and (not (null x))
> (or (null (car x))
> (enigma (cdr x)))))

While you don't need to be a mathematician to be a programmer, you still
need to know boolean logic and counting.

If you cannot understand:

enigma(x) = (¬x=â??) â?§ ( (¬car(x)=â??) â?¨ enigma(cdr(x)) )

then you cannot be a programmer.

So go learn some boolean logic.



But then, you may object that AND and OR are not exactly like the
boolean operators, since they are "short-cutting", and have more complex
evaluation rules for their arguments and results.

So, as a form containing macros, you can expand it, either using M-x
slime-macroexpand-all RET or when you know the semantics of lisp, doing
it yourself by hand or in your head. Macroexpansion depends on the
implementation so it might be better to know the semantics and do it
yourself, since the implementation expansion can contain implementation
specific function calls.

But in this case you obtain, in ccl at least, something usable:

(and (not (null x))
(or (null (car x))
(enigma (cdr x))))

expands to:

(if (not (null x))
(let ((#1=#:g7399 (null (car x))))
(if #1#
#1#
(enigma (cdr x)))))

We may add the implicit NIL result:

(if (not (null x))
(let ((#1=#:g7399 (null (car x))))
(if #1#
#1#
(enigma (cdr x))))
nil)

Then, depending on how you read it, you may simplify not null:

(if x
(let ((#1=#:g7399 (null (car x))))
(if #1#
#1#
(enigma (cdr x))))
nil)


or if not:

(if (null x)
nil
(let ((#1=#:g7399 (null (car x))))
(if #1#
#1#
(enigma (cdr x)))))

Knowing the results of null, you can substitute the #1# in the then
branch:

(if (null x)
nil
(let ((#1=#:g7399 (null (car x))))
(if #1#
T
(enigma (cdr x)))))

And then remove the temporary variable:

(if (null x)
nil
(if (null (car x))
T
(enigma (cdr x)))))

You may now replace the embedded IFs by a COND that may be easier to
read:

(cond ((null x) nil)
((null (car x)) T)
(t (enigma (cdr x))))

and so it's clear what it means:

the result is: NIL if the list X is empty,
T if NIL is the first element of the list X,
(enigma (cdr x)) otherwise.

Or, said without the recursion, this function is a predicate indicating
the presence of NIL in the list.

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

William James

10/7/2015 7:49:00 PM

0

Helmut Eller wrote:

> On Wed, Oct 07 2015, CAI GENGYANG wrote:
>
> > What's this ?
> >
> >
> > (defun enigma (x)
> > (and (not (null x))
> > (or (null (car x))
> > (enigma (cdr x)))))
>
> Bad taste? It returns T if the list X contains nil as an element. It
> would be clearer to write (some #'null x).

Gauche Scheme:

gosh> (member '() '(a b c d))
#f
gosh> (member '() '(a b () c d))
(() c d)
gosh> (member #f '(a b c d))
#f
gosh> (member #f '(a b #f c d))
(#f c d)

--
[Jesse Jackson] would spit into the food of white patrons he hated and then
smilingly serve it to them. He did this, he said, "because it gave me
psychological gratification." -- Life Magazine, 1969-11-29

gengyangcai

10/8/2015 6:40:00 AM

0

This is the answer I got from Quora :

It defines a function, called enigma, which takes one parameter. It returns the result of an 'and' operation. The first argument to the 'and' is (not (null x)), meaning, if x is null, then the result of the 'and' (and the result of 'enigma') is false.

If x is not null, the second argument to 'and' is evaluated, and returned. This is an 'or' expression. The first argument to the 'or' is (null (car x)). If this is true (and the first argument to 'and' is true), the function returns true. Otherwise, the function returns the second argument to the 'or', that is, (enigma (cdr x)).

So: If x is null, the result is false; if x is not null, and (car x) is null, the result is true. If x is not null, and (car x) is not null, the result is (enigma (cdr x)).






On Thursday, October 8, 2015 at 3:51:36 AM UTC+8, WJ wrote:
> Helmut Eller wrote:
>
> > On Wed, Oct 07 2015, CAI GENGYANG wrote:
> >
> > > What's this ?
> > >
> > >
> > > (defun enigma (x)
> > > (and (not (null x))
> > > (or (null (car x))
> > > (enigma (cdr x)))))
> >
> > Bad taste? It returns T if the list X contains nil as an element. It
> > would be clearer to write (some #'null x).
>
> Gauche Scheme:
>
> gosh> (member '() '(a b c d))
> #f
> gosh> (member '() '(a b () c d))
> (() c d)
> gosh> (member #f '(a b c d))
> #f
> gosh> (member #f '(a b #f c d))
> (#f c d)
>
> --
> [Jesse Jackson] would spit into the food of white patrons he hated and then
> smilingly serve it to them. He did this, he said, "because it gave me
> psychological gratification." -- Life Magazine, 1969-11-29

Pascal J. Bourguignon

10/8/2015 1:18:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> This is the answer I got from Quora :

Hence you learned nothing.


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Pascal J. Bourguignon

10/8/2015 1:19:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> This is the answer I got from Quora :

What was expected was:

- here is what I learned of boolean logic.
- here is what I learned about CL AND and OR.
- here is what I understood about the enigma function.


Instead you're doing the job of computers and robot (fetching an answer
from quora). Needless to say, there's not much of a professionnal
future for you, since robots can ALREADY do what you doâ?¦


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk