简体   繁体   English

将功能列表应用于球拍中的值列表

[英]Applying list of functions to list of values in Racket

I'm new to Racket and scheme language in general and I'm having a hard time implementing my ideas. 我一般对球拍和方案语言都不熟悉,很难实现自己的想法。

Basically, I have a list of functions (lets call it List f ) and a list of strings (lets call it List s ). 基本上,我有一个函数列表(将其称为List f )和一个字符串列表(将其称为List s )。 What I need to do is, for each function in f, execute the function and store the value in another list (lets call is List done ). 我需要为f中的每个函数执行该函数,并将值存储在另一个列表中(让调用为List done )。

For example: say I have List A = (f1 f2) and List B = (abc) , I would execute: 例如:说我有List A = (f1 f2)List B = (abc) ,我将执行:

f1 a
f1 b
f1 c
f2 a
f2 b
f2 c

and they would all append their values into List done 他们都会将其值附加到List done

I can also not use any form of set in racket. 我也不能使用任何形式的set球拍。

I understand how this is suppose to work and could easily code this in C or java, but scheme is giving me trouble. 我了解这是如何工作的,并且可以轻松地用C或Java编写此代码,但是方案给我带来了麻烦。

In Racket, there are lots of built-in procedures that make it easy to solve problems that involve manipulating lists. 在Racket中,有许多内置过程可轻松解决涉及操作列表的问题。 For your example, you're looking for for*/list : 对于您的示例,您正在寻找for*/list

(define A (list string-upcase string-downcase))
(define B (list "Aa" "Bb" "Cc"))

(define done
  (for*/list ([f A] [s B])
    (f s)))

It iterates over all the elements in A , assigning each one in turn to the variable f . 遍历A所有元素,依次将每个元素分配给变量f And in a nested loop, it also iterates over all the elements in B , assigning each one in turn to the variable s . 并且在嵌套循环中,它还会遍历B所有元素,依次将每个元素分配给变量s In the body of the loop it applies each f to all s , and collects everything in an output list. 在循环的主体中,它将每个f应用于所有s ,并将所有内容收集在输出列表中。 Now done contains the expected values: 现在done包含预期值:

done
=> '("AA" "BB" "CC" "aa" "bb" "cc")

If we were to do this by hand using recursion, it'd be more work. 如果我们要使用递归手动完成此操作,则将需要更多工作。 A possible implementation would be to have a procedure to do the "outer" loop, which would call another procedure to do the "inner" loop. 可能的实现方式是使一个过程执行“外部”循环,这将调用另一个过程进行“内部”循环。 A third procedure would start the recursion and combine the results: 第三个过程将启动递归并合并结果:

(define (outer-loop funcs params)
  (if (null? funcs)
      '()
      (cons (inner-loop (car funcs) params)
            (outer-loop (cdr funcs) params))))

(define (inner-loop f params)
  (if (null? params)
      '()
      (cons (f (car params))
            (inner-loop f (cdr params)))))

(define (apply-funcs funcs params)
  (apply append ; required to "flatten" the list of lists
         (outer-loop funcs params)))

We'd use it like this: 我们将像这样使用它:

(define done (apply-funcs A B))

At first you should learn how to solve list problems by hand, implementing your own loops. 首先,您应该学习如何手工解决列表问题,实现自己的循环。 Once you're confident that you understand what you're doing, spend some time learning about the existing list procedures and iterations and comprehensions , the idiomatic way to use the language in real life. 一旦确信自己了解自己在做什么,就花一些时间来学习现有的列表过程以及迭代和理解 ,这是在现实生活中使用该语言的惯用方式。

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

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