car implementation in scheme
I am trying to write by myself the cons
function in scheme. I have written this code:
(define (car. z)
(z (lambda (p q) p)))
and I am trying to run :
(car. '(1 2 3))
I expect to get the number 1
, but it does not work properly.
scheme racket
|
show 7 more comments
I am trying to write by myself the cons
function in scheme. I have written this code:
(define (car. z)
(z (lambda (p q) p)))
and I am trying to run :
(car. '(1 2 3))
I expect to get the number 1
, but it does not work properly.
scheme racket
1
In your definition ofcar.
the argument z is used as a function applied to another function. In the call you pass instead a list, not a function.
– Renzo
Jan 1 at 10:23
5
car.
wants a "lambda-encoded" list, not a Scheme list. You need to implement the correspondingcons.
operation, so you can create such lists.
– molbdnilo
Jan 1 at 10:25
1
As @molbdnilo said in the comment, you must decide if you want to represent lists as functional objects (the definition of the functioncar.
), or as data structures (as in your call), and act uniformely. If you represent lists as functional objects, you must define firstcons
, and pass the result of this function tocar.
.
– Renzo
Jan 1 at 10:28
1
In practice the genuinecar
,cdr
andcons
are builtins (and they have to be). Read Queinnec's Lisp In Small Pieces
– Basile Starynkevitch
Jan 1 at 10:42
2
@user10853826, you can find everything in this great free book online. Or see for instance this SO question
– Renzo
Jan 1 at 11:28
|
show 7 more comments
I am trying to write by myself the cons
function in scheme. I have written this code:
(define (car. z)
(z (lambda (p q) p)))
and I am trying to run :
(car. '(1 2 3))
I expect to get the number 1
, but it does not work properly.
scheme racket
I am trying to write by myself the cons
function in scheme. I have written this code:
(define (car. z)
(z (lambda (p q) p)))
and I am trying to run :
(car. '(1 2 3))
I expect to get the number 1
, but it does not work properly.
scheme racket
scheme racket
edited Jan 1 at 10:35
rsm
1,26431522
1,26431522
asked Jan 1 at 10:07
user10853826user10853826
63
63
1
In your definition ofcar.
the argument z is used as a function applied to another function. In the call you pass instead a list, not a function.
– Renzo
Jan 1 at 10:23
5
car.
wants a "lambda-encoded" list, not a Scheme list. You need to implement the correspondingcons.
operation, so you can create such lists.
– molbdnilo
Jan 1 at 10:25
1
As @molbdnilo said in the comment, you must decide if you want to represent lists as functional objects (the definition of the functioncar.
), or as data structures (as in your call), and act uniformely. If you represent lists as functional objects, you must define firstcons
, and pass the result of this function tocar.
.
– Renzo
Jan 1 at 10:28
1
In practice the genuinecar
,cdr
andcons
are builtins (and they have to be). Read Queinnec's Lisp In Small Pieces
– Basile Starynkevitch
Jan 1 at 10:42
2
@user10853826, you can find everything in this great free book online. Or see for instance this SO question
– Renzo
Jan 1 at 11:28
|
show 7 more comments
1
In your definition ofcar.
the argument z is used as a function applied to another function. In the call you pass instead a list, not a function.
– Renzo
Jan 1 at 10:23
5
car.
wants a "lambda-encoded" list, not a Scheme list. You need to implement the correspondingcons.
operation, so you can create such lists.
– molbdnilo
Jan 1 at 10:25
1
As @molbdnilo said in the comment, you must decide if you want to represent lists as functional objects (the definition of the functioncar.
), or as data structures (as in your call), and act uniformely. If you represent lists as functional objects, you must define firstcons
, and pass the result of this function tocar.
.
– Renzo
Jan 1 at 10:28
1
In practice the genuinecar
,cdr
andcons
are builtins (and they have to be). Read Queinnec's Lisp In Small Pieces
– Basile Starynkevitch
Jan 1 at 10:42
2
@user10853826, you can find everything in this great free book online. Or see for instance this SO question
– Renzo
Jan 1 at 11:28
1
1
In your definition of
car.
the argument z is used as a function applied to another function. In the call you pass instead a list, not a function.– Renzo
Jan 1 at 10:23
In your definition of
car.
the argument z is used as a function applied to another function. In the call you pass instead a list, not a function.– Renzo
Jan 1 at 10:23
5
5
car.
wants a "lambda-encoded" list, not a Scheme list. You need to implement the corresponding cons.
operation, so you can create such lists.– molbdnilo
Jan 1 at 10:25
car.
wants a "lambda-encoded" list, not a Scheme list. You need to implement the corresponding cons.
operation, so you can create such lists.– molbdnilo
Jan 1 at 10:25
1
1
As @molbdnilo said in the comment, you must decide if you want to represent lists as functional objects (the definition of the function
car.
), or as data structures (as in your call), and act uniformely. If you represent lists as functional objects, you must define first cons
, and pass the result of this function to car.
.– Renzo
Jan 1 at 10:28
As @molbdnilo said in the comment, you must decide if you want to represent lists as functional objects (the definition of the function
car.
), or as data structures (as in your call), and act uniformely. If you represent lists as functional objects, you must define first cons
, and pass the result of this function to car.
.– Renzo
Jan 1 at 10:28
1
1
In practice the genuine
car
, cdr
and cons
are builtins (and they have to be). Read Queinnec's Lisp In Small Pieces– Basile Starynkevitch
Jan 1 at 10:42
In practice the genuine
car
, cdr
and cons
are builtins (and they have to be). Read Queinnec's Lisp In Small Pieces– Basile Starynkevitch
Jan 1 at 10:42
2
2
@user10853826, you can find everything in this great free book online. Or see for instance this SO question
– Renzo
Jan 1 at 11:28
@user10853826, you can find everything in this great free book online. Or see for instance this SO question
– Renzo
Jan 1 at 11:28
|
show 7 more comments
2 Answers
2
active
oldest
votes
When you implement language data structures you need to supply constructors and accessors that conform to the contract:
(car (cons 1 2)) ; ==> 1
(cdr (cons 1 2)) ; ==> 2
(pair? (cons 1 2)) ; ==> 2
Here is an example:
(define (cons a d)
(vector a d))
(define (car p)
(vector-ref p 0))
(define (cdr p)
(vector-ref p 1))
Now if you make an implementation you would implement read
to be conformant to this way of doing pairs so that '(1 2 3)
would create the correct data structure the simple rules above is still the same.
From looking at car
I imagine cons
looks like this:
(define (cons a d)
(lambda (p) (p a d)))
It works with closures. Now A stack machine implementation of Scheme would analyze the code for free variables living passed their scope and thus create them as boxes. Closures containing a
, and d
aren't much different than vectors.
I urge you to implement a minimalistic Scheme interpreter. First in Scheme since you can use the host language, then a different than a lisp language. You can even do it in an esoteric language, but it is very time consuming.
add a comment |
Sylwester's answer is great. Here's another possible implementation of null
, null?
, cons
, car
, cdr
-
(define null 'null)
(define (null? xs)
(eq? null xs))
(define (cons a b)
(define (dispatch message)
(match message
('car a)
('cdr b)
(_ (error 'cons "unsupported message" message))
dispatch)
(define (car xs)
(if (null? xs)
(error 'car "cannot call car on an empty pair")
(xs 'car)))
(define (cdr xs)
(if (null? xs)
(error 'cdr "cannot call cdr on an empty pair")
(xs 'cdr)))
It works like this -
(define xs (cons 'a (cons 'b (cons 'c null))))
(printf "~a -> ~a -> ~an"
(car xs)
(car (cdr xs))
(car (cdr (cdr xs))))
;; a -> b -> c
It raises errors in these scenarios -
(cdr null)
; car: cannot call car on an empty pair
(cdr null)
; cdr: cannot call cdr on an empty pair
((cons 'a 'b) 'foo)
;; cons: unsupported dispatch: foo
define/match
adds a little sugar, if you like sweet things -
(define (cons a b)
(define/match (dispatch msg)
(('car) a)
(('cdr) b)
(('pair?) #t)
((_) (error 'cons "unsupported dispatch: ~a" msg)))
dispatch)
((cons 1 2) 'car) ;; 1
((cons 1 2) 'cdr) ;; 2
((cons 1 2) 'pair?) ;; #t
((cons 1 2) 'foo) ;; cons: unsupported dispatch: foo
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994591%2fcar-implementation-in-scheme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you implement language data structures you need to supply constructors and accessors that conform to the contract:
(car (cons 1 2)) ; ==> 1
(cdr (cons 1 2)) ; ==> 2
(pair? (cons 1 2)) ; ==> 2
Here is an example:
(define (cons a d)
(vector a d))
(define (car p)
(vector-ref p 0))
(define (cdr p)
(vector-ref p 1))
Now if you make an implementation you would implement read
to be conformant to this way of doing pairs so that '(1 2 3)
would create the correct data structure the simple rules above is still the same.
From looking at car
I imagine cons
looks like this:
(define (cons a d)
(lambda (p) (p a d)))
It works with closures. Now A stack machine implementation of Scheme would analyze the code for free variables living passed their scope and thus create them as boxes. Closures containing a
, and d
aren't much different than vectors.
I urge you to implement a minimalistic Scheme interpreter. First in Scheme since you can use the host language, then a different than a lisp language. You can even do it in an esoteric language, but it is very time consuming.
add a comment |
When you implement language data structures you need to supply constructors and accessors that conform to the contract:
(car (cons 1 2)) ; ==> 1
(cdr (cons 1 2)) ; ==> 2
(pair? (cons 1 2)) ; ==> 2
Here is an example:
(define (cons a d)
(vector a d))
(define (car p)
(vector-ref p 0))
(define (cdr p)
(vector-ref p 1))
Now if you make an implementation you would implement read
to be conformant to this way of doing pairs so that '(1 2 3)
would create the correct data structure the simple rules above is still the same.
From looking at car
I imagine cons
looks like this:
(define (cons a d)
(lambda (p) (p a d)))
It works with closures. Now A stack machine implementation of Scheme would analyze the code for free variables living passed their scope and thus create them as boxes. Closures containing a
, and d
aren't much different than vectors.
I urge you to implement a minimalistic Scheme interpreter. First in Scheme since you can use the host language, then a different than a lisp language. You can even do it in an esoteric language, but it is very time consuming.
add a comment |
When you implement language data structures you need to supply constructors and accessors that conform to the contract:
(car (cons 1 2)) ; ==> 1
(cdr (cons 1 2)) ; ==> 2
(pair? (cons 1 2)) ; ==> 2
Here is an example:
(define (cons a d)
(vector a d))
(define (car p)
(vector-ref p 0))
(define (cdr p)
(vector-ref p 1))
Now if you make an implementation you would implement read
to be conformant to this way of doing pairs so that '(1 2 3)
would create the correct data structure the simple rules above is still the same.
From looking at car
I imagine cons
looks like this:
(define (cons a d)
(lambda (p) (p a d)))
It works with closures. Now A stack machine implementation of Scheme would analyze the code for free variables living passed their scope and thus create them as boxes. Closures containing a
, and d
aren't much different than vectors.
I urge you to implement a minimalistic Scheme interpreter. First in Scheme since you can use the host language, then a different than a lisp language. You can even do it in an esoteric language, but it is very time consuming.
When you implement language data structures you need to supply constructors and accessors that conform to the contract:
(car (cons 1 2)) ; ==> 1
(cdr (cons 1 2)) ; ==> 2
(pair? (cons 1 2)) ; ==> 2
Here is an example:
(define (cons a d)
(vector a d))
(define (car p)
(vector-ref p 0))
(define (cdr p)
(vector-ref p 1))
Now if you make an implementation you would implement read
to be conformant to this way of doing pairs so that '(1 2 3)
would create the correct data structure the simple rules above is still the same.
From looking at car
I imagine cons
looks like this:
(define (cons a d)
(lambda (p) (p a d)))
It works with closures. Now A stack machine implementation of Scheme would analyze the code for free variables living passed their scope and thus create them as boxes. Closures containing a
, and d
aren't much different than vectors.
I urge you to implement a minimalistic Scheme interpreter. First in Scheme since you can use the host language, then a different than a lisp language. You can even do it in an esoteric language, but it is very time consuming.
edited Jan 1 at 19:05
answered Jan 1 at 17:43
SylwesterSylwester
34.9k22956
34.9k22956
add a comment |
add a comment |
Sylwester's answer is great. Here's another possible implementation of null
, null?
, cons
, car
, cdr
-
(define null 'null)
(define (null? xs)
(eq? null xs))
(define (cons a b)
(define (dispatch message)
(match message
('car a)
('cdr b)
(_ (error 'cons "unsupported message" message))
dispatch)
(define (car xs)
(if (null? xs)
(error 'car "cannot call car on an empty pair")
(xs 'car)))
(define (cdr xs)
(if (null? xs)
(error 'cdr "cannot call cdr on an empty pair")
(xs 'cdr)))
It works like this -
(define xs (cons 'a (cons 'b (cons 'c null))))
(printf "~a -> ~a -> ~an"
(car xs)
(car (cdr xs))
(car (cdr (cdr xs))))
;; a -> b -> c
It raises errors in these scenarios -
(cdr null)
; car: cannot call car on an empty pair
(cdr null)
; cdr: cannot call cdr on an empty pair
((cons 'a 'b) 'foo)
;; cons: unsupported dispatch: foo
define/match
adds a little sugar, if you like sweet things -
(define (cons a b)
(define/match (dispatch msg)
(('car) a)
(('cdr) b)
(('pair?) #t)
((_) (error 'cons "unsupported dispatch: ~a" msg)))
dispatch)
((cons 1 2) 'car) ;; 1
((cons 1 2) 'cdr) ;; 2
((cons 1 2) 'pair?) ;; #t
((cons 1 2) 'foo) ;; cons: unsupported dispatch: foo
add a comment |
Sylwester's answer is great. Here's another possible implementation of null
, null?
, cons
, car
, cdr
-
(define null 'null)
(define (null? xs)
(eq? null xs))
(define (cons a b)
(define (dispatch message)
(match message
('car a)
('cdr b)
(_ (error 'cons "unsupported message" message))
dispatch)
(define (car xs)
(if (null? xs)
(error 'car "cannot call car on an empty pair")
(xs 'car)))
(define (cdr xs)
(if (null? xs)
(error 'cdr "cannot call cdr on an empty pair")
(xs 'cdr)))
It works like this -
(define xs (cons 'a (cons 'b (cons 'c null))))
(printf "~a -> ~a -> ~an"
(car xs)
(car (cdr xs))
(car (cdr (cdr xs))))
;; a -> b -> c
It raises errors in these scenarios -
(cdr null)
; car: cannot call car on an empty pair
(cdr null)
; cdr: cannot call cdr on an empty pair
((cons 'a 'b) 'foo)
;; cons: unsupported dispatch: foo
define/match
adds a little sugar, if you like sweet things -
(define (cons a b)
(define/match (dispatch msg)
(('car) a)
(('cdr) b)
(('pair?) #t)
((_) (error 'cons "unsupported dispatch: ~a" msg)))
dispatch)
((cons 1 2) 'car) ;; 1
((cons 1 2) 'cdr) ;; 2
((cons 1 2) 'pair?) ;; #t
((cons 1 2) 'foo) ;; cons: unsupported dispatch: foo
add a comment |
Sylwester's answer is great. Here's another possible implementation of null
, null?
, cons
, car
, cdr
-
(define null 'null)
(define (null? xs)
(eq? null xs))
(define (cons a b)
(define (dispatch message)
(match message
('car a)
('cdr b)
(_ (error 'cons "unsupported message" message))
dispatch)
(define (car xs)
(if (null? xs)
(error 'car "cannot call car on an empty pair")
(xs 'car)))
(define (cdr xs)
(if (null? xs)
(error 'cdr "cannot call cdr on an empty pair")
(xs 'cdr)))
It works like this -
(define xs (cons 'a (cons 'b (cons 'c null))))
(printf "~a -> ~a -> ~an"
(car xs)
(car (cdr xs))
(car (cdr (cdr xs))))
;; a -> b -> c
It raises errors in these scenarios -
(cdr null)
; car: cannot call car on an empty pair
(cdr null)
; cdr: cannot call cdr on an empty pair
((cons 'a 'b) 'foo)
;; cons: unsupported dispatch: foo
define/match
adds a little sugar, if you like sweet things -
(define (cons a b)
(define/match (dispatch msg)
(('car) a)
(('cdr) b)
(('pair?) #t)
((_) (error 'cons "unsupported dispatch: ~a" msg)))
dispatch)
((cons 1 2) 'car) ;; 1
((cons 1 2) 'cdr) ;; 2
((cons 1 2) 'pair?) ;; #t
((cons 1 2) 'foo) ;; cons: unsupported dispatch: foo
Sylwester's answer is great. Here's another possible implementation of null
, null?
, cons
, car
, cdr
-
(define null 'null)
(define (null? xs)
(eq? null xs))
(define (cons a b)
(define (dispatch message)
(match message
('car a)
('cdr b)
(_ (error 'cons "unsupported message" message))
dispatch)
(define (car xs)
(if (null? xs)
(error 'car "cannot call car on an empty pair")
(xs 'car)))
(define (cdr xs)
(if (null? xs)
(error 'cdr "cannot call cdr on an empty pair")
(xs 'cdr)))
It works like this -
(define xs (cons 'a (cons 'b (cons 'c null))))
(printf "~a -> ~a -> ~an"
(car xs)
(car (cdr xs))
(car (cdr (cdr xs))))
;; a -> b -> c
It raises errors in these scenarios -
(cdr null)
; car: cannot call car on an empty pair
(cdr null)
; cdr: cannot call cdr on an empty pair
((cons 'a 'b) 'foo)
;; cons: unsupported dispatch: foo
define/match
adds a little sugar, if you like sweet things -
(define (cons a b)
(define/match (dispatch msg)
(('car) a)
(('cdr) b)
(('pair?) #t)
((_) (error 'cons "unsupported dispatch: ~a" msg)))
dispatch)
((cons 1 2) 'car) ;; 1
((cons 1 2) 'cdr) ;; 2
((cons 1 2) 'pair?) ;; #t
((cons 1 2) 'foo) ;; cons: unsupported dispatch: foo
edited Jan 2 at 21:12
answered Jan 2 at 21:00
user633183user633183
70.8k21140180
70.8k21140180
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994591%2fcar-implementation-in-scheme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
In your definition of
car.
the argument z is used as a function applied to another function. In the call you pass instead a list, not a function.– Renzo
Jan 1 at 10:23
5
car.
wants a "lambda-encoded" list, not a Scheme list. You need to implement the correspondingcons.
operation, so you can create such lists.– molbdnilo
Jan 1 at 10:25
1
As @molbdnilo said in the comment, you must decide if you want to represent lists as functional objects (the definition of the function
car.
), or as data structures (as in your call), and act uniformely. If you represent lists as functional objects, you must define firstcons
, and pass the result of this function tocar.
.– Renzo
Jan 1 at 10:28
1
In practice the genuine
car
,cdr
andcons
are builtins (and they have to be). Read Queinnec's Lisp In Small Pieces– Basile Starynkevitch
Jan 1 at 10:42
2
@user10853826, you can find everything in this great free book online. Or see for instance this SO question
– Renzo
Jan 1 at 11:28