Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Help with CSV (Keith Sheppard) 2. Re: Identical function and variable names and type inference (Magnus Therning) 3. Re: Identical function and variable names and type inference (Magnus Therning) 4. Multiplexing I/O in Haskell (Sergey V. Mikhanov) 5. Re: Multiplexing I/O in Haskell (Magnus Therning) 6. Haskell data structures: cheap immutable manipulation and nested equality? (Anand Patil) 7. Re: Haskell data structures: cheap immutable manipulation and nested equality? (Heinrich Apfelmus) 8. Generalised data constructor matching (Colin Campbell-McPherson) 9. Re: Generalised data constructor matching (Daniel Fischer) ---------------------------------------------------------------------- Message: 1 Date: Wed, 2 Sep 2009 21:23:05 -0400 From: Keith Sheppard <keiths...@gmail.com> Subject: Re: [Haskell-beginners] Help with CSV To: Hong Yang <hyang...@gmail.com>, beginners@haskell.org Message-ID: <92e42b740909021823x6e32cf56o8138e2b8339da...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Not quite code but... here is an example of parsing CSV http://book.realworldhaskell.org/read/using-parsec.html and here is a library that you can use which is similar http://hackage.haskell.org/package/csv These approaches give you a 2D String list that you can do whatever you want with. if you need to turn a string into a double and you know the string is well formed i think the syntax looks like > let doubleVal = read stringVal :: Double There are better ways to do this if you need to be able to handle formatting errors but I don't know them off the top of my head -Keith On Wed, Sep 2, 2009 at 6:40 PM, Hong Yang<hyang...@gmail.com> wrote: > I need to process csv files that have the characteristics as follows: > 1) each file has thousands of columns which have String, Int, and Double > types > 2) the number of columns may change > 3) for those columns whose name do not change, their location may change > > I want to process some columns in 3) using Haskell. > > In Perl, I can easily have the code like below: > > use Text::CSV; > my $csv = Text::CSV->new( { allow_whitespace => 1 } ); > open my $temp, "<", "temp.csv" or die "Cannot open temp.csv! ($!)"; > my @fields = @{ $csv->getline($temp) }; > $csv->column_names(@fields); > while ( my $hr = $csv->getline_hr($temp) ) { > my $sn = $hr->{"UNIT:unitSerialNumber"}; > # processing goes here ... > } > close $temp; > > Can someone please give me an equivalent code in Haskell? Then I can digest > and expand it. > > Thanks, > > Hong > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- keithsheppard.name ------------------------------ Message: 2 Date: Thu, 3 Sep 2009 08:35:56 +0200 From: Magnus Therning <mag...@therning.org> Subject: Re: [Haskell-beginners] Identical function and variable names and type inference To: Thomas Davie <tom.da...@gmail.com> Cc: beginners <beginners@haskell.org> Message-ID: <e040b520909022335t28c49faaya3190d93776fa...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Wed, Sep 2, 2009 at 11:33 PM, Thomas Davie<tom.da...@gmail.com> wrote: > Ints can't make up the function part of an application, that must have type > (a -> b). Well, I don't think that's strictly true. foo::Int can be argued to be a function that takes no arguments and return an Int. It is however very convenient to call such a function a "constant". However, that such a function actully is constant is far from guaranteed in most programming languages. It happens to be true in Haskell though, due to the strict separation between (proper) functions and "procedures" (functions with side effects, i.e. stuff in the IO monad). > In the mean time, the reason it didn't accept test id = id id is because it > must fix the argument id to only one type. It infers that id must have type > (a -> b), from the fact that it appears in the function position, then sees > it in the argument position, and infers that a = (a -> b) which obviously > causes a problem. Â To resolve that problem, you need rank-2 polymorphism. Is that really correct? I suspect the only thing that causes the OP problems is scoping. If I understand you correctly I wouldn't be able to do the following: Prelude> let f x = x Prelude> :t f f :: t -> t Prelude> :t id id :: a -> a Prelude> :t id f id f :: t -> t Prelude> let g = id f Prelude> :t g g :: t -> t Prelude> id 5 5 Prelude> f 5 5 Prelude> g 5 5 Clearly that's not a problem at all. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org http://therning.org/magnus identi.ca|twitter: magthe ------------------------------ Message: 3 Date: Thu, 3 Sep 2009 08:48:28 +0200 From: Magnus Therning <mag...@therning.org> Subject: Re: [Haskell-beginners] Identical function and variable names and type inference To: Thomas Davie <tom.da...@gmail.com> Cc: beginners <beginners@haskell.org> Message-ID: <e040b520909022348s2fbaf98cy4f5de5adf0f6f...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Thu, Sep 3, 2009 at 8:35 AM, Magnus Therning<mag...@therning.org> wrote: > On Wed, Sep 2, 2009 at 11:33 PM, Thomas Davie<tom.da...@gmail.com> wrote: >> Ints can't make up the function part of an application, that must have type >> (a -> b). > > Well, I don't think that's strictly true. foo::Int can be argued to be > a function that takes no arguments and return an Int. Â It is however > very convenient to call such a function a "constant". Â However, that > such a function actully is constant is far from guaranteed in most > programming languages. Â It happens to be true in Haskell though, due > to the strict separation between (proper) functions and "procedures" > (functions with side effects, i.e. stuff in the IO monad). > >> In the mean time, the reason it didn't accept test id = id id is because it >> must fix the argument id to only one type. It infers that id must have type >> (a -> b), from the fact that it appears in the function position, then sees >> it in the argument position, and infers that a = (a -> b) which obviously >> causes a problem. Â To resolve that problem, you need rank-2 polymorphism. > > Is that really correct? Â I suspect the only thing that causes the OP > problems is scoping. Â If I understand you correctly I wouldn't be able > to do the following: > > Prelude> let f x = x > Prelude> :t f > f :: t -> t > Prelude> :t id > id :: a -> a > Prelude> :t id f > id f :: t -> t > Prelude> let g = id f > Prelude> :t g > g :: t -> t > Prelude> id 5 > 5 > Prelude> f 5 > 5 > Prelude> g 5 > 5 > > Clearly that's not a problem at all. Ah, sorry, I just remembered that the 'forall' is implicit (http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types). /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org http://therning.org/magnus identi.ca|twitter: magthe ------------------------------ Message: 4 Date: Thu, 3 Sep 2009 14:41:46 +0200 From: "Sergey V. Mikhanov" <ser...@mikhanov.com> Subject: [Haskell-beginners] Multiplexing I/O in Haskell To: beginners <beginners@haskell.org> Message-ID: <b38b3dd10909030541k416cff0exb27c1f2474d03...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Hello community, Is there a good/proper way to do multiplexing I/O in Haskell, similar to what we are getting by using select()/poll() in Unix kernel? It does not seem impossible to implement this on top of existing IO library (though I haven't tried this actually, so I may be wrong). What interest me most here is whether we could use Haskell's implicit multithreading here (select()/poll() combination is all about multithreading)? Thanks in advance, Sergey ------------------------------ Message: 5 Date: Thu, 3 Sep 2009 15:52:15 +0200 From: Magnus Therning <mag...@therning.org> Subject: Re: [Haskell-beginners] Multiplexing I/O in Haskell To: "Sergey V. Mikhanov" <ser...@mikhanov.com> Cc: beginners <beginners@haskell.org> Message-ID: <e040b520909030652pbb33d04lf1bdd56b3a72b...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Thu, Sep 3, 2009 at 2:41 PM, Sergey V. Mikhanov<ser...@mikhanov.com> wrote: > Â Hello community, > > Is there a good/proper way to do multiplexing I/O in Haskell, similar > to what we are getting by using select()/poll() in Unix kernel? It > does not seem impossible to implement this on top of existing IO > library (though I haven't tried this actually, so I may be wrong). > What interest me most here is whether we could use Haskell's implicit > multithreading here (select()/poll() combination is all about > multithreading)? There used to be a select()-like call available in a Posix-related module in older versions of GHC. It seems to have disappeared a while ago though. The current "best-practice" seems to be using forkIO "threads". The old-ish paper on the haskell web server[1] contains some numbers that suggest this is indeed a fairly high-performing solution. /M [1]: http://www.haskell.org/haskellwiki/Haskell_Web_Server -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org http://therning.org/magnus identi.ca|twitter: magthe ------------------------------ Message: 6 Date: Fri, 4 Sep 2009 10:38:49 +0100 From: Anand Patil <anand.prabhakar.pa...@gmail.com> Subject: [Haskell-beginners] Haskell data structures: cheap immutable manipulation and nested equality? To: beginners@haskell.org Message-ID: <2bc7a5a50909040238y32325c00ke5cbaae6a448d...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Hi all, I'm slowly shopping around for a functional language. The first one I seriously considered was Clojure, because of its agents; but I'm now realizing that most of my concurrency needs can be met using 'par' and 'seq', so I'm thinking about giving Haskell a closer look. However, Clojure's collections (maps, lists, vectors, sets) have a couple of features that I would really miss: - Cheap 'manipulation'. In the following: user=> (def m {:a 1 :b 2}) #'user/m1 user=> (assoc m :a 3) {:a 3, :b 2} the 'assoc' function produces a new map which actually shares structure with m, meaning it doesn't need to make a full copy in memory; but it also leaves m unchanged. This works efficiently even for nested, mixed collections. - Cheap equality by value: user=> (= m {:a 1 :b 2 :c {:d 3 :f 4}}) false user=> (= m {:a 1 :b 2}) true If I understand correctly, equality is computed based on some kind of hash rather than by comparing the two maps element by element, so it's efficient even for large and/or nested collections. Do Haskell's data structures have analogous properties? If not, are there libraries providing such data structures? Thanks, Anand ------------------------------ Message: 7 Date: Fri, 04 Sep 2009 12:27:59 +0200 From: Heinrich Apfelmus <apfel...@quantentunnel.de> Subject: [Haskell-beginners] Re: Haskell data structures: cheap immutable manipulation and nested equality? To: beginners@haskell.org Message-ID: <h7qq3f$17...@ger.gmane.org> Content-Type: text/plain; charset=ISO-8859-1 Anand Patil wrote: > - Cheap 'manipulation'. In the following: > > user=> (def m {:a 1 :b 2}) > #'user/m1 > user=> (assoc m :a 3) > {:a 3, :b 2} > > the 'assoc' function produces a new map which actually shares > structure with m, meaning it doesn't need to make a full copy in > memory; but it also leaves m unchanged. This works efficiently even > for nested, mixed collections. Since Haskell is pure, this is the default. Every data structure is persistent and shares as much as possible with previous versions. Example in point: an infinite list of ones ones = 1:ones > - Cheap equality by value: > > user=> (= m {:a 1 :b 2 :c {:d 3 :f 4}}) > false > user=> (= m {:a 1 :b 2}) > true > > If I understand correctly, equality is computed based on some kind of > hash rather than by comparing the two maps element by element, so it's > efficient even for large and/or nested collections. > > Do Haskell's data structures have analogous properties? If not, are > there libraries providing such data structures? Not present in Haskell, no existing library either. You can roll your own, of course. (Also note that hash based comparison might yield false positives). Why would you need this feature? I can only think of common subexpression elimination. Generally, the above two points are minor compared to the other things that Haskell has to offer, like pattern matching, Hindler Milner type system, purity. Regards, apfelmus -- http://apfelmus.nfshost.com ------------------------------ Message: 8 Date: Fri, 4 Sep 2009 20:01:47 +0800 From: Colin Campbell-McPherson <cora.nightsh...@gmail.com> Subject: [Haskell-beginners] Generalised data constructor matching To: beginners@haskell.org Message-ID: <7b251974-f3b6-485c-b2d5-5ec525878...@logaan.net> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Hi Chaps, I've been trying to write a function that would get the name of most data that is an instance of class "Animal". This getName function could then be overwritten for data that doesn't follow the normal pattern for "Animal" data types. So the getName defined in the Animal class would be a generic, or default implementation. I've written a couple of examples that don't use classes, but that I hope expresses what I'm trying to accomplish. In the first example getName needs to be defined for Dogs and Birds, even though they're essentially identical. The tries to define a more general function that works for both Birds and Dogs, and anything else that might come along. The first examples works, the second gives a "Parse error in pattern" error. EXAMPLE 1 data Animal = Person String String | Dog String | Bird String deriving Show getName :: Animal -> String getName (Person firstName lastName) = firstName ++ " " ++ lastName getName (Dog name) = name getName (Bird name) = name logan = Person "Logan" "Campbell" ebony = Dog "Ebony" poly = Bird "Poly" main = do putStrLn $ show $ getName logan putStrLn $ show $ getName ebony putStrLn $ show $ getName poly EXAMPLE 2 data Animal = Person String String | Dog String | Bird String deriving Show getName :: Animal -> String getName (Person firstName lastName) = firstName ++ " " ++ lastName getName (_ name) = name logan = Person "Logan" "Campbell" ebony = Dog "Ebony" poly = Bird "Poly" main = do putStrLn $ show $ getName logan putStrLn $ show $ getName ebony putStrLn $ show $ getName poly I have a background with Ruby and Erlang, so if drawing from concepts in either of those languages would help explain please do so. I'm also quite new to haskell so if i've used the wrong terms, or I'm trying to apply concepts where they don't belong, sorry about that. TLDR: How can I write a generic function that will work on most instances of a class, but be over over-written for instances of that class that deviate from the normal structure (ie. a data constructor with two params, rather than one). Many thanks, Colin ------------------------------ Message: 9 Date: Fri, 4 Sep 2009 14:57:18 +0200 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] Generalised data constructor matching To: beginners@haskell.org Message-ID: <200909041457.18565.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="iso-8859-1" Am Freitag 04 September 2009 14:01:47 schrieb Colin Campbell-McPherson: > Hi Chaps, > > I've been trying to write a function that would get the name of most > data that is an instance of class "Animal". This getName function > could then be overwritten for data that doesn't follow the normal > pattern for "Animal" data types. So the getName defined in the Animal > class would be a generic, or default implementation. > > I've written a couple of examples that don't use classes, but that I > hope expresses what I'm trying to accomplish. In the first example > getName needs to be defined for Dogs and Birds, even though they're > essentially identical. The tries to define a more general function > that works for both Birds and Dogs, and anything else that might come > along. > > The first examples works, the second gives a "Parse error in pattern" > error. > > EXAMPLE 1 > > data Animal = Person String String | Dog String | Bird String deriving > Show > getName :: Animal -> String > getName (Person firstName lastName) = firstName ++ " " ++ lastName > getName (Dog name) = name > getName (Bird name) = name > > logan = Person "Logan" "Campbell" > ebony = Dog "Ebony" > poly = Bird "Poly" > > main = do > putStrLn $ show $ getName logan > putStrLn $ show $ getName ebony > putStrLn $ show $ getName poly > For such a datatype, named fields come in handy: data Animal = Person { firstName, lastName :: String } | Dog { name :: String } | Bird { name :: String } getName :: Animal -> String getName (Person f l) = f ++ " " ++ l -- other special cases if the datatype is extended getName other = name other > > EXAMPLE 2 > > data Animal = Person String String | Dog String | Bird String deriving > Show > getName :: Animal -> String > getName (Person firstName lastName) = firstName ++ " " ++ lastName > getName (_ name) = name > > logan = Person "Logan" "Campbell" > ebony = Dog "Ebony" > poly = Bird "Poly" > > main = do > putStrLn $ show $ getName logan > putStrLn $ show $ getName ebony > putStrLn $ show $ getName poly > > I have a background with Ruby and Erlang, so if drawing from concepts > in either of those languages would help explain please do so. I'm also > quite new to haskell so if i've used the wrong terms, or I'm trying to > apply concepts where they don't belong, sorry about that. > > > TLDR: How can I write a generic function that will work on most > instances of a class, but be over over-written for instances of that > class that deviate from the normal structure (ie. a data constructor > with two params, rather than one). In general: not. The problem is that potentially every datatype can be made an instance of the class, so in the default implementations, you can only[*] use functions which work on every datatype. There aren't many interesting functions that do. [*]well, you can also use methods of the class and superclasses, so e.g. class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) class (Eq a) => Ord a where compare :: a -> a -> Ordering (<), (<=), (>), (>=) :: a -> a -> Bool min, max :: a -> a -> a x < y = x <= y && x /= y x <= y = x < y || x == y -- etc. is possible. But you still have to write instances manually for every type (except instances you can let the compiler derive). > > Many thanks, > Colin ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 15, Issue 3 ****************************************