[Haskell-cafe] Re: Function to detect duplicates

Daniel Fischer daniel.is.fischer at web.de
Fri Feb 26 16:03:08 EST 2010


Am Freitag 26 Februar 2010 21:34:28 schrieb Ketil Malde:
> Daniel Fischer <daniel.is.fischer at web.de> skrev:
> > Am Freitag 26 Februar 2010 16:50:42 schrieb Ketil Malde:
> >>> solutions = [[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]
> >>>
> >>>           | x1 <- [0..9]
> >
> > First digit can't be 0, so make it [1 .. 9].
> > Since you use the fact that the last digit must be the 0, pull all
> > others from [1 .. 9].
>
> Originally, I pulled from alternating odds (x1 <- [1,3..9] etc) and
> evens, since this is fairly easy to deduce...  I reverted this since the
> point was to use brute force.

Yes, but did you forget x10 or did you think that one was too obvious?

>
> >>> solve :: [Int] -> [[Int]]
> >
> > Not on a 32-bit system. Word would suffice there, but you don't know
> > that in advance, so it'd be Int64 or Integer
>
> Hm?  The Ints are just individual digits here.
>

Yup. I didn't realise that you don't call val for the 10-digit number(s). 
If you also did x10 <- [0 .. 9] and checked 
val [x1, x2, ..., x10] `mod` 10 == 0, it would overflow, that's what I was 
thinking of.

> > I would make the length of the prefix a parameter of solve.
>
> I thought about generating a list with solutions for increasing lenghts,
> so that e.g. 'solve [] !! 10' would solve this particular problem.
>

That's nice, but I think it'd be ugly with a DFS, much nicer with a BFS, 
like Rafael did.

> > solve prefix =
> >       case length prefix of
> >         10 -> return prefix
> >         l -> do
> >            x <- [0 .. 9]
> >             ...
> >
> > over the if-then-else.
>
> Yes, much nicer.  Thanks for the feedback!
>
> -k



More information about the Haskell-Cafe mailing list