Now, this looks good, but there is a problem: each stage of the recursion takes singleĮlements and turns them into a list. (lambda (j) (cons element j) (permute sublist))) Want to apply this operation to each element of the list, and the way toĭo that is using another map, giving: (map (lambda (element) Then (map (lambda (j) (cons element j)) (permute sublist)). So aīetter approach is just to use a lambda, which captures the value of Maybe overkill - especially since they all have the same form. Now, defining a new prepend function for each element is Permutations of the sublist would then be (map prepend (permute Operation of prepending the element to the sublist could be defined as (define (prepend j)Īnd the operation of doing it for all the Permutations of (b c), b preceding (a c) and (c a) and so on. True - we have a preceding (b c) and (c b), which are the Looking at the (a b c) case confirms that this is [element prepended to the permutations of list \ element, for every It turns out that the permutations of a list a precisely the set Permutations of a list of length (n - 1), and so on until we reach the Thisĭecrease in the degrees of freedom as we fix more and more of the firstĮlements of the list is very suggestive: maybe we can represent theįinding the permutations of a list of length n in terms of the In general there are n! permutations of a list of length n - we have nĬhoices for the first element, and once we've picked that, (n-1) choicesįor the second element, (n-2) for the third element, and so on. The permutations of a single element list are evidently just the list So the question is: what are the permutations Level before diving into an implementation and trying to understand I've always found it easier to understand the algorithm on a higher So we get a flat list of permutations out. Applying append flattens a list of lists, > (append '(1 2) '(3 4) '(5 6)) > (map (lambda (i) (map (lambda (j) (cons i j)) The outer map makes sure all permutations are generated by considering all elements of lst as the first element. So the inner map produces all permutations that start with some i, which we've set to 1 here. Now map the lambda over these permutations: > (map (lambda (j) (cons i j)) (permute (remove i lst))) Looks good: the inner expression removes an element and generates permutations of the remainder of the list, recursively. Fix lst and apply the inner expression to one of its elements. Let's pick this apart, going from the inside out. But the part that calls again to permute and remove with i, what is that part doing? It is just removing the head of the list to generate subsets of the list having the head of the pair, element i, fixed until it runs out of elements? So what is exactly doing this line: (apply append(map(lambda (i) (map (lambda (j)(cons i j))įor me it seems that it just wants to create a pair with two elements: i and j, which they will become a list with the elements permuted (if we take the assumption that a list is just a bunch of concatenated pairs). For what I know map appies a function to every element of an argument (in this case a list), and apply just applies one function one time completely to all the arguments. The part that I quite do not get it, is the permute function. The first function remove, it seems straightforward that only gets rid of the caracter denoted by x, even if its repeated or not, by comparing it with the beginning of the list and calling recursively with the rest of it. (else (apply append(map(lambda (i) (map (lambda (j)(cons i j)) (else (cons (car lst) (remove x (cdr lst)))))) The code is the following: (define (remove x lst) The simplest is actually to just roll all three "dice", and if the results don't meet your constraint, roll them all again.I have found the following piece of code that it makes permutation in Scheme. So there's a few ways to get "good" randomness. But that's not the case with these other approaches! So having a 3 as the first number should be four times as likely as having a 6. In reality, is not nearly 1/6th of all acceptable rolls. That's because after you've rolled the 6 (at 1/6 probability), the only acceptable remaining numbers are 1 and 1. If you roll them one at a time, there's a 1/6 chance that the permutation you end up with will be. Say you want to roll three dice where they add up to 8. By choosing the first number, then the second, then the third, you have a large bias towards the extremes. All of the answers so far do not have good randomness properties.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |