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
2888 Views

Similar Articles

[PageSpeed] 44

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

Similar Artilces:

'is not' or '!='
A newbie question to you; what is the difference between statements like: if x is not None: and if x != None: Without any context, which one should be preferred? IMHO, the latter is more readable. On 2014-08-18 21:35, ElChino wrote: > A newbie question to you; what is the difference between statements > like: > if x is not None: > and > if x != None: > > Without any context, which one should be preferred? > IMHO, the latter is more readable. > "x == y" tells you whether x and y refer to objects that are equal. "x is y&qu...

'^=' and '~='?
Hello, What is the difference between '^=' and '~='? Thanks, Duckhye ...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal''''''''''''''
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; _____________________________________________________ SELECT "criteria" FROM "criteria" WHERE "criteria" UPDATE/APPEND "field selections" RecordSource "qryExample" = above text strings" _______________________________________________________________________ When i am running a SELECT query in this manner it works fine with no problems, and accepts the values of specified linked for...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal'''''''''''''' #2
Hi, Thanks for ur help there HJ. I know how to do the tasks you specified there. I would like for the update query to use field values from some of the fields on the form (frmInvoices) such as InvoiceNumber, DateFrom, DateTo. My problem is that an append/update query can't find the values in the open Form (frmInvoices) when I specify them as; [Forms]![frmInvoices]![InvoiceNumber] a select query has no problem finding the field values on a form. please help. Aaron Hi Aaron, Could you post the entire code that you are having trouble with? Now it is not possible to see what goes wron...

if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.') Any shorter ?
Hi, there. =20 I'm just curious if it ever dawned on anybody how to abbreviate this line : if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.')=20 =20 Igor Kurbeko Clinical Programmer Analyst 678 336 4328 ikurbeko@atherogenics.com =20 no brain no pain =20 how about: if not (str_mo in ('','.') or str_da in ('','.') or str_yy in ('','.')) OR if not (missing(str_mo) or missing(str_da) or missing(str_yy)) Eric On 22 Oct 03 21:13:37 GMT, ikurbeko@ATHER...

Re: if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.') Any shorter ?
OR you could use ARRAY data new; set old; array igor $(*) str_mo str_da str_yr; do over igor; if igor ~in (' ','.') then do; end; run; Prasad Ravi Igor Kurbeko <ikurbeko@ATHEROGENIC To: SAS-L@LISTSERV.UGA.EDU S.COM> cc: Sent by: "SAS(r) Subject: if str_mo not in ('','.') and str_da not in ('','.') and str_yy ... error: expected '=', ',', ';', 'asm' or '__attrib Hi I'm trying to compile an ADC Driver & come acrosss the following error. I've no experience writing drivers before, and hence have no clue how to fix it. Hope someone out there has encountered the problem & suggesst a fix for the same. The Error is I get is : qadc.c: At top level: qadc.c:97: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'qadc_read' make: *** [qadc.o] Error 1 [root@localhost qadc]# ########################################################################### ADC Driver Code ####################... error: expected '=', ',', ';', 'asm' or '__attrib Hi I'm trying to compile an ADC Driver & come acrosss the following error. I've no experience writing drivers before, and hence have no clue how to fix it. Hope someone out there has encountered the problem & suggesst a fix for the same. The Error is I get is : qadc.c: At top level: qadc.c:97: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'qadc_read' make: *** [qadc.o] Error 1 [root@localhost qadc]# ########################################################################### ADC Driver Code ##... Re: '^=' and '~='? Duckhye, According to the doc ( http://xrl.us/befwjx ) they, and one other set of characters, and the mnemonic 'NE' all represent 'NOT EQUAL'. Art ------- On Wed, 11 Feb 2009 16:52:40 -0600, Duck-Hye Yang <dyang@CHAPINHALL.ORG> wrote: >Hello, >What is the difference between '^=' and '~='? > >Thanks, >Duckhye ... replacement for '{' and '}' ? I am still playing around with what kind of syntax I would like to mark up my documents. Are there equivalent long substitutes for { and } when they are not used to describe arguments to functions? Something like \begin{group} and \end{group}. In other words, if I could force myself to write, say, \begin{group} \it ... \end{group} instead of {\it ... }, then I believe I could identify from the markup context what is an argument that belongs to a just invoked macro and what is text. {Of course, in this context, \textit{...} would be better.} No more ambiguity whether a in \myfunction{a} i... Replacing ',' with '.' Hello, I have a huge amount of numbers in a .txt file. The numbers are in the form 2,43252e+1. I need to replace the , with . How should I do this? I'd prefer some import method that does this during the import procedure. -Janne Hi, I guess you import the data as text and convert it then to numbers. Try 'strrep' before you convert the text to numbers. Tobias Jake the Snake schrieb: > Hello, > > I have a huge amount of numbers in a .txt file. The numbers are in the form 2,43252e+1. I need to replace the , with . How should I do this? I'd prefer some import method... Difference between 'is' and '==' Hey guys, this maybe a stupid question, but I can't seem to find the result anywhere online. When is the right time to use 'is' and when should we use '=='? Thanks alot~ mwql: >Hey guys, this maybe a stupid question, but I can't seem to find the >result anywhere online. When is the right time to use 'is' and when >should we use '=='? http://docs.python.org/ref/comparisons.html -- Ren� Pijlman mwql wrote: > Hey guys, this maybe a stupid question, but I can't seem to find the > result anywhere online. When is the right time to ... replacing '/' with '\\' I have a MKS ksh shell script running on a Windows box. In some scripts I have to convert a UNIX filename into a DOS filename, that is replace the '/' with '\\'. For example, replace /opt/siips/archive/text.dat with \\opt\\siips\\archive\\text.dat. I've tried using sed like$FILE='echo \$FILE|sed "s/\//\\\/g"' which got me \opt\siips\archive\text.dat. It's close but I still need the second \. Does anyone have any suggestion? Or tell me where I've gone wrong? Thanks -- Steffen On 9 Dec 2004 20:14:21 -0800, sajohn52@yahoo.com <sajohn52@yahoo....

difference between ',' and 'a,'
Small question. In gforth is there a difference between the words ',' and 'a,'? I'm thinking not, so perhaps another question, why have both ',' and 'a,'? Thanks Should be the same, in gforth: see , : , here cell allot ! ; ok see a, : , here cell allot ! ; ok On Friday, January 9, 2015 at 5:46:04 AM UTC-8, beeflo wrote: > Small question. In gforth is there a difference between the words ',' and 'a,'? > > I'm thinking not, so perhaps another question, why have both ',' and 'a,'...