Dotted list in lisp - list

I'm trying to recreate this structure with lisp (key . (list of values))
Ex: (a . (b c))
I managed to recreate the opposite ((a b) . c), but is not what i need.
Is possible?

(cons 'a (list 'b 'c))
Note that when you print this it will be printed as
(A B C)
because a cons whose cdr is a list is printed using list notation, not dotted notation.

Related

What is the result of this in scheme: (cdr '((a b c d)))

Isn't it supposed to be (a b c d) ??
When I try it on Racket it gives me () .
'((a b c d)) is a list, containing only one element, that is the four-element list (a b c d). So if you get the car of it, you obtain as result (a b c d), while the cdr produces correctly the empty list ().
How would you create such a list using cons?
(cons (cons 'a (cons 'b (cons 'c (cons 'd '()))))
'())
or
(cons '(a b c d) '())
cdr returns the second element of the given pair, so it returns '().

How to create a dictionary mapping on custom dictionary in Racket?

I have defined dictionary as this judgment in BNF grammar:
d ::= () (any boolean) (list cons d d)
Meaning, dictionaries are empty, or (any boolean) or a list of such pairs.
If I want to create a mapping, say "a true", how do I do it?
If I do
(define-values (d) (values '(a true)))
it just creates a new d, doesn't map to previous judgment d defined.
IIUC you want your dictionary to be just an association list:
(define d (list (cons 'x #t) (cons 'y #f)))
Depending on how are you going to implement the add operation you could either set! a new mapping:
(set! d (cons (cons 'z #t) d))
Or just create a new list (preferred):
(define d (list (cons 'z #t) (cons 'x #t) (cons 'y #f)))
Either way, the dictionary d will have the new mapping in the expected format:
'((z . #t) (x . #t) (y . #f))

Is possible to check is list proper or improper in scheme?

I'm trying to create function which checks is list proper or improper. Can't find any solution in the internet. Is this possible?
For example we have code using list?:
(define (proper-list? list)
(cond
((list? list) '(it's a proper list))
(else '(it's an improper list))))
(proper-list? '(a b c))
; -> (it's a proper list) - OK
(proper-list? '(a b . c))
; -> (it's an improper list) - OK
(proper-list? '(a . b))
; -> (it's an improper list)
; - NOT OK. it should return (it's a pair)
How to distinguish improper list from pair?
The function list? checks wether a list is proper or not:
(list? '(1 2 3)) ; ==> #t
(list? '(1 2 . 3)) ; ==> #f
Use this in your function to return either of the two lists in your specification.
EDIT
Because of the strange requirement that (a . b) would not be considered a improper list I think you should add an extra term in you cond that differentiate the two improper lists of one pair in chain and more than one pair in chain by using pair? in the cdr. (pair? (cdr '(a . b)) ; ==> #f and (pair? (cdr '(a b . c)) ; ==> #t

Scheme Zip Function

I need to write a function in Scheme that acts as a zipper - meaning it takes two lists and creates a 'zipper' out of the two lists such that
(zip '(a b c) '(1 2 3)) => ((a 1) (b 2) (c 3))
Furthermore it takes each element in the nth position and pairs them together.
I'm fairly new to Scheme, so any help would be much appreciated!
Thanks!
From my Standard Prelude: (define (zip . xss) (apply map list xss))

Why does '(a . b . c) evaluate to (b a c) in PLT-Scheme 372?

I'm trying to understand the relations between pair, cons, dotted tuples and proper list in PLT-Scheme 372. The detailed context of my question is as follows:
After reading some textbook and doing trial-and-error, I have got the following understanding and intuitive ideas (I may be wrong...):
all lists are pairs, e.g.:
(pair? (list 'a 'b 'c)) => #t
all conses are pairs, e.g.:
(pair? (cons 'a (cons 'b 'c))) => #t
some dot-separated tuples are pairs, e.g.:
(pair? '(a . b)) => #t
(pair? '(a . b . c)) => #t in Standard R5RS it's not legal syntax.
Then I bumped into this problem: Why does '(a . b . c) evaluate to (b a c)? Where can I find a complete usage manual of dot?
'(a . b) => (a . b)
'(a . b . c) => (b a c)
'(a . b . c . d) => illegal use of `.'
'(cons 'a (cons 'b 'c)) => (a b . c)
For Racket (PLT), there is a good description here.
Regarding the syntax (a . b . c) look at the bottom of the page, it's a Racket-specific reader extension designed to express typical tests like (< 1 2) as (1 . < . 2).