Generate all possibilities in scheme from a list -


i have list of sublists:

((a b c) (e f) (z h))  

and want generate this:

((a e z) (a f z) (a e h) (a f h) (b e z) (b e h) ... ) , on. 

i want, given list of sublist, generate possibilities of sublists contains element each of input's sublists.

how can ouput?

you're describing cartesian product of list of lists, here's possible implementation (works in racket):

(define (cartesian-product lsts)   (foldr (lambda (lst acc)            (for*/list ((x (in-list lst))                        (y (in-list acc)))              (cons x y)))          '(())          lsts)) 

now, if you're not using racket, here's vanilla implementation using standard procedures; should work on scheme interpreter defines fold-right-like procedure:

(define (flatmap f lst)   (apply append (map f lst)))  (define (cartesian-product lsts)   (foldr (lambda (lst acc)            (flatmap (lambda (x)                       (map (lambda (y)                              (cons x y))                            acc))                     lst))          '(())          lsts)) 

either way, works expected:

(cartesian-product '((a b c) (e f) (z h)))  => '((a e z) (a e h) (a f z) (a f h) (b e z) (b e h)      (b f z) (b f h) (c e z) (c e h) (c f z) (c f h)) 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -