[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

(numberp x) , (numberp y)

gengyangcai

10/5/2015 7:56:00 PM

What does a statement like (numberp x) or (numberp y) mean ?

37 Answers

Matthew Carter

10/5/2015 8:10:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> What does a statement like (numberp x) or (numberp y) mean ?
>

CL-USER> (documentation #'numberp 'function)
"Return true if OBJECT is a NUMBER, and NIL otherwise."

--
Matthew Carter (m@ahungry.com)
http://a...

Pascal J. Bourguignon

10/5/2015 10:38:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> What does a statement like (numberp x) or (numberp y) mean ?

There are no statements in lisp.

--
__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/5/2015 10:38:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> What does a statement like (numberp x) or (numberp y) mean ?

Read chapter 3.
http://www.lispworks.com/documentation/HyperSpec/Bo...

--
__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

Kaz Kylheku

10/6/2015 3:18:00 AM

0

On 2015-10-05, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> CAI GENGYANG <gengyangcai@gmail.com> writes:
>
>> What does a statement like (numberp x) or (numberp y) mean ?
>
> There are no statements in lisp.

It turns out that this isn't the case:

Macro PROG, PROG*

Syntax:

prog ({var | (var [init-form])}*) declaration* {tag | statement}*:

^^^^^^^^^

:)

Pascal J. Bourguignon

10/6/2015 11:03:00 AM

0

Kaz Kylheku <kaz@kylheku.com> writes:

> On 2015-10-05, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
>> CAI GENGYANG <gengyangcai@gmail.com> writes:
>>
>>> What does a statement like (numberp x) or (numberp y) mean ?
>>
>> There are no statements in lisp.
>
> It turns out that this isn't the case:
>
> Macro PROG, PROG*
>
> Syntax:
>
> prog ({var | (var [init-form])}*) declaration* {tag | statement}*:
>
> ^^^^^^^^^
>
> :)

Those are compound forms, which are expressions, not statements.


--
__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

gengyangcai

10/6/2015 11:39:00 AM

0

This is a program that defines a function that takes 2 arguments and returns the greater one.

(defun greater (x y)?
(if (and (numberp x) (numberp y))?
(if (> x y) x y)))

The first sentence is simple, it defines a a function called 'greater' that takes 2 arguments.

As for the 2nd sentence, numberp is a predicate that tests whether its argument is a number. So this line basically means "if both x and y" are numbers.

The last line takes the larger argument amongst the 2 and returns the greater one. I am having trouble with this one (why is the syntax structured the way it is?)

I know for example that
: (> (+ 2 5) 3) returns T because it means (2+5) = 7 > 3 = True.

but as for this line
: (if (> x y) x y)))

I am just confused as to why the syntax is structured the way it is. Maybe I am experiencing a brain freeze, why isn't this line simply (if (> x y)) ?



On Tuesday, October 6, 2015 at 7:02:50 PM UTC+8, informatimago wrote:
> Kaz Kylheku <kaz@kylheku.com> writes:
>
> > On 2015-10-05, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> >> CAI GENGYANG <gengyangcai@gmail.com> writes:
> >>
> >>> What does a statement like (numberp x) or (numberp y) mean ?
> >>
> >> There are no statements in lisp.
> >
> > It turns out that this isn't the case:
> >
> > Macro PROG, PROG*
> >
> > Syntax:
> >
> > prog ({var | (var [init-form])}*) declaration* {tag | statement}*:
> >
> > ^^^^^^^^^
> >
> > :)
>
> Those are compound forms, which are expressions, not statements.
>
>
> --
> __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/6/2015 11:57:00 AM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> This is a program that defines a function that takes 2 arguments and
> returns the greater one.
>
> (defun greater (x y)â?¨
> (if (and (numberp x) (numberp y))â?¨
> (if (> x y) x y)))
>
> The first sentence is simple, it defines a a function called 'greater'
> that takes 2 arguments.

It is wrong, ie. it will signal an error for some arguments:

