[Haskell-cafe] Rewrite this imperative in FP way

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Sun Feb 5 10:24:24 CET 2012

Haisheng Wu <freizl at gmail.com> writes:

> a = [1,1,1,1]
> b = [0,1,2,3]
> d = [0,0,0,0]
> for i in b:
>   for j in c:
>     if (i+j)<3:
>       d[i+j] += a[i]
> Do you have any cool solution in FP way?

I find the above sufficiently alien that I can’t work out what
it’s meant to do (what is it actually for?). c is undefined for
one thing. But you might like to see what 

do i <- b; j <-c ; return (i,j)

does and consider what “filter (< 3) . map (uncurry (+))”
does, and possibly look at Data.Array.array, depending on what
the problem you are trying to solve is.

Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk

More information about the Haskell-Cafe mailing list