I'm working on a problem that has me using Racket with plait language and I'm trying to get a program that takes two lists and associate them together like this. I am relatively new to Racket and the plait language.
'(a b c d) and '(1 2 3 4)
Together they should output:
'((make-assoc 'a 1) (make-assoc 'b 2) (make-assoc 'c 3) (make-assoc 'd 4)))
This is what I have defined so far:
#lang plait
(define-type Associate
(assoc [name : Symbol]
[values : Number]))
(define (make-assoc [names : (Listof Symbol)] [values : (Listof Number)]): (Listof assoc)
(map (lambda (name value) (assoc name value)) names values))
(test (make-assoc '(a b c d) '(1 2 3 4))
'((make-assoc 'a 1) (make-assoc 'b 2) (make-assoc 'c 3) (make-assoc 'd 4)))
(test (make-assoc '(t a c o tuesday) '(0 1 34 1729 42))
'((make-assoc 't 0) (make-assoc 'a 1) (make-assoc 'c 34) (make-assoc 'o 1729) (make-assoc 'tuesday 42)))
I tried to get somewhere coming up stuff for make-assoc but I am having trouble with syntax and I guess I can't use map or lambda because make-assoc is already the identifier. So maybe I could use append in some way?
First of all, the type is
Associate, notassoc.associs the name of its only variant, and is used to construct values of theAssociatetype.So you should have
(You also need to rename it, as
make-associs already taken by the "machinery".)Second, the type of
mapisso it takes a one-argument function and one list and produces a list.
You want
map2, which has the typeSo,
Third, quoting "suspends" evaluation;
'(a b)is not the same as(list a b)– the former is a list of the symbolsaandbwhile the latter is a list of the values those symbols are bound to.So you want to use
listrather than'in your test cases.You also use
assoc, notmake-assoc, to create values of theAssociatetype.In summary:
And now,