Skip to main content
  1. About
  2. For Teams
Asked
Viewed 712 times
1

So, from the SICP we know that the cons car and cdr can be defined as a procedure:

(define (cons x y)
    (lambda (m) (m x y)))

(define (car z)
    (z (lambda (p q) p)))

(define (cdr z)
    (z (lambda (p q) q)))

But the pre-defined procedure list, which takes the arguments to build a list, uses the original cons. That means, a list that list built, isn't a procedure as I want.

(car (list 1 2 3))
;The Object (1 2 3) is not applicable

So i write this:

(define (list . l)
    (if (null? l)
        '()
        (cons (original-car l)
              (list (original-cdr l)))))

I just wondering how to define the original-car and original-cdr. Are there some way to make a copy of a procedure in Scheme? Or there's some alternate way to solve this problem. thx

3
  • (define original-car car)? I don't think I get what you're trying to do. I use chicken scheme and (car '( 1 2 3)) works fine, as does (car (list 1 2 3)).
    cmt
    –  cmt
    2013-06-09 02:18:43 +00:00
    Commented Jun 9, 2013 at 2:18
  • @cmt it'll work fine in any scheme
    daniel gratzer
    –  daniel gratzer
    2013-06-09 02:29:03 +00:00
    Commented Jun 9, 2013 at 2:29
  • @cmt not works as i want. I just want to define a list procedure to adapt my lambda-version cons and car/cdr.
    DeathKing
    –  DeathKing
    2013-06-09 02:59:36 +00:00
    Commented Jun 9, 2013 at 2:59

2 Answers 2

4

If you need to save a reference to the "original" procedures before redefining them, simply create an alias before defining the "new" procedures (I guess that's what you mean by "copying" them). Like this:

(define original-cons cons)
(define original-car car)
(define original-cdr cdr)
(define original-list list)

In this way, the old procedures can still be used, as long as we refer to them by their new names. In other words, the implementation of cons, car, cdr and list as procedures will look like this:

(define (my-cons x y)
  (lambda (m) (m x y)))

(define (my-car z)
  (z (lambda (p q) p)))

(define (my-cdr z)
  (z (lambda (p q) q)))

(define (my-list . els)
  (if (null? els)
      '()
      (my-cons
       (original-car els)
       (apply my-list (original-cdr els)))))

And sure enough, it works:

(define lst (my-list 1 2 3 4))
lst
=> #<procedure>
(my-car lst)
=> 1
(my-car (my-cdr lst))
=> 2
Sign up to request clarification or add additional context in comments.

Comments

2

List in an implementation is defined as

(define (list . l) l)

However, this is using a lot of the underlying implementation. E.g. to work it uses the native cons. cons as defined in SICP is a thought experiment so you're implementation needs a little correction:

(define (my-cons x y)
    (lambda (m) (m x y)))

(define (my-car z)
    (z (lambda (p q) p)))

(define (my-cdr z)
    (z (lambda (p q) q)))

(define (my-list . l)
  (define (my-list-aux l)
    (if (null? l)
        '()
        (my-cons (car l)
                 (my-list-aux (cdr l)))))
  (my-list-aux l))

;; optional, update binding
(define car my-car)
(define cdr my-cdr)
(define list my-list)

my-cons my-car, my-cdr and my-list are as defined in your question. Only change is reference to correct procedure (with name not conflicting with Scheme)

3 Comments

For some reason I HAVE TO overload the default procedure car cdr and cons. That is my biggest problem. And another problem is that my procedure my-list should be (my-list . l) (otherwise (my-list 1 2 3) raise a Argument Error) but if you defined as . l, the l is a list that constructed by original cons, not my lambda version. So i just want to make a copy of the original car and cdr to get out the object.
Sylwester's answer is correct. Use his implementation of my-list. Just create another function called list that does (define (list . l) (my-list (car l)))
@cmt Actually. It wasn't quite right. I have updated my answer. You may do what you do in your question and change their names after their are defined like this (define car my-car) and this will not alter what is called in the definition since the procedures has it's lexical environment.

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.