[Haskell-cafe] What is this function? (RESOLVED)
John Ky
newhoggy at gmail.com
Thu Oct 16 20:46:06 EDT 2008
Hi Luke,
Thankyou so much. I need the sequence function.
What I was after was this:
class Scramblable a where
scramble :: a -> [a]
instance Scramblable [MyType] where
scramble values = sequence (map scramble values)
instance Scramblable MyType where
scramble myType = {- blah blah -}
I was basically after exhaustively generating lots ASTs given a template AST
with lots of leaf values changed.
Thanks,
-John
On Thu, Oct 16, 2008 at 8:39 PM, Luke Palmer <lrpalmer at gmail.com> wrote:
> 2008/10/16 John Ky <newhoggy at gmail.com>:
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081017/296e57eb/attachment.htm
More information about the Haskell-Cafe
mailing list