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
5
votes
1 answer

in clojure language what <'a> really is

actually i am trying to perfectly understand clojure and particularly symbols (def a 1) (type a) ;;=>java.lang.Long (type 'a) ;;=>clojure.lang.Symbol I know that type is a function so its arguments get evaluated first so i perfectly understand why…
user3228423
  • 125
  • 3
4
votes
5 answers

Lisp, cons and (number . number) difference

What is the difference between (cons 2 3) and '(2 . 3) in Lisp?
David Degea
  • 1,378
  • 2
  • 12
  • 18
4
votes
3 answers

Is it still inadvisable to define constructors and selectors as cons, car, and cdr?

Structure and Interpretation of Computer Programs has the following footnote: Another way to define the selectors and constructor is (define make-rat cons) (define numer car) (define denom cdr) The first definition associates the name make-rat with…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
4
votes
2 answers

Why does the position of arguments matter in cons?

Simple code: > (cons null (cons 1 2)) '(() 1 . 2) > (cons (cons 1 2) null) '((1 . 2)) Initially, I'd expect the result to be the same. I can think of some vague explanations, but would also like to hear a strong point from someone…
kek_mek
  • 105
  • 4
4
votes
1 answer

No cons operator or curried cons function in F#?

We can write 3 + 4 or (+) 3 4 in F# and get the same result, and this works for most operators. Why is it that the cons operator :: behaves differently? E.g. if I run (::) 1 [2;3] I get error FS0010: Unexpected symbol '::' in expression whereas I'd…
Paul Orland
  • 542
  • 4
  • 16
4
votes
1 answer

Clojure sequences and collections

In Lisp all data structures builds of cons cells, i.e they are essentially linked lists or binary trees or both (correct me if I'm wrong). Clojure data structures are lists, vectors, maps and sets. Clojure incorporates two inclusive abstractions for…
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225
4
votes
5 answers

Scheme List Manipulation (Recursion)

The basic problem here is, when given a list, to return all elements of that list other than the last element. For example, given (a b c d) --> return (a b c). I essentially have the function, it's just the Scheme syntax that I'm having trouble with…
Chris Arena
  • 1,602
  • 15
  • 17
4
votes
1 answer

Why Does :: Only Work With Lists?

What is the reasoning behind making :: specific to List and not available to all subclasses of Seq? To give a concrete example: // :: for pattern matching def getTail[E](s: Seq[E]): Seq[E] = s match { case head :: tail => tail case empty =>…
Jake Greene
  • 5,539
  • 2
  • 22
  • 26
4
votes
2 answers

Why is `(a) read as a list while `(a b) isn't?

While learning clojure, I was very surprised to find out that these two objects are different types: (list? `(inc)) ;; true (list? `(inc 1)) ;; false In theory, I understand why the second form returns false, that object is actually a…
Malabarba
  • 4,473
  • 2
  • 30
  • 49
4
votes
3 answers

Building a compile time list incrementally in C++

In C++, is there a way to build a compile time list incrementally, in the following pattern? START_LIST(List) ADD_TO_LIST(List, int) ADD_TO_LIST(List, float) ADD_TO_LIST(List, double) END_LIST(List) The result of this should be equivalent to: using…
Ambroz Bizjak
  • 7,809
  • 1
  • 38
  • 49
4
votes
1 answer

Scheme cons won't take two number arguments

I've seen many instances of cons taking two numbers as arguments, and I've been told to pass pass two numbers as arguments into cons in a lab, but whenever I do I get the following error: > (cons 1 2) cons: second argument must be a list, but…
Christian Baker
  • 375
  • 2
  • 7
  • 22
4
votes
2 answers

The result of cons in "The Little Schemer"

On page 178, there is a question: what is the value of (cons rep-car (cons (cons rep-quote (cons (cons rep-a (cons rep-b (cons rep-c (quote ())))) (quote…
user1506414
3
votes
1 answer

How to pop a value from cons list?

In the chapter 15.1 of The Book an example of Box<> usage for recursive type (cons list) implementation is shown. I tried to implement a method for this cons list to pop the outermost value out of the list, leaving the list with whatever left or Nil…
Virgileo
  • 55
  • 4
3
votes
4 answers

Memory allocation in Lisp

> (cons 2 3) (2 . 3) The Lisp environment needs to allocate only a single cons cell to connect the two items. Above is from the Lisp book "Land of Lisp". I don't understand why this pair is only located in a single cons cell. What does the…
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
3
votes
3 answers

Python: functional programming with cons, car & cdr

There is this problem that goes like this: cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. Now, I have seen the…
Helen
  • 533
  • 12
  • 37
1 2
3
11 12