Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-06 Thread Haisheng Wu
The reason I redefined Eq is: When doing `union` over two list of MyTuple is just base on its first element. Basically it means: [(1,2), (2,2)] `union` [(1,0), (2,0), (0,0)] produce [(1,2), (2,2), (0,0)] rather than [(1,2),(2,2),(1,0),(2,0),(0,0)] by default. -Haisheng On Sun, Feb 5, 2012

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-06 Thread Haisheng Wu
*d = [sum $ map (a !!) [i | i - b, j - c, i + j 3, i + j == dIndex] | dIndex - [0..3]] * This is cool. -Simon On Sun, Feb 5, 2012 at 5:07 PM, L Corbijn aspergesoe...@gmail.com wrote: On Sun, Feb 5, 2012 at 7:28 AM, Haisheng Wu fre...@gmail.com wrote: a = [1,1,1,1] b = [0,1,2,3] d =

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-06 Thread Yves Parès
Okay. But that's misleading, as normally x == y = True *iff* compare x y = EQ, but there this is not verified, as you don't redefined A function like unionBy doesn't exist, so IMHO to limit ambiguity, it would be a good idea to use MyTuple *only *at the specific place where you need its Eq

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-06 Thread Thomas Hallgren
How about this: import Array a = [1,1,1,1] b = [0,1,2,3] c = [0,2] d = elems $ accumArray (+) 0 (0,3) [(i+j,a!!i) | i-b, j-c, i+j3] -- Thomas H On 2012-02-06 12:01 , Haisheng Wu wrote: *d = [sum $ map (a !!) [i | i - b, j - c, i + j 3, i + j == dIndex] | dIndex - [0..3]] *

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Roman Cheplyaka
* Haisheng Wu fre...@gmail.com [2012-02-05 14:28:10+0800] 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? You can use IntMap as a replacement for arrays: (I didn't understand your

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Jon Fairbairn
Haisheng Wu fre...@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

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Haisheng Wu
Sorry there is a mistake in the problem description. Here it is in Python: a = [1,1,1,1] b = [0,1,2,3] c = [0,2] d = [0,0,0,0] for i in b: for j in c: if (i+j)3: d[i+j] += a[i] -Haisheng On Sun, Feb 5, 2012 at 2:28 PM, Haisheng Wu fre...@gmail.com wrote: a =

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Yves Parès
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

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Yves Parès
For instance your Eq instance could have been written x == y = (==) `on` (fst . getTuple) Sorry, wrong arity: (==) = (==) `on` (fst . getTuple) Okay for the imperative code. 2012/2/5 Yves Parès yves.pa...@gmail.com Concerning your first solution, I don't understand why you redefine Eq but

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Morel Pisum
+= a[i] is the same as +=1, isn't it? (i accidentally didn't reply to the list on my first try. sorry.) Am 05.02.2012 16:36, schrieb Haisheng Wu: Sorry there is a mistake in the problem description. Here it is in Python: a = [1,1,1,1] b = [0,1,2,3] c = [0,2] d = [0,0,0,0] for i in b:

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Mike Burns
Can you write it as a Python function? Another way of asking: is the goal to mutate d or is it to produce the list? On 2012-02-05 23.36.28 +0800, Haisheng Wu wrote: Sorry there is a mistake in the problem description. Here it is in Python: a = [1,1,1,1] b = [0,1,2,3] c = [0,2] d =

Re: [Haskell-cafe] Rewrite this imperative in FP way

2012-02-05 Thread Matthew Farkas-Dyck
On Sun, Feb 5, 2012 at 2:28 PM, Haisheng Wu fre...@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

[Haskell-cafe] Rewrite this imperative in FP way

2012-02-04 Thread Haisheng Wu
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 http://hpaste.org/57452 Another people implementation in Haskell with Monad and it turns out complex and very imperatively. http://hpaste.org/57358 Do