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

Pages: 1-

Exercise 1.12

Name: Anonymous 2013-05-04 20:04

The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 Write a procedure that computes elements of Pascal's triangle by means of a recursive process.

FUCKING EASY


(define (f n)
  (if (= n 0) 1
      (* n (f (- n 1)))))

(define (p x y)
  (cond ((or (= x y) (= x 0)) 1)
        (else (/ (f y)
                 (* (f x) (f (- y x)))))))

(define (pascal n)
  (define (loop x)
    (if (= x 0) (list 1) (cons (p x n) (loop (- x 1)))))
  (loop n))


i think im staring to get a hang of this *grabs dick*

Name: Anonymous 2013-05-04 20:10

ur mom is recursive in bed ol

Name: Anonymous 2013-05-04 23:41

>>1
Symta:to p X Y: cond | or (X is Y) (X is 0): 1
               | else: f.Y/f.X*(f Y-X)


Lisp:(define (p x y)
  (cond ((or (= x y) (= x 0)) 1)
        (else (/ (f y)
                 (* (f x) (f (- y x)))))))

Name: Anonymous 2013-05-04 23:42

fix:

Symta:
to p X Y: cond | or (X is Y) (X is 0): 1
               | else: f.Y/f.X*(f Y-X)


Lisp:
(define (p x y)
  (cond ((or (= x y) (= x 0)) 1)
        (else (/ (f y)
                 (* (f x) (f (- y x)))))))

Name: Anonymous 2013-05-04 23:45

Lisp with a macro:
(to p X Y
  ! cond
  ! ! or (= X Y) (= X 0) :> 1
  ! ! or t :> / (f Y) (* (f X) (f (- Y X))))


expands into:
(DEFUN P (X Y)
  (COND
   ((OR (= X Y) (= X 0))
    (PROGN 1))
   ((OR T)
    (/ (F Y) (* (F X) (F (- Y X)))))))

Name: Anonymous 2013-05-05 0:16

Symta:
to p X Y: cond | or (X is Y) (X is 0): 1
               | else: f.Y/f.X*(f Y-X)


Scheme:
(define (p x y) (if (memv x `(0 ,y)) 1 (/ (f y) (* (f x) (f (- y x))))))

C:
[code]float p(float x, float y) {
        return (x == y || x == 0) ? 1 : f(y)/f(x)*f(y-x);
}[code]

Name: Anonymous 2013-05-05 0:38

And here's the patrician way to do it.

(define (pascal n)
  (if (zero? n)
    (list 1)
    (cons 1 (let iter ((ls (pascal (- n 1))))
              (if (null? (cdr ls))
                (list 1)
                (cons (+ (car ls) (cadr ls))
                      (iter (cdr ls))))))))


Waiting on you, Symta guy.

Name: >>7 2013-05-05 0:41

Optimized even further.

(define (pascal n)
  (if (zero? n)
    (list 1)
    (cons 1 (let iter ((ls (pascal (- n 1))))
              (if (null? (cdr ls))
                ls
                (cons (+ (car ls) (cadr ls))
                      (iter (cdr ls))))))))

Name: Anonymous 2013-05-05 10:57

>>7

to pascal N
| if N is 0
| | list 1
| | pre 1
| | | let iter Ls (pascal N-1)
| | | | if Ls.tail empty?
| | | | | list 1
| | | | | list Ls.0+Ls.1 @Ls.tail,tail

Name: Anonymous 2013-05-05 11:00

>>9
@Ls.tail,iter
self fix

Name: Anonymous 2013-05-05 13:55

>>9
|
| |
| |
| | |
| | | |
| | | | |
| | | | |

Name: Anonymous 2013-05-05 14:15

Name: Anonymous 2013-05-05 14:50

>>11
At this point you might as well have FIOC.

Name: Anonymous 2013-05-05 15:05

>>11
Python:


Symta:

|
| |
| |
| | |
| | | |
| | | | |
| | | | |

Name: Anonymous 2013-05-05 15:55

Name: Anonymous 2013-05-05 20:42

>>15
Shalom!

Name: Anonymous 2013-05-06 13:46

>>14
lol!

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