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

React constant with parameters using square brackets

I'm new to React. I have the code below with a function, but when I run it, it returns an error: TypeError: renderJson[item.node] is not a function. How can I fix the renderJson function? export const readItem = item => { printlog(item); …
ccd
  • 23
  • 2
0
votes
4 answers

Trying to print a triangle recursively in lisp

Trying to print a triangle recursively in lisp. I get an overflow but I don't know from where. Mind you I am new to Lisp programming. (defun triangle (n) (if (not (oddp n))(progn (print "This is not an odd integer") (return-from…
Mike
  • 9
  • 1
0
votes
1 answer

Fixing this undefined-function in lisp code?

I am losing my mind over evaluating this code to find the error. I am not sure why I am getting an undefined function. Would someone please guide me to understand why? thanks! O is supposed to be an object, and L a list. I am trying to attach O to…
Drunkhydra
  • 47
  • 2
0
votes
0 answers

How to implement a Cons list prepend without a useless assignment?

I am going through Testcase: linked-list in Rust by Example. RBE has implemented prepend method by passing self by value: fn prepend(self, elem: u32) -> List { // `Cons` also has type List Cons(elem, Box::new(self)) } and calls it as: list…
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0
votes
2 answers

Different rules for cons arguments?

I'm new in Racket language and I bumped into an issue. I'm now trying to implement Binary Search Tree using cons(list). This is a simple BST I tried to make: For this BST, if I convert this into Racket list, this can be one possibility: '(6,…
cadenzah
  • 928
  • 3
  • 10
  • 23
0
votes
3 answers

Can't cons cells be implemented efficiently at the library level in Clojure?

Clojure has its own collections, and has no need of the traditional lispy cons cells. But I find the concept interesting, and it is used in some teaching materials (e.g., SICP). I have been wondering if there is any reasons that this cons primitive…
HappyFace
  • 3,439
  • 2
  • 24
  • 43
0
votes
1 answer

cons function does not accept my input

Well, I am trying to add two elements to become a list, and from the video of Prof. Colleen Lewis at minute 4 and 53 secs, and from the official racket site I saw that the best practice is to use "cons". When I did: (cons (Listof Number) (Listof…
Tomer
  • 531
  • 7
  • 19
0
votes
1 answer

This expression has type 'a list but an expression was expected of type 'a

I have a function in OCaml which should merge two lists: let rec merge (list1, list2) = match (list1, list2) with ([], []) -> merge (List.tl list1, List.tl list2) :: List.hd list1 :: List.hd list2 |(_, []) -> merge (List.tl…
Andrej Korowacki
  • 169
  • 2
  • 4
  • 10
0
votes
1 answer

How to generate a generic TypeScript type signature for a cons cell?

I'm struggling to find a generic type signature for a cons cell, that allows for the selector function to be typed. I.e. The code I want to be typed: const cons = (head, tail) => selector => selector(head, tail); I want to assign a generic type,…
YouCodeThings
  • 590
  • 3
  • 13
0
votes
3 answers

How to create dotted pair with nil

I have a list of queue names in the following form: '("foo" "bar") I am trying to store queues as assoc list in the following way: '(("foo" . nil) ("bar" . nil)) Basically it is an assoc list with queues "foo" and "bar" which are currently empty.…
dptd
  • 274
  • 1
  • 3
  • 12
0
votes
1 answer

Scheme Program processing sub lists into one list

I have just started learning scheme, and I find the cons-cdr part a bit hard to understand. I am making a function which takes a list, then displays all the atoms in that list, including in the sub lists as if it were one big list. It would look…
P. Rick
  • 3
  • 4
0
votes
0 answers

Scheme: mutating local variable in different environments mutates the same object

Consider these two similar procedures: (define (append-internal-cons obj) (let ((local-var (cons 1 '()) )) (append! local-var (list obj)) local-var)) (define (append-internal-no-cons obj) (let ((local-var '(1) )) (append! local-var…
plafer
  • 185
  • 12
0
votes
2 answers

Scheme: how to produce '(5 . (5))

I have tried all kinds of combinations of cons and append to produce '(5 . (5)) but I couldn't. Is there any way?
Bibrak
  • 544
  • 4
  • 20
0
votes
1 answer

Scheme Cons to create a list out of 2 lists

So, I have a school project in Scheme (working in Dr.Racket enviorment), here is a short run what it is (please note that im not asking any of you to do my schoolwork for me). I have 2 lists : one is 6 or more characters and the other one is 3…
Dorirot9
  • 11
  • 3
0
votes
1 answer

Multiply in Scheme for cons lists

I was able to make a scheme code to add two cons lists in scheme. say , list1 - '( p . d) list 2 ' ( p p p . d) my custom add function using cdr & car concepts , can do ( p p p p . d) as is expected. However, I now want to multiply both & based on…
user222040
  • 33
  • 6