[Haskell-cafe] Rewrite this imperative in FP way

Matthew Farkas-Dyck strake888 at gmail.com
Sun Feb 5 18:57:41 CET 2012


On Sun, Feb 5, 2012 at 2:28 PM, Haisheng Wu <freizl at gmail.com> wrote:
> 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?

Not sure whether this is cool, but here it is nonetheless:

a = repeat 1;
b = [0..3];
c = [0,2];
d = map (sum ∘ map ((a !!) ∘ fromIntegral) ∘ ($ (filter (<3) ∘ map sum
∘ sequence) [b,c]) ∘ filter ∘ (≡)) [1..];



More information about the Haskell-Cafe mailing list