cl-user> (greater #c(1 0) #c(0 1))
> Debug: The value #C(0 1) is not of the expected type real.
> While executing: (:internal swank::invoke-default-debugger), in process repl-thread(15).
> Type :POP to abort, :R for a list of available restarts.
> Type :? for other options.
1 > :q
; Evaluation aborted on #<type-error #x302002133D2D>.
cl-user>


> As for the 2nd sentence, numberp is a predicate that tests whether its
> argument is a number. So this line basically means "if both x and y"
> are numbers.
>
> The last line takes the larger argument amongst the 2 and returns the
> greater one. I am having trouble with this one (why is the syntax
> structured the way it is?)

Because this is the simpliest structure you can have for a ternary IF.

- you need to specify the operator: IF
- you need to specify the condition: (> x y)
- you need to specify what to return when the condition is true: x
- you need to specify what to return when the condition is false: y
- you don't need to specify useless characters such as ? or :
or useless keywords such as :condition :then :else.
- you don't need to use a different order.

Hence: (if (> x y) x y)


> I know for example that
> : (> (+ 2 5) 3) returns T because it means (2+5) = 7 > 3 = True.

You know false things. (> (+ 2 5) 3) doesn't return T. It _may_ return
T, but it may also return 7 or 0 or HELLO or "You should read the clhs already!"

> but as for this line
> : (if (> x y) x y)))
> I am just confused as to why the syntax is structured the way it
> is. Maybe I am experiencing a brain freeze, why isn't this line simply
> (if (> x y)) ?

Because (if (> x y)) is invalid syntax, the IF operator takes at least
two arguments.


--
__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

gengyangcai

10/6/2015 1:46:00 PM

0

Got it ...

(if (> x y) x y)))

if = IF operator that takes at least 2 statements
(> x y) = condition
x y = arguments

One must always obey the rules set by the Gods who created Lisp



On Tuesday, October 6, 2015 at 7:57:10 PM UTC+8, informatimago wrote:
> CAI GENGYANG <gengyangcai@gmail.com> writes:
>
> > This is a program that defines a function that takes 2 arguments and
> > returns the greater one.
> >
> > (defun greater (x y)?
> > (if (and (numberp x) (numberp y))?
> > (if (> x y) x y)))
> >
> > The first sentence is simple, it defines a a function called 'greater'
> > that takes 2 arguments.
>
> It is wrong, ie. it will signal an error for some arguments:
>
> cl-user> (greater #c(1 0) #c(0 1))
> > Debug: The value #C(0 1) is not of the expected type real.
> > While executing: (:internal swank::invoke-default-debugger), in process repl-thread(15).
> > Type :POP to abort, :R for a list of available restarts.
> > Type :? for other options.
> 1 > :q
> ; Evaluation aborted on #<type-error #x302002133D2D>.
> cl-user>
>
>
> > As for the 2nd sentence, numberp is a predicate that tests whether its
> > argument is a number. So this line basically means "if both x and y"
> > are numbers.
> >
> > The last line takes the larger argument amongst the 2 and returns the
> > greater one. I am having trouble with this one (why is the syntax
> > structured the way it is?)
>
> Because this is the simpliest structure you can have for a ternary IF.
>
> - you need to specify the operator: IF
> - you need to specify the condition: (> x y)
> - you need to specify what to return when the condition is true: x
> - you need to specify what to return when the condition is false: y
> - you don't need to specify useless characters such as ? or :
> or useless keywords such as :condition :then :else.
> - you don't need to use a different order.
>
> Hence: (if (> x y) x y)
>
>
> > I know for example that
> > : (> (+ 2 5) 3) returns T because it means (2+5) = 7 > 3 = True.
>
> You know false things. (> (+ 2 5) 3) doesn't return T. It _may_ return
> T, but it may also return 7 or 0 or HELLO or "You should read the clhs already!"
>
> > but as for this line
> > : (if (> x y) x y)))
> > I am just confused as to why the syntax is structured the way it
> > is. Maybe I am experiencing a brain freeze, why isn't this line simply
> > (if (> x y)) ?
>
> Because (if (> x y)) is invalid syntax, the IF operator takes at least
> two arguments.
>
>
> --
> __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

