简体   繁体   English

Racket Lang--方案如何为环境组合变量和值列表

[英]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. 我目前是一名使用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. 我尚未了解(map)的作用,但我认为,如果输入是两个参数,则函数(combine-list)与(map)具有相同的行为,就像@soegaard在答案中所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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