Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

DERP

Name: HERP 2010-07-22 20:53

(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))

I understand everything up until the else.
HALP.

Name: Anonymous 2010-07-22 20:56

By that I mean, it calls itself with the first line of the else, but would it ever get to the second line? It keeps calling itself until one of the conditions is met at the first line. Why is there a second line if it's never used?

Name: Anonymous 2010-07-22 21:01

Please indent your code properly. Most Lispers have trouble reading unintended code.

Name: Anonymous 2010-07-22 21:03

I'm loling.

Name: Anonymous 2010-07-22 21:04

>>3
The Forced Optional Indenting of Code

Name: Anonymous 2010-07-22 21:04

>>3
Sorry, it's copypasted straight from my SICP pdf.
(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))

Name: Anonymous 2010-07-22 21:05

>>6
Ha, fuck my face. The formatting in the reply box is shit.

Name: Anonymous 2010-07-22 21:06

Try matching parenthesis. There is no second line to the else.

Name: Anonymous 2010-07-22 21:06

>>2
Read about the COND form.
Here's I'll show you an expansion of a COND form, so you'd understand how it works:


(cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (t (A (- x 1)
              (A x (- y 1)))))
=>

(if (= y 0)
    (begin 0)
    (cond ((= x 0) (* 2 y)) ((= y 1) 2) (t (a (- x 1) (a x (- y 1))))))

=>

(if (= y 0)
    (begin 0)
    (if (= x 0)
        (begin (* 2 y))
        (cond ((= y 1) 2) (t (a (- x 1) (a x (- y 1)))))))

=>

(if (= y 0)
    (begin 0)
    (if (= x 0)
        (begin (* 2 y))
        (if (= y 1)
            (begin 2)
            (cond (t (a (- x 1) (a x (- y 1))))))))

=>

(if (= y 0)
    (begin 0)
    (if (= x 0)
        (begin (* 2 y))
        (if (= y 1)
            (begin 2)
            (begin (a (- x 1) (a x (- y 1)))))))

(cond
  (test1 result1)
  ...)

=>

(if test1
    (begin result1)
    (cond ...))

Name: Anonymous 2010-07-22 21:09

In the event you reach the else case, [code](A x (- y 1))[/code will be called and its return value is used as the second argument in the other recursive call. What's there to understand? Is it that you want to know the purpose of the function?

Name: Anonymous 2010-07-22 21:15

>>9
I meant the second line of the else, not the entire definition. Because the procedure calls itself in the first expression after else, wouldn't it be impossible to get to the second expression after else, regardless of input?

Name: Anonymous 2010-07-22 21:18

>>10
its return value is used as the second argument in the other recursive call
Ah, that's what I wanted to know, many thanks.

Name: Anonymous 2010-07-22 21:24

>>12
Wait a second >>10
Would (A x(- y 1)) run before (A (- x 1)) and use it's value as the argument? I would think (A (- x 1)) would run first and use it's value for the argument of (A x(- y 1)) .

Name: Anonymous 2010-07-22 21:25

>>13
Function calls work by evaluating the arguments first then passing the values to the function.

Name: Anonymous 2010-07-22 21:25

Apparently either reading the documentation or experimenting to figure basic things out yourself is considered too advanced to bother with now.

Name: Anonymous 2010-07-22 21:29

>>14,15
Sorry for the confusion on my part, I'm still getting used to reading the parentheses.

Name: Anonymous 2010-07-22 21:31

>>16
Get a good editor/implementation.
For Scheme, try Racket's.
For Common Lisp, try Emacs+SLIME+Paredit+Redshank (+your CL implementation).

Name: Anonymous 2010-07-22 21:33

>>16
It's just function application
(A (- x 1) (A x (- y 1)))))) =>
A( -(x,1) , A( x, -(y,1) ) )

Name: Anonymous 2010-07-22 21:34

>>17
I'm using Racket right now, I just fucked up reading the parentheses. I thought it was calling itself two separate times in the else body(clause?), it did not look nested to me whenever I was reading.

Don't change these.
Name: Email:
Entire Thread Thread List