gengyangcai

10/6/2015 1:47:00 PM

0

On Tuesday, October 6, 2015 at 9:46:36 PM UTC+8, CAI GENGYANG wrote:
> Got it ...
>
> (if (> x y) x y)))
>
> if = IF operator that takes at least 2 statements
> (> x y) = condition
> x y = arguments
>
> One must always obey the rules set by the Gods who created Lisp lol
>
>
>
> On Tuesday, October 6, 2015 at 7:57:10 PM UTC+8, informatimago wrote:
> > CAI GENGYANG <gengyangcai@gmail.com> writes:
> >
> > > This is a program that defines a function that takes 2 arguments and
> > > returns the greater one.
> > >
> > > (defun greater (x y)?
> > > (if (and (numberp x) (numberp y))?
> > > (if (> x y) x y)))
> > >
> > > The first sentence is simple, it defines a a function called 'greater'
> > > that takes 2 arguments.
> >
> > It is wrong, ie. it will signal an error for some arguments:
> >
> > cl-user> (greater #c(1 0) #c(0 1))
> > > Debug: The value #C(0 1) is not of the expected type real.
> > > While executing: (:internal swank::invoke-default-debugger), in process repl-thread(15).
> > > Type :POP to abort, :R for a list of available restarts.
> > > Type :? for other options.
> > 1 > :q
> > ; Evaluation aborted on #<type-error #x302002133D2D>.
> > cl-user>
> >
> >
> > > As for the 2nd sentence, numberp is a predicate that tests whether its
> > > argument is a number. So this line basically means "if both x and y"
> > > are numbers.
> > >
> > > The last line takes the larger argument amongst the 2 and returns the
> > > greater one. I am having trouble with this one (why is the syntax
> > > structured the way it is?)
> >
> > Because this is the simpliest structure you can have for a ternary IF.
> >
> > - you need to specify the operator: IF
> > - you need to specify the condition: (> x y)
> > - you need to specify what to return when the condition is true: x
> > - you need to specify what to return when the condition is false: y
> > - you don't need to specify useless characters such as ? or :
> > or useless keywords such as :condition :then :else.
> > - you don't need to use a different order.
> >
> > Hence: (if (> x y) x y)
> >
> >
> > > I know for example that
> > > : (> (+ 2 5) 3) returns T because it means (2+5) = 7 > 3 = True.
> >
> > You know false things. (> (+ 2 5) 3) doesn't return T. It _may_ return
> > T, but it may also return 7 or 0 or HELLO or "You should read the clhs already!"
> >
> > > but as for this line
> > > : (if (> x y) x y)))
> > > I am just confused as to why the syntax is structured the way it
> > > is. Maybe I am experiencing a brain freeze, why isn't this line simply
> > > (if (> x y)) ?
> >
> > Because (if (> x y)) is invalid syntax, the IF operator takes at least
> > two arguments.
> >
> >
> > --
> > __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

Jussi Piitulainen

10/6/2015 2:31:00 PM

0

CAI GENGYANG writes:

> Got it ...
>
> (if (> x y) x y)))
>
> if = IF operator that takes at least 2 statements
> (> x y) = condition
> x y = arguments
>
> One must always obey the rules set by the Gods who created Lisp

In Lisp, of all languages, you *can* set things up so that (if (> x y))
does what you seem to want it to do. You could even set your if up to do
double duty so that one-armed and two-armed ifs work as everyone expects
while unarmed ifs have your private meaning. Write a macro, probably do
some package magic.

That is, you can have (if (> x y)) be the maximum and (if (< x y)) be
the minimum of x and y, and whatever other cases you might come up with.

Do you also want (let ((z (max x y))) (if z)) be the maximum of x and y?
That's quite a bit more weird.