Re: [Haskell-cafe] adding the elements of two lists

2012-03-29 Thread David Fox
On Sun, Mar 25, 2012 at 5:06 AM, Michael Snoyman mich...@snoyman.com wrote: On Sun, Mar 25, 2012 at 2:01 PM, TP paratribulati...@free.fr wrote: Hello, My primary problem may be reduced to adding elements of two lists: [1,2,3] + [4,5,6] = [5,7,9] My first idea was to declare a list of Int as

Re: [Haskell-cafe] adding the elements of two lists

2012-03-29 Thread Ketil Malde
Richard O'Keefe o...@cs.otago.ac.nz writes: newtype PS a = PS [a] deriving (Eq, Show) u f (PS x)= PS $ map f x b f (PS x) (PS y) = PS $ zipWith f x y to_ps x = PS (x : repeat 0) BTW, isn't this a good candidate for an Applicative instance (similar to ZipList)? u f p

Re: [Haskell-cafe] adding the elements of two lists

2012-03-29 Thread Doug McIlroy
From: Richard O'Keefe o...@cs.otago.ac.nz Date: Thu, 29 Mar 2012 16:34:46 +1300 On 29/03/2012, at 3:08 PM, Doug McIlroy wrote: - without newtype toSeries f = f : repeat 0 -- coerce scalar to series instance Num a = Num [a] where (f:fs) + (g:gs) = f+g : fs+gs

Re: [Haskell-cafe] adding the elements of two lists

2012-03-29 Thread Ozgur Akgun
On 29 March 2012 04:34, Richard O'Keefe o...@cs.otago.ac.nz wrote: u f (PS x)= PS $ map f x b f (PS x) (PS y) = PS $ zipWith f x y to_ps x = PS (x : repeat 0) Also see: http://hackage.haskell.org/package/newtype -- Ozgur Akgun

Re: [Haskell-cafe] adding the elements of two lists

2012-03-28 Thread Doug McIlroy
Date: Tue, 27 Mar 2012 11:03:54 +1300 From: Richard O'Keefe o...@cs.otago.ac.nz Subject: Re: [Haskell-cafe] adding the elements of two lists To: jerzy.karczmarc...@unicaen.fr Cc: haskell-cafe@haskell.org And *that* is why I stopped trying to define instance Num t = Num [t]. If I KNEW

Re: [Haskell-cafe] adding the elements of two lists

2012-03-28 Thread Richard O'Keefe
On 29/03/2012, at 3:08 PM, Doug McIlroy wrote: - without newtype toSeries f = f : repeat 0 -- coerce scalar to series instance Num a = Num [a] where (f:fs) + (g:gs) = f+g : fs+gs (f:fs') * gs@(g:gs') = f*g : fs'*gs + (toSeries f)*gs' - with newtype newtype Num

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Jerzy Karczmarczuk
Le 26/03/2012 02:41, Chris Smith a écrit : Of course there are rings for which it's possible to represent the elements as lists. Nevertheless, there is definitely not one that defines (+) = zipWith (+), as did the one I was responding to. What? The additive structure does not define a ring.

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Jake McArthur
This is interesting because it seems to be a counterexample to the claim that you can lift any Num through an Applicative (ZipList, in this case). It seems like maybe that only works in general for monoids instead of rings? On Mar 25, 2012 8:43 PM, Chris Smith cdsm...@gmail.com wrote: Jerzy

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Chris Smith
Jerzy Karczmarczuk jerzy.karczmarc...@unicaen.fr wrote: Le 26/03/2012 02:41, Chris Smith a écrit : Of course there are rings for which it's possible to represent the elements as lists.  Nevertheless, there is definitely not one that defines (+) = zipWith (+), as did the one I was responding

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Jerzy Karczmarczuk
Le 26/03/2012 16:31, Chris Smith a écrit : If you were asking about why there is no ring on [a] that defines (+) = zipWith (+), then here's why. By that definition, you have [1,2,3] + [4,5] = [5,7]. But also [1,2,42] + [4,5] = [5,7]. Addition by [4,5] is not one-to-one, so [4,5] cannot be

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Chris Smith
On Mon, Mar 26, 2012 at 10:18 AM, Jerzy Karczmarczuk jerzy.karczmarc...@unicaen.fr wrote: So, * the addition* is not invertible, why did you introduce rings ... My intent was to point out that the Num instance that someone suggested for Num a = Num [a] was a bad idea. I talked about rings

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread wren ng thornton
On 3/26/12 8:16 AM, Jake McArthur wrote: This is interesting because it seems to be a counterexample to the claim that you can lift any Num through an Applicative (ZipList, in this case). It seems like maybe that only works in general for monoids instead of rings? I'm not so sure about that.

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Richard O'Keefe
On 27/03/2012, at 5:18 AM, Jerzy Karczmarczuk wrote: But I don't care about using (+) = zipWith (+) anywhere, outside of a programming model / framework, where you keep the sanity of your data. In my programs I KNEW that the length of the list is either fixed, or of some minimal size (or

