I know how to do it by using a function that removes an element and then recursively use that function to generate all permutations. I now have a problem where I want to use the following function:
(define (insert-everywhere item lst)
(define (helper item L1 L2)
(if (null? L2) (cons (append L1 (cons item '())) '())
(cons (append L1 (cons item L2))
(helper item (append L1 (cons (car L2) '())) (cdr L2)))))
(helper item '() lst))
This function will insert the item into every possible location of the list, like the following:
(insert-everywhere 1 '(a
How would I use this function to get all permutations of a list?
eric.mercer, on 16 February 2012 - 07:58 PM, said:
So I am trying to write a Scheme function that takes a list and returns a list of lists, each is a different permutation of the argument.
I know how to do it by using a function that removes an element and then recursively use that function to generate all permutations. I now have a problem where I want to use the following function:
This function will insert the item into every possible location of the list, like the following:
(insert-everywhere 1 '(2 3)) will get: '((1 2 3) (2 1 3) (2 3 1))
How would I use this function to get all permutations of a list?
I know how to do it by using a function that removes an element and then recursively use that function to generate all permutations. I now have a problem where I want to use the following function:
(define (insert-everywhere item lst)
(define (helper item L1 L2)
(if (null? L2) (cons (append L1 (cons item '())) '())
(cons (append L1 (cons item L2))
(helper item (append L1 (cons (car L2) '())) (cdr L2)))))
(helper item '() lst))
This function will insert the item into every possible location of the list, like the following:
(insert-everywhere 1 '(2 3)) will get: '((1 2 3) (2 1 3) (2 3 1))
How would I use this function to get all permutations of a list?

New Topic/Question
Reply


MultiQuote




|