how would you do that?
Language similar to LISP
I don't think you can use cons twice.
really stuck.
maybe
(define a '('()))
how would you do that?
Language similar to LISP
I don't think you can use cons twice.
really stuck.
maybe
(define a '('()))
A list of lists in Scheme can be a literal or built at runtime. As correctly pointed out, a single-quote before a list literal produces a constant list structure; alternatively, use list or cons to construct one in code. The concern from about using cons repeatedly is unnecessary — you can nest cons calls — but ensure the second argument you cons onto is a proper list (otherwise you get an improper/dotted pair).
Example using list (clear and idiomatic):
(define a (list (list) (list) (list))) Example helper to produce N empty lists programmatically:
(define (n-empty-lists n)
(if (zero? n)
(list)
(cons (list) (n-empty-lists (- n 1))))) General notes and cautions: prefer list for readability and to create fresh lists rather than relying on quoted literals when you plan to mutate data. Mutating literal structures with set-car! or set-cdr! is unspecified and should be avoided. If you see a dotted pair printed, check that the cdr you passed to cons was actually a list, not some other value.
Like this: '(() () ()) -> 3 lists inside 1
(define a '(()))
You only have to put 1 quote..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.