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

The Little Schemer

Name: Anonymous 2009-07-19 23:23

Hi, I am working through the little schemer. I do not have the math skills to work through SICP.

I have the following code:


(define (member? a lat)
  (lambda (a lat)
    (cond
      ((null? lat) #f)
      (else (or (eq? (car lat) a)
                (member? a (cdr lat)))))))



When I run this say like:
(member? 3 (cons 3 '(9 8 7 6 13 3)))

I get the following:
#<procedure:...my-scheme-log.ss:137:2>
rather than a list without the first instance of 3

Can you help me?

Name: Scheme Noobie 2009-07-21 0:44

Hi friends. I am going to use a name to keep track of my progress. I hope that you guys don't mind. I am not trolling.
I just want to keep track of this.


;;write substr, with single arg - not following instructions
(define substr
  (lambda (rem lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) rem) (cdr lat))
      (else
       (cons (car lat) (substr rem (cdr lat)))))))

;; following the instructions now lol
(define substr-right
  (lambda (new old lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) old) (cons new (cdr lat)))
      (else (cons (car lat) (substr-right new old (cdr lat)))))))

;;write substr2
(define substr2
  (lambda (new o1 o2 lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) o1) (cons new (cdr lat)))
      ((eq? (car lat) o2) (cons new (cdr lat)))
      (else
       (cons (car lat) (substr2 new o1 o2 (cdr lat)))))))

;;substr2 v2 - using or - review how to use or and else and stuff. There are so many parentheses lol.
(define substr2-v2
  (lambda (new o1 o2 lat)
    (cond
      ((null? lat) '())
      ((or (eq? (car lat) o1) (eq? (car lat) o2)) (cons new (cdr lat)))
      (else
       (cons (car lat) (substr2 new o1 o2 (cdr lat)))))))


These are all recursive functions and challenges from The Little Schemer. It is a great book and really allows me to learn scheme. I like it so far. I feel like I am finally understanding recursion and how to reason about programs.
This makes it more fun to program. It would have taken me more time to write these in python (not flaming, just my personal opinion) I know python as well as I know scheme. Not very well lol.

I am looking forward to learning more about the lambda item and more functions.


Hey guys, if I learn scheme can I learn lisp too? They look the same and I read that scheme is a dialect of lisp.

Thanks.

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