[Haskell-cafe] Rewrite this imperative in FP way
yves.pares at gmail.com
Sun Feb 5 16:37:40 CET 2012
Concerning your first solution, I don't understand why you redefine Eq but
not Ord instance. Ord will still work by comparing the tuples and not the
first elements of said tuples.
Plus the good news is you don't have to do this: just use regular tuples
and use sort*By *or group*By *functions from Data.List with the 'on'
function from Data.Function.
For instance your Eq instance could have been written
x == y = (==) `on` (fst . getTuple)
With regular tuples you can write "sortBy (compare `on` fst)".
Plus can you rewrite your original imperative algorithm with the right
variable names? You're using a 'd' array that's not been defined.
2012/2/5 Haisheng Wu <freizl at gmail.com>
> 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]
> My just work implementation in Haskell
> Another people implementation in Haskell with Monad and it turns out
> complex and very imperatively.
> Do you have any cool solution in FP way?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe