
Hi, I've written this function here: scramble [] = [] scramble [x] = [[z] | z <- scramble x] scramble (x:xs) = [(y:z)|y <- scramble x, z <- scramble xs] and (I think) it roughly does what I want it to: *Main> scramble ([]::[Int]) [] *Main> scramble ([1]::[Int]) [[1],[2]] *Main> scramble ([1,2]::[Int]) [[1,1],[1,2],[2,1],[2,2]] *Main> scramble ([1,2,3]::[Int]) [[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,1],[2,1,2],[2,2,1],[2,2,2]] Three questions: 1. What should I call this function? 2. Is there already one in the standard library that does the same thing? 3. Is it possible to rewrite it with only "scramble []" and "scramble (x:xs)" and not the "scramble[x]"? Thanks -John

John Ky wrote:
Hi,
I've written this function here:
scramble [] = [] scramble [x] = [[z] | z <- scramble x] scramble (x:xs) = [(y:z)|y <- scramble x, z <- scramble xs]
and (I think) it roughly does what I want it to:
That is strange, as it does not even type-check ;-) See the "y <- scramble x" part. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

2008/10/16 John Ky
Hi,
I've written this function here:
scramble [] = [] scramble [x] = [[z] | z <- scramble x] scramble (x:xs) = [(y:z)|y <- scramble x, z <- scramble xs]
and (I think) it roughly does what I want it to:
*Main> scramble ([]::[Int]) [] *Main> scramble ([1]::[Int]) [[1],[2]]
So, um, this is nonsense. You've given it only 1, and yet it outputs a 2, yet there is no mention of addition or the literal 2 anywhere in your function. This function looks a lot like the more sensible: scramble' n xs = sequence (replicate n xs)
scramble' 0 [1,2,3] [[]] scramble' 1 [1,2,3] [[1],[2],[3]] scramble' 2 [1,2,3] [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]
Where your examples all had [1,2] as the argument. Luke
participants (3)
-
Janis Voigtlaender
-
John Ky
-
Luke Palmer