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

Erlang: which pattern matching is more efficient (lists)?

I'm going through "Pragmatic Programming Erlang" where there is a function defined like this: split("\r\n\r\n" ++ T, L) -> {reverse(L), T}; split([H|T], L) -> split(T, [H|L]); split([], _) -> more. What interests me is first match, namely…
cji
  • 6,635
  • 2
  • 20
  • 16
9
votes
6 answers

LISP cons in python

Is there an equivalent of cons in Python? (any version above 2.5) If so, is it built in? Or do I need easy_install do get a module?
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
9
votes
3 answers

What does [a|b|c] evaluate to in SWI-Prolog?

The pipe operator in prolog returns one or more atomic Heads and a Tail list. ?- [a,b,c] = [a,b|[c]]. true. Nesting multiple pipes in a single match can be done similar to this: ?- [a,b,c] = [a|[b|[c]]]. true. What does the statement [a|b|c] infer…
Ambrose
  • 1,220
  • 12
  • 18
7
votes
1 answer

Why is line-seq returning clojure.lang.Cons instead of clojure.lang.LazySeq?

According to the ClojureDocs entry for line-seq (http://clojuredocs.org/clojure_core/clojure.core/line-seq) and the accepted answer for the Stack question (In Clojure 1.3, How to read and write a file), line-seq should return a lazy seq when passed…
user1922460
6
votes
3 answers

Does ruby support an enumerable map_cons method or its equivalent?

Ruby has a handy function for enumerables called each_cons. Which "Iterates the given block for each array of consecutive elements." This is really nice. Except that this is definitely an each method, which returns nil upon completion and not an…
kingsfoil
  • 3,795
  • 7
  • 32
  • 56
5
votes
3 answers

cons :: operator for sequences in F#?

Is there a better code that does not need to transform the sequence into a list ? let rec addentry map keys = match keys with | ((i,j) :: tail) -> Map.add (i,j) ((inputd.[i]).[j]) (addentry map tail) | ([]) -> map addentry Map.empty…
nicolas
  • 9,549
  • 3
  • 39
  • 83
5
votes
2 answers

fst and 3-tuple in fsharp

Do you know the nicest way to make this work : let toTableau2D (seqinit:seq<'a*'b*'c>) = let myfst = fun (a,b,c) -> a let myscd = fun (a,b,c) -> b let mytrd = fun (a,b,c) -> c let inputd = seqinit |> groupBy2 myfst myscd there must be…
nicolas
  • 9,549
  • 3
  • 39
  • 83
5
votes
1 answer

cons operator (::) performance in F#

In official documentation it is stated that :: is faster than @. Once all lists are immutable in F#, why is there a difference? In any case the original list should be copied. but in case with :: we will prepend, while in case with @ we will append.…
dr11
  • 5,166
  • 11
  • 35
  • 77
5
votes
4 answers

32-bit or 64-bit application on 64-bit OS?

We are developing a swing application written by Java which requires only about 128MB memory, and in the short future I don't see it will require much more memory like 4GB. Previously we provide always 3 different releases, one for 32-bit Windows,…
Wayne Xu
  • 53
  • 4
5
votes
4 answers

How to do ((A.B).(C.D)) in lisp

I'm trying to figure out how to do this using cons: ((A . B) . (C . D)) where (A . B) and (C . D) are in each cons cell I've tried doing this (cons (cons 'a 'b) (cons 'c 'd)) but it gives me this: ((A.B) C . D) I also tried this: (cons (cons 'a 'b)…
5
votes
2 answers

perl6 functions similar to "car cdr cons" in scheme?

I really love perl6 and scheme. I am wondering if there are functions in perl6 that are similar to the "cons, car, cdr" functions in scheme? What I have been doing feels cumbersome: sub cons($a, $aList) { return flat($a, $aList); } # sometimes flat…
lisprogtor
  • 5,677
  • 11
  • 17
5
votes
1 answer

Where is Reason's cons (::) operator?

The cons (::) operator is a fundamental part of 1) writing recursive list functions in OCaml and similar languages, and 2) pattern matching on lists. However, I can't find anything in Reason's documentation concerning cons, and in the REPL, this…
jayelm
  • 7,236
  • 5
  • 43
  • 61
5
votes
0 answers

Statically typed well-formed cons list in C#

I've asked myself a stupid question and have been wracking my brain for a few hours now with no result. Let's say you want to implement a cons cell in C# for whatever reason (Greenspun's tenth rule should be enough). Since it's in C#, you want it to…
Alexey
  • 1,354
  • 13
  • 30
5
votes
4 answers

Difference between multiple values and plain tuples in Racket?

What is the difference between values and list or cons in Racket or Scheme? When is it better to use one over the other? For example, what would be the disadvantage if quotient/remainder returns (cons _ _) rather than (values _ _)?
Alex
  • 1,184
  • 7
  • 15
5
votes
4 answers

How do I create a unique object?

I want class MyClass(object): _my_unique = ???? # if this were lisp, I would have written (cons nil nil) here def myFunc (self, arg): assert arg != _my_unique # this must never fail ... What do use instead of ???…
sds
  • 58,617
  • 29
  • 161
  • 278
1
2
3
11 12