![](/img/trans.png)
[英]Using Racket ISL to define functions using list abstractions and local/lambda
[英]List of Permutations in Racket without using Functions like map and lambda
我无法编写与 map 和 lambda 相同的工作的助手 function
(define (permutations lst)
(cond [(empty? lst) '()]
[(empty? (rest lst)) (list lst)]
[else???]))
map
只是一个抽象。 想象一下,你做了一个典型的吃名单者:
;; add 2 to each element
(define (add2 lst)
(if (null? lst)
'()
(cons (+ 2 (car lst))
(add2 (cdr lst)))))
(add2 '(1 2 3)) ; ==> (3 4 5)
;; reverse each sublist in a list
(define (revel lst)
(if (null? lst)
'()
(cons (reverse (car lst))
(revel (cdr lst)))))
(revel '((1 2) (3 4))) ; ==> ((2 1) (4 3))
你注意到这两个有多相似吗? 几乎 90% 的代码相同。 通用代码是map
的核心:
(define (map1 f lst)
(if (null? lst)
'()
(cons (f (car lst))
(map1 f (cdr lst)))))
广告现在我们可以使用map1
来实现这两者
(define (add2 lst)
(map1 (lambda (v) (+ v 2)) lst))
(define (revel lst)
(map1 reverse lst))
当然map
比map1
更多,因为它还支持更多列表 arguments,但是实现完整的map
超出了此 Z31A1FD14EC14BEF2D18ZE 后的 Z31A1FD140BE4BEF2D58ZE
因此,这是执行此操作的一种方法。 我假设您被允许编写辅助函数。
第一步是要意识到,在某些时候,您将需要获取一个元素列表并通过它“穿线”一些东西,生成所有可能的新列表,并将该元素插入某处。 所以给定(1 2)
和3
你想要((3 1 2) (1 3 2) (1 2 3))
。
出于显而易见的原因,我将以 function将其答案添加到一些先前存在的答案的方式来执行此操作。 它本身将有一个小帮手 function。
(define (thread-list e l into)
;; Thread e through l accumulating into into
(thread-list-loop e '() l into))
(define (thread-list-loop e before after into)
;; Do the work of threading e through a list:
;; - before is the chunk of list before e;
;; - after is the chunk after e;
;; - into is what we're collecting into
(if (null? after)
;; We now just need before with e at the end
(cons (append before (list e)) into)
;; add the current thing to results and loop
(thread-list-loop
e
(append before (list (first after)))
(rest after)
(cons (append before (list e) after) into))))
所以让我们测试一下:
> (thread-list 1 '(2 3) '())
'((2 3 1) (2 1 3) (1 2 3))
现在我们想要一个 function 它将接受一个元素和一堆列表,并将元素穿过每个列表,累积结果:
(define (thread-lists e lists into)
;; thread e through each element of lists, accumulating into into
(if (null? lists)
into
(thread-lists
e
(rest lists)
(thread-list e (first lists) into))))
并检查:
> (thread-lists 1 '((2 3) (3 2)) '())
'((3 2 1) (3 1 2) (1 3 2) (2 3 1) (2 1 3) (1 2 3))
我将在这里停下来,但看看上面的调用做了什么:给定((2 3) (3 2))
,这是(2 3)
的排列,和1
,它返回了(1 2 3)
,所以您现在应该能够编写另一个函数permutations
,它利用thread-lists
来创建排列。
注意我不知道这种方法是否有效,我希望它不会。 我认为这是相当清楚的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.