Questions tagged [cons]

The fundamental operation for constructing data in LISP

In the dialects of LISP (including Common Lisp, Scheme, Clojure) the cons procedure is the basic building block for constructing a memory object which holds two values (or pointers to values). The objects created by a call to cons are referred to as (cons) cells or as (cons) pairs.

178 questions
3
votes
3 answers

Scheme: When I cons two atoms I am getting a dot in the middle. Why?

When I do a cons on two atoms, I am getting a . in between. 1]=>(cons 'one 'two) ;Value 1: (one . two) Why I am getting the . operator. Does it have any meaning? I am using mit-scheme. I have seen this stackoverflow link but not clear. UPDATE: The…
Sreekumar R
  • 573
  • 6
  • 24
3
votes
1 answer

Is the cons operation cons elements from right to left?

We know 1:2:[] will returns [1,2]. I just tried 1:2, this gives me an error. :48:1: error: ? Non type-variable argument in the constraint: Num [a] (Use FlexibleContexts to permit this) ? When checking the inferred type …
user8314628
  • 1,952
  • 2
  • 22
  • 46
3
votes
1 answer

Pattern matching x::xs not splitting list accordingly in F#?

I'm new to F# and I'm trying to write a method split that splits a list into 2 pieces. It takes a tuple with the first element being the number of elements to split and the second element is the list . For example, split (2, [1;2;3;4;5;6]) should…
coder4lyf
  • 927
  • 1
  • 17
  • 36
3
votes
2 answers

OCaml cons purpose

I'm reading through the language manual for OCaml and came across the "cons" operator, denoted as :: However, it's not explained at all what it is, and what its typically used for.
user2789945
  • 527
  • 2
  • 6
  • 23
3
votes
2 answers

Scheme explanation(construct)

Can someone please explain me why : (define a (lambda() (cons a #f))) (car (a)) ==> procedure ((car (a))) ==> (procedure . #f) I don't think I get it. Thanks
3
votes
1 answer

SICP cons-stream

regarding to SICP 3.5 my own implementation is as following (define (delay exp) (lambda () exp)) (define (force delayed-obj) (delayed-obj)) (define (cons-stream a b) (cons a (delay b))) (define (stream-car stream) (car stream)) (define…
user3754786
  • 57
  • 1
  • 6
3
votes
1 answer

How to return nothing with cons in scheme

I have a small issue with an assigned programming task in Scheme. We were given the task to make a function that returns only the values in a pair structure that meet the requirements of a given predicate. They are also to be returned in the same…
Tenbin
  • 181
  • 2
  • 11
3
votes
3 answers

Lisp difference between (cons 'a (cons 'b 'c)) and (cons 'a '(b.c))

What's the difference between: (cons 'a (cons 'b 'c)) ;; (A B . C) and (cons 'a '(b.c)) ;; (A B.C) I need to create the following list ((a.b).c) using cons so i'm trying to understand what that "." represents. L.E.: I have the following (cons…
daniels
  • 18,416
  • 31
  • 103
  • 173
3
votes
1 answer

MIT Scheme Expression alternative form of append

I am currently attempting to resolve a question in a practice mid-term. The question asks for me to write an expression to append two lists (let us call them list1 and list2), and list2 must be appended to the end of list1. The function append…
CodeRook
  • 171
  • 2
  • 11
3
votes
2 answers

Why is caddr[(A B C)] = C?

Ideally in LISP: caddr[(A B C)] = car[cdr[cdr[(A B C)]]] = car[cdr[(B C)]] = car[C] = Undefined. But the book says answer is C. Can anyone please explain? Thanks a lot.
nrb
  • 31
  • 3
2
votes
4 answers

c(a|d)+r macro in Racket

I wonder if it's possible to write a macro in Racket that would translate every form of shape (c(a|d)+r xs), where c(a|d)+r is a regular expression matching car, cdr, caar, cadr, ... etc, into the corresponding composition of first and rest. For…
Racket Noob
  • 1,056
  • 1
  • 8
  • 16
2
votes
3 answers

Are expressions such as (set! c (cons 3 c)) the way to add an item to a list?

Scheme has set-car! and set-cdr!, but no set-cons! . Are expressions such as (set! c (cons 3 c)) which places the element 3 on the list c, the proper/only/best/usual way to modify a list?
drb
  • 728
  • 8
  • 21
2
votes
3 answers

delete-doubles function (scheme)

(define (delete-doubles lst) (cond ((null? lst) '()) ((null? (cdr lst)) (car lst)) ((equal? (car lst) (cadr lst)) (delete-doubles (cdr lst))) (else (cons (car lst) (delete-doubles (cdr lst)))))) This is the code I made. It…
2
votes
1 answer

How do lisps that prefer first and rest to car and cdr approach combinations like cdaddr?

One of the great schisms in the Lisp community is if we should have car and cdr or first and rest. One of the benefits of the traditional car and cdr is that we can combine them to produce pronoucible functions like cdaddr. How do Lisps that do not…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
2
votes
1 answer

What does appending two lists like this return one list rather than a cons cell of two lists?

A common and simple way of appending two lists is as follows: (define (append a b) (if (null? a) b (cons (car a) (append (cdr a) b)))) Why does this work? When we reach the final element of a, my clearly incorrect belief is that we…
J. Mini
  • 1,868
  • 1
  • 9
  • 38