In Haskell, ++ can spends second list argument onto the end of first list argument. How to substract the second list from the first list? For example, [(5,1),(4,1),(3,1),(2,1),(1,1)] substract [(2,1),(1,1)] I want to get result list: [(5,1),(4,1),(3,1)] Tks. __________________________________ Do you Yahoo!? Make Yahoo! your home page http://www.yahoo.com/r/hs
Bright Sun <bright_sun@yahoo.com> writes:
In Haskell, ++ can spends second list argument onto the end of first list argument. How to substract the second list from the first list?
For example, [(5,1),(4,1),(3,1),(2,1),(1,1)] substract [(2,1),(1,1)] I want to get result list: [(5,1),(4,1),(3,1)]
Look at e.g. http://haskell.org/ghc/docs/latest/html/libraries/base/Data.List.html#v%3A%5... for inspiration Regards, Peter
One answer is the infix operator (\\) defined in the List module (or in Data.List ...) But it's in ont of the "set" operations. For example try : [1..10] \\ [3..6] [1,2,7,8,9,10] But also : ([1..10]++[1..10]) \\ [3..6] [1,2,7,8,9,10,1,2,7,8,9,10] So _all_ the elements with values in [3..6] where removed !! If that's what you want ! Perfect ... if not : what do you want exactly ? List substraction is not uniq. Pierre Bright Sun a écrit :
In Haskell, ++ can spends second list argument onto the end of first list argument. How to substract the second list from the first list?
For example, [(5,1),(4,1),(3,1),(2,1),(1,1)] substract [(2,1),(1,1)] I want to get result list: [(5,1),(4,1),(3,1)]
Tks.
__________________________________ Do you Yahoo!? Make Yahoo! your home page http://www.yahoo.com/r/hs _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
-- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
On Mon, 28 Mar 2005 01:53:39 +0200, Pierre Barbier de Reuille <pierre.barbier@cirad.fr> wrote:
([1..10]++[1..10]) \\ [3..6] [1,2,7,8,9,10,1,2,7,8,9,10]
So _all_ the elements with values in [3..6] where removed !! If that's what you want ! Perfect ... if not : what do you want exactly ? List substraction is not uniq.
This is what I would have expected as well; however, the definition of (\\) in the report uses delete, which only removes the first occurence of each element in the second list from the first. GHC 6.2.2 uses that behavior as well, so: Prelude Data.List> ([1..10] ++ [1..10]) \\ [3..6] [1,2,7,8,9,10,1,2,3,4,5,6,7,8,9,10] .trev -- As far as I can tell It doesn't matter who you are If you can believe there's something worth fighting for
participants (4)
-
Bright Sun -
Peter Eriksen -
Pierre Barbier de Reuille -
Trevion