微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有直接映射的方案中的组合?

如何解决没有直接映射的方案中的组合?

与此问题相关:Pair combinations in scheme,我正在尝试编写一个函数来创建列表的可能序列。我还尝试用一些 let 为自己注释,而不是将所有内容都放在 map 中。这是我目前所拥有的:

(define (remove-from-list elem L)
  (filter (lambda (x) (not (= x elem))) L))

(define (prepend-element-to-list-of-lists elem L)
  (map (lambda (x) (append (list elem) x)) L))

(define (perm L)
  ; returns a list of lists,so base case will be '(()) rather than '()
  (if (null? L) '(())
      ; we will take out the first element,this is our "prepend-item"
      (let ((prepend-element (car L))
            (list-minus-self (remove-from-list (car L) L)))
        ; prepend-to-list-of-lists
        (let ((other-lists-minus-self (perm list-minus-self)))
          (prepend-element-to-list-of-lists prepend-element other-lists-minus-self)
          ))))
(perm3 '(1 2 3))
((1 2 3)) ; seems to be stopping before doing the recursive cases/iterations.

在这里尝试做的是取出列表的第一个元素,并将其添加到由没有该元素的过程创建的所有列表列表中。例如,对于 [1,2,3],第一种情况是:

取出 1 --> 前置于 [2,3] 的组合,所以最终它涉及到 [1,3][1,3,2]

但是,我想看看我是否可以在没有 map 的情况下做到这一点,而只是调用它自己。有没有办法做到这一点,或者 map 是对 1 执行上述操作的唯一方法,然后是 2,然后是 3,...

与此相关,对于“正常工作的情况”,为什么以下内容会一直嵌套括号?

(define (perm L)
  (if (null? L) '(())
        ; (apply append     <-- why is this part required?
        (map (lambda (elem)
               (map (lambda (other_list) (cons elem other_list))
                    (perm (remove-from-list elem L)))) 
              L)))
          ; )

也就是说,如果没有在 (apply append) 之外执行 map,我会得到“正确”的答案,但是有大量嵌套的括号:(((1 (2 (3))) (1 (3 (2)))) ((2 (1 (3))) (2 (3 (1)))) ((3 (1 (2))) (3 (2 (1)))))。我想如果有人可以展示一个更基本的设置示例,其中地图“望远镜”没有可能有用的大功能

解决方法

关于“括号从何而来”,它是关于类型的:被映射的函数将“元素”变成一个“元素列表”,所以如果你{ {1}} 遍历元素列表,将列表中的每个元素转换为元素列表:....

map

,比如说,(一般来说;而不是那些有问题的功能)。因为那里有递归,所以我们有类似

[    1,2,3,]   --> 

[ [  1a,1b ],[2a],[] ]

,等等。

所以 [ [ [1a1],[] ],[[]],[] ] map foo

listof elts -> listof (listof elts)

但是如果我们在每一步的 `foo` is: elt -> (listof elts) ------------------------------------------------------- `map foo` is: listof elts -> listof (listof elts) 之后apply append,我们已经将它调整到 map

listof elts -> listof elts

所以没有新的parens出现——因为当它们出现时,它们在每一步都被调平,所以它们不会像那样积累;嵌套级别保持不变。

这就是 `map foo`: listof elts -> listof (listof elts) `apply append`: listof (listof elts) -> listof elts ---------------------------------------------------------------------- `flatmap foo`: listof elts -> listof elts 的作用:它删除了 inner 括号:

apply append

举个例子

(apply append [ [x,...],[y,[z,...] ] )  ==

(      append   [x,...]  [y,...]  [z,...]   )  == 

              [  x,...,y,z,...  ]

既然类型合适,> (define (func x) (if (= 0 (remainder x 3)) '() (if (= 0 (remainder x 2)) (list (+ x 1)) (list (+ x 1) (+ x 2))))) > (display (map func (list 1 2 3 4))) ((2 3) (3) () (5)) > (display (map (lambda (xs) (map func xs)) (map func (list 1 2 3 4)))) (((3) ()) (()) () ((6 7))) > (display (flatmap func (list 1 2 3 4))) (2 3 3 5) > (display (flatmap func (flatmap func (list 1 2 3 4)))) (3 6 7) s compose 就很好,不像没有扁平化。在该函数的递归过程中也会发生同样的情况。更深层次的递归适用于更深层次的结果列表。如果没有扁平化,这会产生更多的嵌套。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。