I'm trying to make a function that takes in two lists of atoms as a parameter and returns them as a list of pairs.
Example Input
(combine '(1 2 3 4 5) '(a b c d e))
Example Output
'((1 a) (2 b) (3 c) (4 d) (5 e))
However, I'm new to Racket and can't seem to figure out the specific syntax to do so. Here is the program that I have so far:
(define connect
(lambda (a b)
(cond [(> (length(list a)) (length(list b))) (error 'connect"first list too long")]
[(< (length(list a)) (length(list b))) (error 'connect"first list too short")]
[else (cons (cons (car a) (car b)) (connect(cdr a) (cdr b)))]
)))
When I run it, it gives me the error:
car: contract violation
expected: pair?
given: '()
Along with that, I don't believe the error checking here works either, because the program gives me the same error in the else
statement when I use lists of different lengths.
Can someone please help? The syntax of cons
doesn't make sense to me, and the documentation for Racket didn't help me solve this issue.
(define (unzip . lists) (apply map list lists))
? Works with any number of arguments. If you usemap
from SRFI-1 it will stop at the shortest list or signal an error with the standardmap
- Sylwesterconnect
,combine
orzip
? - Óscar Lópezlist
ona
orb
, this is wrong:(list a)
. This is becausea
is already a list, why do you want to put it inside another list? - Óscar López