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.

164 questions
90
votes
3 answers

What does "my other car is a cdr" mean?

Can anyone well versed in lisp explain this joke to me? I've done some reading on functional programming languages and know that CAR/CDR mean Contents of Address/Decrement Register but I still don't really understand the humour.
CaptainCasey
  • 1,311
  • 1
  • 14
  • 21
46
votes
2 answers

What's the difference between `::` and `+:` for prepending to a list)?

List has 2 methods that are specified to prepend an element to an (immutable) list: +: (implementing Seq.+:), and :: (defined only in List) +: technically has a more general type signature— def +:[B >: A, That](elem: B)(implicit bf:…
Mechanical snail
  • 26,499
  • 14
  • 83
  • 107
25
votes
1 answer

clojure cons vs conj with lazy-seq

Why does cons work in this context with lazy-seq, but conj doesn't? This works: (defn compound-interest [p i] (cons p (lazy-seq (compound-interest (* p (+ 1 i)) i)))) This doesn't (it gives a stack overflow exception): (defn compound-interest2…
caleb
  • 2,287
  • 24
  • 21
22
votes
7 answers

Kotlin prepend element

I am searching for Kotlin alternative to: (cons 1 '(2 3)) in lisp or 1 : [2, 3] in haskell or 1 :: List(2, 3) in scala, (which all result in sth like [1, 2, 3]) so I can prepend an element to a List (or any other list you can offer). It will also…
Columpio
  • 323
  • 1
  • 2
  • 5
20
votes
2 answers

Understanding infix method call and cons operator(::) in Scala

I'm quite new to Scala programming language, and was trying something out stucked in my mind while I was following the lecture notes at here. I think I couldn't really understand how cons operator works, here are some things I tried: I've created a…
ciuncan
  • 992
  • 2
  • 10
  • 23
16
votes
3 answers

OCaml cons (::) operator?

In OCaml, is there a way to refer to the cons operator by itself? For example, I can use (+) and ( * ) as int -> int -> int functions, but I cannot use (::) as a 'a -> 'a list -> 'a list function, as the following example show: # (+) 3 5;; - : int =…
Alan C
  • 343
  • 1
  • 2
  • 8
14
votes
5 answers

common lisp cons creates a list from two symbols, clojure cons requires a seq to cons onto?

(Disclaimer - I'm aware of the significance of Seqs in Clojure) In common lisp the cons function can be used to combine two symbols into a list: (def s 'x) (def l 'y) (cons s l) In clojure - you can only cons onto a sequence - cons hasn't been…
hawkeye
  • 31,052
  • 27
  • 133
  • 271
14
votes
4 answers

Is an empty list in Lisp built from a cons cell?

I'm trying to emulate Lisp-like list in JavaScript (just an exercise with no practical reason), but I'm struggling to figure out how to best represent an empty list. Is an empty list just a nil value or is it under the hood stored in a cons cell? I…
Jan Wrobel
  • 6,419
  • 1
  • 31
  • 49
13
votes
3 answers

Understanding pattern matching with cons operator

In "Programming F#" I came across a pattern-matching like this one (I simplified a bit): let rec len list = match list with | [] -> 0 | [_] -> 1 | head :: tail -> 1 + len tail;; Practically, I understand that the last match recognizes the…
Mathias
  • 14,541
  • 9
  • 55
  • 92
13
votes
5 answers

Accumulators, conj and recursion

I've solved 45 problems from 4clojure.com and I noticed a recurring problem in the way I try to solve some problems using recursion and accumulators. I'll try to explain the best I can what I'm doing to end up with fugly solutions hoping that some…
Cedric Martin
  • 5,798
  • 4
  • 26
  • 61
12
votes
4 answers

clojure: no cons cells

i heard that clojure does not have cons cells as of most lisp languages. does that mean a clojure list does not end with an empty list? could anyone explain what that exactly means?
象嘉道
  • 3,303
  • 5
  • 26
  • 48
12
votes
4 answers

How does "Cons" work in Lisp?

I was studying Lisp and I am not experienced in Lisp programming. In a part of my studies I encountered the below examples: > (cons ‘a ‘(a b)) ----> (A A B) > (cons ‘(a b) ‘a) ----> ((A B).A) I was wondering why when we have (cons ‘a ‘(a b)) the…
Amir Jalilifard
  • 1,663
  • 2
  • 21
  • 32
11
votes
1 answer

CAS vs Okta Vs Keycloak comparisions as an SSO solution

Which is the best option for SSO implementation Keycloack Vs CAS Vs Okta? I'm specifically looking for the disadvantages of each service to identify the best suitability for my system.
Sachin Arora
  • 113
  • 1
  • 1
  • 4
11
votes
3 answers

Unexpected output with cons()

I am from an imperative background but these days trying my hands on LISP (Common LISP) I read here about cons that (cons x L): Given a LISP object x and a list L, evaluating (cons x L) creates a list containing x followed by the elements in…
Prasoon Saurav
  • 85,400
  • 43
  • 231
  • 337
11
votes
1 answer

Cons element to list vs cons list to element in Scheme

What's the difference between using cons to combine an element to a list and using cons to combine a list to an element in scheme? Furthermore, how exactly does cons work? Does it add element to the end of the list or the beginning? Thanks!
Maritha Wang
  • 165
  • 2
  • 8
1
2 3
10 11