在一般Lisp中传递函数列表作为参数 [英] Passing a list of functions as an argument in common Lisp

查看:200
本文介绍了在一般Lisp中传递函数列表作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个函数F.我想将一个函数列表作为参数传递给函数F.



函数F会遍历列表中的每个函数一个接一个地将每一个应用到两个整数:x和y。

例如,如果列表=(加,减,加,除,乘,加)和 x = 6 y = 2 ,结果如下: > 8 4 8 3 12 8

如何在普通的Lisp中实现这个功能?

解决方案

有很多种可能性。

  CL -user> (defun f(x y functions)
(mapcar(lambda(function)(funcall function x y))functions))
F
CL-USER> (f 6 2(list#'+#' - #'+#'/#'*#'+))
(8 4 8 3 12 8)
CL-USER> (defun f(x y functions)
(函数循环函数
collect(funcall函数x y)))
F
CL-USER> (f 6 2(list#'+#' - #'+#'/#'*#'+))
(8 4 8 3 12 8)
CL-USER> (函数(函数)
(cond((空函数)'())
(t(cons(funcall(car functions)xy)
(fxy(cdr functions))) )))
F
CL-USER> (f 6 2(list#'+#' - #'+#'/#'*#'+))
(8 4 8 3 12 8)
CL-USER> (def(f)(xy函数)
((rec(函数acc)
(cond((空函数)acc)
(t(rec(cdr函数)
cons(funcall(汽车功能)xy)
acc))))))
(反向(rec函数(列表)))))
F
CL-USER> (f 6 2(list#'+#' - #'+#'/#'*#'+))
(8 4 8 3 12 8)
CL-USER> (defun f(xy functions)
(flet((stepper(function result)
(cons(funcall function xy)result)))
(reduce#'stepper functions:from-end t :初始值'())))
F
CL-USER> (f 6 2(list#'+#' - #'+#'/#'*#'+))
(8 4 8 3 12 8)

等等。前两个是可读的,第三个可能是菜鸟怎么样在第一次Lisp课程中会做到这一点,第四名仍然是菜鸟,在听说过关于尾部呼叫优化的消息后,第五位是由Haskeller撰写的。


Say there is a function F. I want to pass a list of functions as an argument into function F.

Function F would go through each function in the list one by one and apply each one to two integers: x and y respectively.

For example, if the list = (plus, minus, plus, divide, times, plus) and x = 6 and y = 2, the output would look like this:

8 4 8 3 12 8

How do I implement this in common Lisp?

解决方案

There are many possibilities.

CL-USER> (defun f (x y functions)
           (mapcar (lambda (function) (funcall function x y)) functions))
F
CL-USER> (f 6 2 (list #'+ #'- #'+ #'/ #'* #'+))
(8 4 8 3 12 8)
CL-USER> (defun f (x y functions)
           (loop for function in functions
                 collect (funcall function x y)))
F
CL-USER> (f 6 2 (list #'+ #'- #'+ #'/ #'* #'+))
(8 4 8 3 12 8)
CL-USER> (defun f (x y functions)
           (cond ((null functions) '())
                 (t (cons (funcall (car functions) x y)
                          (f x y (cdr functions))))))
F
CL-USER> (f 6 2 (list #'+ #'- #'+ #'/ #'* #'+))
(8 4 8 3 12 8)
CL-USER> (defun f (x y functions)
           (labels ((rec (functions acc)
                      (cond ((null functions) acc)
                            (t (rec (cdr functions)
                                    (cons (funcall (car functions) x y)
                                          acc))))))
             (nreverse (rec functions (list)))))
F
CL-USER> (f 6 2 (list #'+ #'- #'+ #'/ #'* #'+))
(8 4 8 3 12 8)
CL-USER> (defun f (x y functions)
           (flet ((stepper (function result)
                    (cons (funcall function x y) result)))
             (reduce #'stepper functions :from-end t :initial-value '())))
F
CL-USER> (f 6 2 (list #'+ #'- #'+ #'/ #'* #'+))
(8 4 8 3 12 8)

And so on.

The first two are readable, the third one is probably how a rookie in a first Lisp course would do it, the fourth one is still the rookie, after he heard about tail call optimization, the fifth one is written by an under cover Haskeller.

这篇关于在一般Lisp中传递函数列表作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