简体   繁体   中英

Racket Lang--Scheme How to combine a list of variables and values for an environment

I am brand new to scheme and trying to create a real simple interpreter as a starting off point.

Given two lists, one containing variables in the form:

(x y z) 

and a second containing their values:

(1 2 3)

how can i combine them to make one list that looks like this:

((x 1) (y 2) (z 3))

I will then append this to my original environment. I'm struggling with just getting used to these simple operations with this type of programming language. Thanks.

使用map

(map list '(x y z) '(1 2 3))

I'm currently a university student using the Begining Student environment of the Dr. Racket. So there might be some differences in terms of the notations and syntax.

(define (combine-list list1 list2)
  (cond
;; Using (cond) function to create a recursion
    [(or (empty? list1) (empty? list2)) empty]
;; The base case: when to stop the recursion
;; It would be written in the first line cuz it will be evaluated first
    [else (cons (list (first list1) (first list2))
;; The recursive case: construct one typical element of the resulting list
                (combine-list (rest list1) (rest list2)))]))
;; Do the recursive case on all of the rest of the lists

I haven't learned what (map) does,but in my opinion, the function (combine-list) has the same behaviour as (map) if the inputs are two arguments, like what @soegaard did in the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM