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

DOCTOR SCHEME

Name: Anonymous 2008-12-10 14:33

(define (pow X N)
  (cond
   ((= N 0) 1)
   ((> N 0) (* X (pow X (- N 1))))
   )
  )

(define (countAtoms X)
  (cond
    ((null? X)
     0)
    ((list? X)
     (cond
       ((list? (car X))
        (+ (countAtoms (cdr X)) (countAtoms (car X))))
       (else
        (+ (countAtoms (cdr X)) 1)))
     )
    )
  )

(define (cons2 X Y)
  (cond
    ((null? X)
     (cons Y ()))
    ((not(pair? X))
     (cons X (cons Y ())))
    (else
     (cons (car X) (cons2 (cdr X) Y)))
    )
  )

(define (reverse X)
  (cond
    ((null? X) ())
    ((null? (cdr X))
     (car X))
    ((list? X)
     (cond
       ((list? (car X))
        (cons2 (reverse (cdr X)) (reverse(car X))))
       (else
        (cons2 (reverse (cdr X)) (car X)))))
    )
  )

Name: Anonymous 2008-12-10 15:02

cons2 is like a reverse cons

cons x y gives a list with x as the first atom/list and y as the rest of the list after x

cons2 gives a list where x is the last atom/list and y is the stuff before x

I was taught scheme 2 days ago and don't know a better way to do it...

I don't know how other people put their parentheses.

Also;
[quote]I HAVEN'T READ MY SICP [s]TODAY[/s] EVER[/quote]

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