f

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

```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
``` 0  CAI
10/7/2015 7:17:35 AM comp.lang.lisp  16861 articles. 5 followers. 6 Replies 3738 Views Similar Articles

[PageSpeed] 13

```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
``` 0  Helmut
10/7/2015 8:33:47 AM
```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

(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.informatimago.com/
“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
``` 0  Pascal
10/7/2015 12:29:04 PM
```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
``` 0  WJ
10/7/2015 7:49:28 PM
```This is the answer I got from Quora :

It defines a function, called enigma, which takes one parameter.  It return=
s the result of an 'and' operation.  The first argument to the 'and' is (no=
t (null x)), meaning, if x is null, then the result of the 'and' (and the r=
esult 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 functi=
on returns true.  Otherwise, the function returns the second argument to th=
e 'or', that is, (enigma (cdr x)).

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

On Thursday, October 8, 2015 at 3:51:36 AM UTC+8, WJ wrote:
> Helmut Eller wrote:
>=20
> > On Wed, Oct 07 2015, CAI GENGYANG wrote:
> >=20
> > > What's this ?
> > >=20
> > >=20
> > > (defun enigma (x)=20
> > >    (and (not (null x))
> > >            (or (null (car x))=20
> > >                 (enigma (cdr x)))))
> >=20
> > Bad taste?  It returns T if the list X contains nil as an element.  It
> > would be clearer to write (some #'null x).
>=20
> Gauche Scheme:
>=20
> 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)
>=20
> --=20
> [Jesse Jackson] would spit into the food of white patrons he hated and th=
en
> smilingly serve it to them.  He did this, he said, "because it gave me
> psychological gratification." -- Life Magazine, 1969-11-29
``` 0  CAI
10/8/2015 6:39:42 AM
```CAI GENGYANG <gengyangcai@gmail.com> writes:

> This is the answer I got from Quora :

Hence you learned nothing.

--
__Pascal Bourguignon__                 http://www.informatimago.com/
“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
``` 0  Pascal
10/8/2015 1:17:36 PM
```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.informatimago.com/
“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
``` 0  Pascal
10/8/2015 1:18:57 PM