Re: [Haskell-cafe] adding the elements of two lists

2012-03-26 Thread Jake McArthur
Well, ZipList's pure is indeed repeat, but there is nothing about ZipList restricting it to infinite lists. As long as pure is repeat, I'm pretty sure any other value can still be finite without violating Applicative's laws. On Mar 26, 2012 1:02 PM, wren ng thornton w...@freegeek.org wrote: On

[Haskell-cafe] adding the elements of two lists

2012-03-25 Thread TP
Hello, My primary problem may be reduced to adding elements of two lists: [1,2,3] + [4,5,6] = [5,7,9] My first idea was to declare a list of Int as an instance of Num, and define (+) in the correct way. However, it seems it is not possible to do that: --- instance Num [Int]

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Michael Snoyman
On Sun, Mar 25, 2012 at 2:01 PM, TP paratribulati...@free.fr wrote: Hello, My primary problem may be reduced to adding elements of two lists: [1,2,3] + [4,5,6] = [5,7,9] My first idea was to declare a list of Int as an instance of Num, and define (+) in the correct way. However, it seems

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Jonathan Grochowski
On Sun, Mar 25, 2012 at 5:01 AM, TP paratribulati...@free.fr wrote: Hello, My primary problem may be reduced to adding elements of two lists: [1,2,3] + [4,5,6] = [5,7,9] My first idea was to declare a list of Int as an instance of Num, and define (+) in the correct way. However, it seems

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Jerzy Karczmarczuk
TP : However, it seems it is not possible to do that: --- instance Num [Int] where l1 + l2 = --- Why? Why not?? It is possible. All what has been said by other people is right. But you can do it your way as well, GHC won't protest if you :set

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Richard O'Keefe
On 26/03/2012, at 1:01 AM, TP wrote: Hello, My primary problem may be reduced to adding elements of two lists: [1,2,3] + [4,5,6] = [5,7,9] zipWith (+) [1,2,3] [4,5,6] gets the job done. However, it seems it is not possible to do that: --- instance Num [Int] where

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Chris Smith
Jonathan Grochowski jongrocho...@gmail.com wrote: As Michael suggests using zipWith (+) is the simplest solution. If you really want to be able to write [1,2,3] + [4,5,6], you can define the instnace as instance (Num a) = Num [a] where xs + ys = zipWith (+) xs ys You can do this

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Jerzy Karczmarczuk
Le 26/03/2012 01:51, Chris Smith a écrit : instance (Num a) = Num [a] where xs + ys = zipWith (+) xs ys You can do this in the sense that it's legal Haskell... but it is a bad idea to make lists an instance of Num, because there are situations where the result doesn't act as

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Chris Smith
Jerzy Karczmarczuk jerzy.karczmarc...@unicaen.fr wrote: Le 26/03/2012 01:51, Chris Smith a écrit :     instance (Num a) = Num [a] where     xs + ys = zipWith (+) xs ys You can do this in the sense that it's legal Haskell... but it is a bad idea [...] It MIGHT be a ring or not. The

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread wren ng thornton
On 3/25/12 8:06 AM, Michael Snoyman wrote: A simple solution is to use the zipWith[1] function: zipWith (+) [1,2,3] [4,5,6] == [5,7,9] It takes a bit of time to get acquainted with all of the incredibly convenient functions in base, but once you know them, it can greatly simplify your

Re: [Haskell-cafe] adding the elements of two lists

2012-03-25 Thread Richard O'Keefe
On 26/03/2012, at 12:51 PM, Chris Smith wrote: More concretely, it's not hard to see that the additive identity is [0,0,0...], the infinite list of zeros. But if you have a finite list x, then x - x is NOT equal to that additive identity! Said another way: if you do want [num] to support