(Scheme) Recursive function to compute all possible combinations of some lists?

前端 未结 2 373
灰色年华
灰色年华 2021-01-06 20:13

What is an example of a recursive function to compute all possible combinations of lists? For example, (combine (list 1 2 3) (list 1 2)) should return \'

2条回答
  •  清歌不尽
    2021-01-06 20:54

    Here's my solution. Requires SRFIs 1 and 26 to be available.

    (define (cartesian-product first . rest)
      (define (iter l result)
        (define (prepend-all x)
          (map (cut cons <> x) l))
        (concatenate (map prepend-all result)))
      (map reverse (fold iter (map list first) rest)))
    

提交回复
热议问题