如何使用过程的n个应用程序创建列表 [英] How to create a list with n applications of a procedure

查看:59
本文介绍了如何使用过程的n个应用程序创建列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这一个有关,但就我而言,我想获得一个列表,其中包含一个函数的n个应用程序的结果,该应用程序的输出无法与先前的结果再次进行计算(例如,从列表中随机选择一个元素).

My question is related to this one but in my case I would like to obtain a list with the results of n applications of a function whose output is not computable again with the previous result (picking randomly an element from a list, for example).

也就是说,函数n本身不是n次组成函数,而是将n个结果一起显示在列表中.

That is, it is not the composition of a function n times with itself but the n results shown together into a list.

推荐答案

像这样吗?

#!racket/base
(require srfi/1)

(define (times/list proc n)
  (unfold-right zero? proc sub1 n))

(times/list (lambda (v) (abs (- v 5))) 10)
; ==> (4 3 2 1 0 1 2 3 4 5)

(times/list (lambda _ 5) 10)
; ==> (5 5 5 5 5 5 5 5 5 5)

(times/list (lambda _ (+ 1 (random 5))) 10)
; ==> (4 2 2 4 4 1 5 3 1 3) (varies)

这篇关于如何使用过程的n个应用程序创建列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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