[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Using Let Operators

gengyangcai

11/5/2015 3:49:00 PM

I know how to use the let operator to introduce 2 variables , add them and print out the result :

> (let ((x 1) (y 2))
(+ x y))

3

How do you use the let operator to create a function that takes 3 inputs (x , y , z), multiple x and y, then divides the result by z , ie --- [ (x+y) / z ] ?

I tried this :

> (let ((x 5) (y 6) (z 2))
(* x y) / z)

expecting the result to be 5 * 6 / 2 = 15, but the result I got instead was

2


6 Answers

Barry Margolin

11/5/2015 3:53:00 PM

0

In article <a3cba1aa-8922-4e21-b0b9-62e470b19c86@googlegroups.com>,
CAI GENGYANG <gengyangcai@gmail.com> wrote:

> I know how to use the let operator to introduce 2 variables , add them and
> print out the result :
>
> > (let ((x 1) (y 2))
> (+ x y))
>
> 3
>
> How do you use the let operator to create a function that takes 3 inputs (x ,
> y , z), multiple x and y, then divides the result by z , ie --- [ (x+y) / z ]
> ?
>
> I tried this :
>
> > (let ((x 5) (y 6) (z 2))
> (* x y) / z)
>
> expecting the result to be 5 * 6 / 2 = 15, but the result I got instead was
>
> 2

Your problem isn't with the let operator, it's that you're confused
about how to do division. Just like every other function call in Lisp,
you put the operator *first* and wrap it in a list:

(/ (* x y) z)

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

gengyangcai

11/5/2015 3:59:00 PM

0

Right, got it .. I keep forgetting that in Lisp, the division sign always comes at the beginning


CL-USER 1 > (let ((x 5) (y 6) (z 2))
(/ (* x y) z))
15







On Thursday, November 5, 2015 at 11:53:32 PM UTC+8, Barry Margolin wrote:
> In article <a3cba1aa-8922-4e21-b0b9-62e470b19c86@googlegroups.com>,
> CAI GENGYANG <gengyangcai@gmail.com> wrote:
>
> > I know how to use the let operator to introduce 2 variables , add them and
> > print out the result :
> >
> > > (let ((x 1) (y 2))
> > (+ x y))
> >
> > 3
> >
> > How do you use the let operator to create a function that takes 3 inputs (x ,
> > y , z), multiple x and y, then divides the result by z , ie --- [ (x+y) / z ]
> > ?
> >
> > I tried this :
> >
> > > (let ((x 5) (y 6) (z 2))
> > (* x y) / z)
> >
> > expecting the result to be 5 * 6 / 2 = 15, but the result I got instead was
> >
> > 2
>
> Your problem isn't with the let operator, it's that you're confused
> about how to do division. Just like every other function call in Lisp,
> you put the operator *first* and wrap it in a list:
>
> (/ (* x y) z)
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***

Pascal J. Bourguignon

11/5/2015 4:00:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> I know how to use the let operator to introduce 2 variables , add them
> and print out the result :
>
>> (let ((x 1) (y 2))
> (+ x y))
>
> 3
>
> How do you use the let operator to create a function that takes 3
> inputs (x , y , z), multiple x and y, then divides the result by z ,
> ie --- [ (x+y) / z ] ?
>
> I tried this :
>
>> (let ((x 5) (y 6) (z 2))
> (* x y) / z)
>
> expecting the result to be 5 * 6 / 2 = 15, but the result I got instead was

(let ()
(lambda (x y z) (/ (* x y)z)))


cl-user> (let () (lambda (x y z) (/ (* x y)z)))

#<Anonymous Function #x3020023D73CF>
cl-user> (funcall (let () (lambda (x y z) (/ (* x y)z))) 5 6 2)
15

But really, LET is useless here. To create a function that takes 3
inputs (x , y , z), multiple x and y, then divides the result by z, you
just need LAMBDA:


cl-user> (lambda (x y z) (/ (* x y)z))

#<Anonymous Function #x30200241A5DF>
cl-user> (funcall (lambda (x y z) (/ (* x y)z)) 5 6 2)

15
cl-user>

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

11/5/2015 4:18:00 PM

0

On 2015-11-05, CAI GENGYANG <gengyangcai@gmail.com> wrote:
> I know how to use the let operator to introduce 2 variables , add them and print out the result :
>
>> (let ((x 1) (y 2))
> (+ x y))
>
> 3
>
> How do you use the let operator to create a function that takes 3 inputs (x , y , z), multiple x and y, then divides the result by z , ie --- [ (x+y) / z ] ?
>
> I tried this :
>
>> (let ((x 5) (y 6) (z 2))
> (* x y) / z)
>
> expecting the result to be 5 * 6 / 2 = 15, but the result I got instead was

I have the impression that your cognitive profile isn't such a good match for
computer programming, at least in a conventional language. Consider investing
your valuable time into a different pursuit.

If you keep plugging away at it, I do think Lisp is good for you, because
you're finding basic syntax difficult, and Lisp makes this area of programming
nearly as simple and free of ambiguity as practically possible.

You might do better still with some graphical programming language where
data flow is represented by arrows between boxes. Some people who don't do
so well with syntax nevertheless are good at visualizing processes,
and spatial relationships.

rpw3

11/6/2015 1:25:00 AM

0

CAI GENGYANG <gengyangcai@gmail.com> wrote:
+---------------
| Right, got it .. I keep forgetting that in Lisp,
| the division sign always comes at the beginning
+---------------

Actually, your mistake is thinking that there is
anything in Lisp called a "division sign" that is
any different from any other function. There isn't.
The built-in function for division is just that:
a normal function. Its usual name happens to be "/",
but in Lisp "/" is just an ordinary symbol name,
usually imported from the COMMON-LISP package
(nicknamed CL).

> (describe '/)

/ is an external symbol in the COMMON-LISP package.

It is a special variable; its value is (T).
(T) is a CONS.
Special documentation:
Holds a list of all the values returned by the most
recent top-level EVAL.

Function: #<Function / {1047E941}>
Function arguments:
(number &rest more-numbers)
Function documentation:
Divides the first arg by each of the following arguments, in turn.
With one arg, returns reciprocal.
Its declared argument types are:
(NUMBER &REST NUMBER)
Its result type is:
NUMBER
...
>

Since *all* compound forms in Lisp start with a function or
special operator, and since "/" has a function value, it too
will go at the beginning of a form that's expressing division.

But be aware that "/" also has a variable value [at least
in the REPL] as well as a function value. As noted in the
above DESCRIBE, as a variable it holds a list of the values
of the most recent REPL result. [And "//" holds the second
most recent, and "///" the third.]

> (values 1 2 3 4 5)

1
2
3
4
5
> /

(1 2 3 4 5)
> (fourth //)

4
> (second ///)

2
>

To repeat, in Lisp "/" is *not* any kind of special syntax
for division; it's an ordinary symbol that just happens to
be named "/" [which is a legal name, in Lisp], and happens
to be function-bound to the function for division. Like many
other symbols, it holds several values, depending upon in
which namespace it is accessed: a (special) variable value;
a function value; possibly even a tag value. Extreme case
[you might not understand this yet, but you might want
to study it until you do]:

> (floor 369 10)

36
9
> (tagbody
(print "first")
(go /) ; Go to the tag named "/".
(print "second") ; This never gets executed.
/ ; Here "/" is used as a tag.
(print (apply #'/ /))) ; Here both the function and variable
; values of "/" are used.

"first"
4 ; Result of dividing 36 by 9.
NIL
>


-Rob

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

taruss

11/6/2015 10:08:00 PM

0

On Thursday, November 5, 2015 at 5:30:08 PM UTC-8, Rob Warnock wrote:

> But be aware that "/" also has a variable value [at least
> in the REPL] as well as a function value. As noted in the
> above DESCRIBE, as a variable it holds a list of the values
> of the most recent REPL result. [And "//" holds the second
> most recent, and "///" the third.]

Which, incidentally, is why the original body of the LET:
(let ((x 5) (y 6) (z 2))
(* x y) / z)

did not signal an error. The body of the let gets evaluated to multiply x and
y, evaluate / to get it's global (special) value, and finally evaluate z. Since LET returns the value of the last form, the value of Z is returned.
This is, for example exactly what you would also get from
(let ()
(* 5 6)
/
2)
which is formatted to make the separate expressions in the body of the loop clearer.