From byorgey at seas.upenn.edu Thu May 1 00:40:07 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed, 30 Apr 2014 20:40:07 -0400 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse In-Reply-To: <20140430221806.137400@gmx.com> References: <20140430221806.137400@gmx.com> Message-ID: <20140501004007.GA26920@seas.upenn.edu> There's no particular need for SourceGraph and definitely no need for cabal-dev. I say try (3). On Thu, May 01, 2014 at 12:18:05AM +0200, Robert Weisser wrote: > I decided to try out Eclipse with the EclipseFP plug-in for Haskell. > I downloaded Eclipse, and then started to install EclipseFP. At > some point, I got a screen which said that I needed to install > buildwrapper and scion-browser. It offered to install both > of them, and optionally install hoogle, hlint, stylish-haskell, > SourceGraph, and cabal-dev. The option to install the additional > items was pre-checked. I left it checked. The installation of > buildwrapper, etc. took a long time. It seemed to be going well > until I saw the messages below, which say that installing SourceGraph > will likely break several other packages, most of which were part > of the EclipseFP installation. > > I am not a complete novice with Haskell but I am a complete novice > with Cabal. I'm not sure what to do. Here are some options I have > considered: > > 1. Use --force-reinstalls and hope for the best. > > 2. Try to resolve the problem with Cabal, although I don't know to > do this. > > 3. Use EclipseFP without SourceGraph and cabal-dev. > > 4. Use EclipseFP without SourceGraph, but install cabal-dev using > cabal on the command line. > > 5. Forget about Eclipse altogether, and go back to using vim. > > Any advice would be appreciated. > > By the way, I am using Haskell Platform 7.6.3. > > Here are the messages I saw in the Eclipse console window: > > Installing executable SourceGraph > > Resolving dependencies... > In order, the following would be installed: > asn1-types-0.2.3 (new package) > asn1-encoding-0.8.1.3 (new package) > asn1-parse-0.8.1 (new package) > bktrees-0.3.1 (new package) > byteable-0.1.1 (new package) > cereal-0.4.0.1 (new package) > colour-2.3.3 (new package) > cpphs-1.18.2 (new version) > crypto-pubkey-types-0.4.2.2 (new package) > cryptohash-0.11.4 (new package) > digest-0.0.1.2 (new package) > dlist-0.5 (new version) > aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 > data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> 0.5 > data-default-0.5.3 (reinstall) > cookie-0.4.1.1 (new package) > haskell-src-exts-1.13.5 (new version) > hslua-0.3.12 (new package) > mime-types-0.1.0.4 (new package) > multiset-0.2.2 (new package) > pandoc-types-1.12.3.2 (new package) > pem-0.2.2 (new package) > polyparse-1.8 (new version) > publicsuffixlist-0.1 (new package) > http-client-0.3.2 (new package) > regex-pcre-builtin-0.94.4.8.8.34 (new package) > highlighting-kate-0.5.6.1 (new package) > resourcet-0.4.10.2 (new version) > securemem-0.1.3 (new package) > crypto-cipher-types-0.0.9 (new package) > cipher-aes-0.2.7 (new package) > cipher-rc4-0.1.4 (new package) > crypto-random-0.0.7 (new package) > cprng-aes-0.5.2 (new package) > crypto-numbers-0.2.3 (new package) > crypto-pubkey-0.2.4 (new package) > socks-0.5.4 (new package) > temporary-1.1.2.5 (new package) > text-stream-decode-0.1.0.5 (new package) > conduit-1.0.17.1 (new version) > http-client-conduit-0.2.0.1 (new package) > wl-pprint-text-1.1.0.2 (new package) > graphviz-2999.16.0.0 (new package) > x509-1.4.11 (new package) > x509-store-1.4.4 (new package) > x509-system-1.4.5 (new package) > x509-validation-1.5.0 (new package) > tls-1.2.6 (new package) > connection-0.2.1 (new package) > http-client-tls-0.2.1.1 (new package) > http-conduit-2.0.0.10 (new package) > xml-1.3.13 (new package) > texmath-0.6.6.1 (new package) > yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, resourcet-1.1.2 > -> 0.4.10.2 > zip-archive-0.2.2.1 (new package) > pandoc-1.12.3.3 (new package) > Graphalyze-0.14.0.2 (new package) > SourceGraph-0.7.0.5 (new package) > cabal: The following packages are likely to be broken by the reinstalls: > stylish-haskell-0.5.10.0 > scion-browser-0.3.1 > persistent-template-1.3.1.3 > persistent-sqlite-1.3.0.5 > persistent-1.3.0.6 > buildwrapper-0.8.0 > dynamic-cabal-0.3.1 > Use --force-reinstalls if you want to install anyway. > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From freitasraffa at gmail.com Thu May 1 02:42:09 2014 From: freitasraffa at gmail.com (raffa f) Date: Wed, 30 Apr 2014 23:42:09 -0300 Subject: [Haskell-beginners] replacing fold with scan! Message-ID: hi everyone! here's my new problem. i wrote my version of filter: filter' :: (a -> Bool) -> [a] -> [a] filter' f = foldr (\x acc -> if f x then x:acc else acc) [] and it works! however, i wanted to use scan too. so i just replaced foldr with scanr, to see what would happen: filter'' :: (a -> Bool) -> [a] -> [a] filter'' f = scanr (\x acc -> if f x then x:acc else acc) [] but that doesn't work! ghci gives me this: folds.hs:15:59: Couldn't match expected type `a' with actual type `[a0]' `a' is a rigid type variable bound by the type signature for filter'' :: (a -> Bool) -> [a] -> [a] at folds.hs:14:13 In the second argument of `scanr', namely `[]' In the expression: scanr (\ x acc -> if f x then x : acc else acc) [] In an equation for filter'': filter'' f = scanr (\ x acc -> if f x then x : acc else acc) [] Failed, modules loaded: none. the problem seems to be with the start value of [], it seems? i don't understand, i thought scan and fold worked pretty much the same. i learned about these functions today, so i'm still trying to wrap my head around them... thank you! -------------- next part -------------- An HTML attachment was scrubbed... URL: From Robert.Weisser at gmx.com Thu May 1 03:17:57 2014 From: Robert.Weisser at gmx.com (Robert Weisser) Date: Thu, 01 May 2014 05:17:57 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse Message-ID: <20140501031757.6400@gmx.com> Thanks. That's great info. ----- Original Message ----- From: Norbert Melzer Sent: 04/30/14 07:19 PM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse Omit cabal-dev it is not maintained anymore and most of its functionality is part of cabal already (cabal sandbox) Am 01.05.2014 00:18 schrieb "Robert Weisser" < Robert.Weisser at gmx.com >: I decided to try out Eclipse with the EclipseFP plug-in for Haskell. I downloaded Eclipse, and then started to install EclipseFP. At some point, I got a screen which said that I needed to install buildwrapper and scion-browser. It offered to install both of them, and optionally install hoogle, hlint, stylish-haskell, SourceGraph, and cabal-dev. The option to install the additional items was pre-checked. I left it checked. The installation of buildwrapper, etc. took a long time. It seemed to be going well until I saw the messages below, which say that installing SourceGraph will likely break several other packages, most of which were part of the EclipseFP installation. I am not a complete novice with Haskell but I am a complete novice with Cabal. I'm not sure what to do. Here are some options I have considered: 1. Use --force-reinstalls and hope for the best. 2. Try to resolve the problem with Cabal, although I don't know to do this. 3. Use EclipseFP without SourceGraph and cabal-dev. 4. Use EclipseFP without SourceGraph, but install cabal-dev using cabal on the command line. 5. Forget about Eclipse altogether, and go back to using vim. Any advice would be appreciated. By the way, I am using Haskell Platform 7.6.3. Here are the messages I saw in the Eclipse console window: Installing executable SourceGraph Resolving dependencies... In order, the following would be installed: asn1-types-0.2.3 (new package) asn1-encoding-0.8.1.3 (new package) asn1-parse-0.8.1 (new package) bktrees-0.3.1 (new package) byteable-0.1.1 (new package) cereal-0.4.0.1 (new package) colour-2.3.3 (new package) cpphs-1.18.2 (new version) crypto-pubkey-types-0.4.2.2 (new package) cryptohash-0.11.4 (new package) digest-0.0.1.2 (new package) dlist-0.5 (new version) aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-0.5.3 (reinstall) cookie-0.4.1.1 (new package) haskell-src-exts-1.13.5 (new version) hslua-0.3.12 (new package) mime-types-0.1.0.4 (new package) multiset-0.2.2 (new package) pandoc-types-1.12.3.2 (new package) pem-0.2.2 (new package) polyparse-1.8 (new version) publicsuffixlist-0.1 (new package) http-client-0.3.2 (new package) regex-pcre-builtin-0.94.4.8.8.34 tel:0.94.4.8.8.34 (new package) highlighting-kate-0.5.6.1 (new package) resourcet-0.4.10.2 (new version) securemem-0.1.3 (new package) crypto-cipher-types-0.0.9 (new package) cipher-aes-0.2.7 (new package) cipher-rc4-0.1.4 (new package) crypto-random-0.0.7 (new package) cprng-aes-0.5.2 (new package) crypto-numbers-0.2.3 (new package) crypto-pubkey-0.2.4 (new package) socks-0.5.4 (new package) temporary-1.1.2.5 (new package) text-stream-decode-0.1.0.5 (new package) conduit-1.0.17.1 (new version) http-client-conduit-0.2.0.1 (new package) wl-pprint-text-1.1.0.2 (new package) graphviz-2999.16.0.0 (new package) x509-1.4.11 (new package) x509-store-1.4.4 (new package) x509-system-1.4.5 (new package) x509-validation-1.5.0 (new package) tls-1.2.6 (new package) connection-0.2.1 (new package) http-client-tls-0.2.1.1 (new package) http-conduit-2.0.0.10 (new package) xml-1.3.13 (new package) texmath-0.6.6.1 (new package) yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, resourcet-1.1.2 -> 0.4.10.2 zip-archive-0.2.2.1 (new package) pandoc-1.12.3.3 (new package) Graphalyze-0.14.0.2 (new package) SourceGraph-0.7.0.5 (new package) cabal: The following packages are likely to be broken by the reinstalls: stylish-haskell-0.5.10.0 scion-browser-0.3.1 persistent-template-1.3.1.3 persistent-sqlite-1.3.0.5 persistent-1.3.0.6 buildwrapper-0.8.0 dynamic-cabal-0.3.1 Use --force-reinstalls if you want to install anyway. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From Robert.Weisser at gmx.com Thu May 1 03:19:39 2014 From: Robert.Weisser at gmx.com (Robert Weisser) Date: Thu, 01 May 2014 05:19:39 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse Message-ID: <20140501031940.6390@gmx.com> Brent: Thanks to you as well, especially since (3) involves the least effort. ----- Original Message ----- From: Brent Yorgey Sent: 04/30/14 08:40 PM To: beginners at haskell.org Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse There's no particular need for SourceGraph and definitely no need for cabal-dev. I say try (3). On Thu, May 01, 2014 at 12:18:05AM +0200, Robert Weisser wrote: > I decided to try out Eclipse with the EclipseFP plug-in for Haskell. > I downloaded Eclipse, and then started to install EclipseFP. At > some point, I got a screen which said that I needed to install > buildwrapper and scion-browser. It offered to install both > of them, and optionally install hoogle, hlint, stylish-haskell, > SourceGraph, and cabal-dev. The option to install the additional > items was pre-checked. I left it checked. The installation of > buildwrapper, etc. took a long time. It seemed to be going well > until I saw the messages below, which say that installing SourceGraph > will likely break several other packages, most of which were part > of the EclipseFP installation. > > I am not a complete novice with Haskell but I am a complete novice > with Cabal. I'm not sure what to do. Here are some options I have > considered: > > 1. Use --force-reinstalls and hope for the best. > > 2. Try to resolve the problem with Cabal, although I don't know to > do this. > > 3. Use EclipseFP without SourceGraph and cabal-dev. > > 4. Use EclipseFP without SourceGraph, but install cabal-dev using > cabal on the command line. > > 5. Forget about Eclipse altogether, and go back to using vim. > > Any advice would be appreciated. > > By the way, I am using Haskell Platform 7.6.3. > > Here are the messages I saw in the Eclipse console window: > > Installing executable SourceGraph > > Resolving dependencies... > In order, the following would be installed: > asn1-types-0.2.3 (new package) > asn1-encoding-0.8.1.3 (new package) > asn1-parse-0.8.1 (new package) > bktrees-0.3.1 (new package) > byteable-0.1.1 (new package) > cereal-0.4.0.1 (new package) > colour-2.3.3 (new package) > cpphs-1.18.2 (new version) > crypto-pubkey-types-0.4.2.2 (new package) > cryptohash-0.11.4 (new package) > digest-0.0.1.2 (new package) > dlist-0.5 (new version) > aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 > data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> 0.5 > data-default-0.5.3 (reinstall) > cookie-0.4.1.1 (new package) > haskell-src-exts-1.13.5 (new version) > hslua-0.3.12 (new package) > mime-types-0.1.0.4 (new package) > multiset-0.2.2 (new package) > pandoc-types-1.12.3.2 (new package) > pem-0.2.2 (new package) > polyparse-1.8 (new version) > publicsuffixlist-0.1 (new package) > http-client-0.3.2 (new package) > regex-pcre-builtin-0.94.4.8.8.34 (new package) > highlighting-kate-0.5.6.1 (new package) > resourcet-0.4.10.2 (new version) > securemem-0.1.3 (new package) > crypto-cipher-types-0.0.9 (new package) > cipher-aes-0.2.7 (new package) > cipher-rc4-0.1.4 (new package) > crypto-random-0.0.7 (new package) > cprng-aes-0.5.2 (new package) > crypto-numbers-0.2.3 (new package) > crypto-pubkey-0.2.4 (new package) > socks-0.5.4 (new package) > temporary-1.1.2.5 (new package) > text-stream-decode-0.1.0.5 (new package) > conduit-1.0.17.1 (new version) > http-client-conduit-0.2.0.1 (new package) > wl-pprint-text-1.1.0.2 (new package) > graphviz-2999.16.0.0 (new package) > x509-1.4.11 (new package) > x509-store-1.4.4 (new package) > x509-system-1.4.5 (new package) > x509-validation-1.5.0 (new package) > tls-1.2.6 (new package) > connection-0.2.1 (new package) > http-client-tls-0.2.1.1 (new package) > http-conduit-2.0.0.10 (new package) > xml-1.3.13 (new package) > texmath-0.6.6.1 (new package) > yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, resourcet-1.1.2 > -> 0.4.10.2 > zip-archive-0.2.2.1 (new package) > pandoc-1.12.3.3 (new package) > Graphalyze-0.14.0.2 (new package) > SourceGraph-0.7.0.5 (new package) > cabal: The following packages are likely to be broken by the reinstalls: > stylish-haskell-0.5.10.0 > scion-browser-0.3.1 > persistent-template-1.3.1.3 > persistent-sqlite-1.3.0.5 > persistent-1.3.0.6 > buildwrapper-0.8.0 > dynamic-cabal-0.3.1 > Use --force-reinstalls if you want to install anyway. > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngzhian at gmail.com Thu May 1 04:31:38 2014 From: ngzhian at gmail.com (Zhi An Ng) Date: Thu, 1 May 2014 12:31:38 +0800 Subject: [Haskell-beginners] replacing fold with scan! Message-ID: ?? Hi, The types of `foldr` and `scanr` are different: > :t scanr scanr :: (a -> b -> b) -> b -> [a] -> [b] > :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b I believe the reason is that `scanr` does something like a prefix sum, https://en.wikipedia.org/wiki/Prefix_sum So to get this to work, you need to change the type signature of filter'' filter'' :: (a -> Bool) -> [a] -> [[a]] That's all. Anyway you can try entering the definition of filter'' into ghci, and use :t to let ghc figure our the type for you as well :) Best, Zhi An On Thu, May 1, 2014 at 10:42 AM, raffa f wrote: > hi everyone! here's my new problem. i wrote my version of filter: > > filter' :: (a -> Bool) -> [a] -> [a] > filter' f = foldr (\x acc -> if f x then x:acc else acc) [] > > and it works! however, i wanted to use scan too. so i just replaced foldr > with scanr, to see what would happen: > > filter'' :: (a -> Bool) -> [a] -> [a] > filter'' f = scanr (\x acc -> if f x then x:acc else acc) [] > > but that doesn't work! ghci gives me this: > > folds.hs:15:59: > Couldn't match expected type `a' with actual type `[a0]' > `a' is a rigid type variable bound by > the type signature for filter'' :: (a -> Bool) -> [a] -> [a] > at folds.hs:14:13 > In the second argument of `scanr', namely `[]' > In the expression: > scanr (\ x acc -> if f x then x : acc else acc) [] > In an equation for filter'': > filter'' f = scanr (\ x acc -> if f x then x : acc else acc) [] > Failed, modules loaded: none. > > the problem seems to be with the start value of [], it seems? i don't > understand, i thought scan and fold worked pretty much the same. i learned > about these functions today, so i'm still trying to wrap my head around > them... > > thank you! > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlatko.basic at gmail.com Thu May 1 07:59:36 2014 From: vlatko.basic at gmail.com (Vlatko Basic) Date: Thu, 01 May 2014 09:59:36 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse In-Reply-To: <20140501031757.6400@gmx.com> References: <20140501031757.6400@gmx.com> Message-ID: <5361FEE8.4020601@gmail.com> An HTML attachment was scrubbed... URL: From Robert.Weisser at gmx.com Thu May 1 14:44:15 2014 From: Robert.Weisser at gmx.com (Robert Weisser) Date: Thu, 01 May 2014 16:44:15 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse Message-ID: <20140501144416.172110@gmx.com> Thanks, Vlatko. I'm not sure I understand everything you said, but I'll give it a try. Robert ----- Original Message ----- From: Vlatko Basic Sent: 05/01/14 03:59 AM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse Hi Robert, Current version supports only cabal-dev for sandboxing. New version that is about to come out will support cabal for sandboxing as well. The problem is that installed ghc depends on cabal version being shipped with. And it looks like it'll stay like that for a while, and until it is solved, you'll depend on cabal being shipped with ghc. The problematic packages are bins only, so you can - build them sandboxed and move the bins to .cabal/bin or - move/rename your .cabal and .ghc, cabal update, build cabal-dev and sourcegraph as usual, copy the bins to moved/renamed .cabal/bin, delete newly created .cabal and .ghc, move/rename back your .cabal and .ghc You can go with 3. also, but for a bit larger project you'll run into cabal-hell, so better to use it. It is quite comfortable to work in eclipseFP, so I think it is worth some initial troubles. :-) Best regards, vlatko -------- Original Message -------- Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse From: Robert Weisser Robert.Weisser at gmx.com To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell beginners at haskell.org Date: 01.05.2014 05:17 Thanks. That's great info. ----- Original Message ----- From: Norbert Melzer Sent: 04/30/14 07:19 PM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse Omit cabal-dev it is not maintained anymore and most of its functionality is part of cabal already (cabal sandbox) Am 01.05.2014 00:18 schrieb "Robert Weisser" < Robert.Weisser at gmx.com >: I decided to try out Eclipse with the EclipseFP plug-in for Haskell. I downloaded Eclipse, and then started to install EclipseFP. At some point, I got a screen which said that I needed to install buildwrapper and scion-browser. It offered to install both of them, and optionally install hoogle, hlint, stylish-haskell, SourceGraph, and cabal-dev. The option to install the additional items was pre-checked. I left it checked. The installation of buildwrapper, etc. took a long time. It seemed to be going well until I saw the messages below, which say that installing SourceGraph will likely break several other packages, most of which were part of the EclipseFP installation. I am not a complete novice with Haskell but I am a complete novice with Cabal. I'm not sure what to do. Here are some options I have considered: 1. Use --force-reinstalls and hope for the best. 2. Try to resolve the problem with Cabal, although I don't know to do this. 3. Use EclipseFP without SourceGraph and cabal-dev. 4. Use EclipseFP without SourceGraph, but install cabal-dev using cabal on the command line. 5. Forget about Eclipse altogether, and go back to using vim. Any advice would be appreciated. By the way, I am using Haskell Platform 7.6.3. Here are the messages I saw in the Eclipse console window: Installing executable SourceGraph Resolving dependencies... In order, the following would be installed: asn1-types-0.2.3 (new package) asn1-encoding-0.8.1.3 (new package) asn1-parse-0.8.1 (new package) bktrees-0.3.1 (new package) byteable-0.1.1 (new package) cereal-0.4.0.1 (new package) colour-2.3.3 (new package) cpphs-1.18.2 (new version) crypto-pubkey-types-0.4.2.2 (new package) cryptohash-0.11.4 (new package) digest-0.0.1.2 (new package) dlist-0.5 (new version) aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-0.5.3 (reinstall) cookie-0.4.1.1 (new package) haskell-src-exts-1.13.5 (new version) hslua-0.3.12 (new package) mime-types-0.1.0.4 (new package) multiset-0.2.2 (new package) pandoc-types-1.12.3.2 (new package) pem-0.2.2 (new package) polyparse-1.8 (new version) publicsuffixlist-0.1 (new package) http-client-0.3.2 (new package) regex-pcre-builtin-0.94.4.8.8.34 tel:0.94.4.8.8.34 (new package) highlighting-kate-0.5.6.1 (new package) resourcet-0.4.10.2 (new version) securemem-0.1.3 (new package) crypto-cipher-types-0.0.9 (new package) cipher-aes-0.2.7 (new package) cipher-rc4-0.1.4 (new package) crypto-random-0.0.7 (new package) cprng-aes-0.5.2 (new package) crypto-numbers-0.2.3 (new package) crypto-pubkey-0.2.4 (new package) socks-0.5.4 (new package) temporary-1.1.2.5 (new package) text-stream-decode-0.1.0.5 (new package) conduit-1.0.17.1 (new version) http-client-conduit-0.2.0.1 (new package) wl-pprint-text-1.1.0.2 (new package) graphviz-2999.16.0.0 (new package) x509-1.4.11 (new package) x509-store-1.4.4 (new package) x509-system-1.4.5 (new package) x509-validation-1.5.0 (new package) tls-1.2.6 (new package) connection-0.2.1 (new package) http-client-tls-0.2.1.1 (new package) http-conduit-2.0.0.10 (new package) xml-1.3.13 (new package) texmath-0.6.6.1 (new package) yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, resourcet-1.1.2 -> 0.4.10.2 zip-archive-0.2.2.1 (new package) pandoc-1.12.3.3 (new package) Graphalyze-0.14.0.2 (new package) SourceGraph-0.7.0.5 (new package) cabal: The following packages are likely to be broken by the reinstalls: stylish-haskell-0.5.10.0 scion-browser-0.3.1 persistent-template-1.3.1.3 persistent-sqlite-1.3.0.5 persistent-1.3.0.6 buildwrapper-0.8.0 dynamic-cabal-0.3.1 Use --force-reinstalls if you want to install anyway. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Thu May 1 17:00:28 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Thu, 1 May 2014 13:00:28 -0400 Subject: [Haskell-beginners] replacing fold with scan! In-Reply-To: References: Message-ID: <20140501170028.GA28751@seas.upenn.edu> On Thu, May 01, 2014 at 12:31:38PM +0800, Zhi An Ng wrote: > > So to get this to work, you need to change the type signature of filter'' > > filter'' :: (a -> Bool) -> [a] -> [[a]] Well, but that is not filter, is it? It is possible to define the original filter using scanr, but you have to do a little postprocessing of the output of scanr. -Brent > > > On Thu, May 1, 2014 at 10:42 AM, raffa f wrote: > > > hi everyone! here's my new problem. i wrote my version of filter: > > > > filter' :: (a -> Bool) -> [a] -> [a] > > filter' f = foldr (\x acc -> if f x then x:acc else acc) [] > > > > and it works! however, i wanted to use scan too. so i just replaced foldr > > with scanr, to see what would happen: > > > > filter'' :: (a -> Bool) -> [a] -> [a] > > filter'' f = scanr (\x acc -> if f x then x:acc else acc) [] > > > > but that doesn't work! ghci gives me this: > > > > folds.hs:15:59: > > Couldn't match expected type `a' with actual type `[a0]' > > `a' is a rigid type variable bound by > > the type signature for filter'' :: (a -> Bool) -> [a] -> [a] > > at folds.hs:14:13 > > In the second argument of `scanr', namely `[]' > > In the expression: > > scanr (\ x acc -> if f x then x : acc else acc) [] > > In an equation for filter'': > > filter'' f = scanr (\ x acc -> if f x then x : acc else acc) [] > > Failed, modules loaded: none. > > > > the problem seems to be with the start value of [], it seems? i don't > > understand, i thought scan and fold worked pretty much the same. i learned > > about these functions today, so i'm still trying to wrap my head around > > them... > > > > thank you! > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From vlatko.basic at gmail.com Thu May 1 17:23:08 2014 From: vlatko.basic at gmail.com (Vlatko Basic) Date: Thu, 01 May 2014 19:23:08 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse In-Reply-To: <20140501144416.172110@gmx.com> References: <20140501144416.172110@gmx.com> Message-ID: <536282FC.6060004@gmail.com> An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Fri May 2 08:06:16 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Fri, 02 May 2014 10:06:16 +0200 Subject: [Haskell-beginners] replacing fold with scan! In-Reply-To: References: Message-ID: <536351F8.7000602@fuuzetsu.co.uk> On 05/01/2014 04:42 AM, raffa f wrote: > hi everyone! here's my new problem. i wrote my version of filter: > > filter' :: (a -> Bool) -> [a] -> [a] > filter' f = foldr (\x acc -> if f x then x:acc else acc) [] > > and it works! however, i wanted to use scan too. so i just replaced foldr > with scanr, to see what would happen: > > filter'' :: (a -> Bool) -> [a] -> [a] > filter'' f = scanr (\x acc -> if f x then x:acc else acc) [] > > but that doesn't work! ghci gives me this: > > folds.hs:15:59: > Couldn't match expected type `a' with actual type `[a0]' > `a' is a rigid type variable bound by > the type signature for filter'' :: (a -> Bool) -> [a] -> [a] > at folds.hs:14:13 > In the second argument of `scanr', namely `[]' > In the expression: > scanr (\ x acc -> if f x then x : acc else acc) [] > In an equation for filter'': > filter'' f = scanr (\ x acc -> if f x then x : acc else acc) [] > Failed, modules loaded: none. > > the problem seems to be with the start value of [], it seems? i don't > understand, i thought scan and fold worked pretty much the same. i learned > about these functions today, so i'm still trying to wrap my head around > them... > > thank you! > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Simply look at the types: foldr :: (a -> b -> b) -> b -> [a] -> b Prelude> :t scanr scanr :: (a -> b -> b) -> b -> [a] -> [b] You should now be able to see why your filter'' can't have the same type signature as your filter'. You could just ask what GHCi thinks about your function by removing the signature: Prelude> :t \f -> scanr (\x acc -> if f x then x:acc else acc) [] \f -> scanr (\x acc -> if f x then x:acc else acc) [] :: (a -> Bool) -> [a] -> [[a]] So you're saying that your function takes (a -> Bool) and [a] and returns [a] but that's not the case: it takes (a -> Bool) and [a] and returns [[a]]. -- Mateusz K. From Robert.Weisser at gmx.com Fri May 2 14:14:12 2014 From: Robert.Weisser at gmx.com (Robert Weisser) Date: Fri, 02 May 2014 16:14:12 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse Message-ID: <20140502141412.194770@gmx.com> Hi Vlatko, Thanks again for your help. I'm actually using a Mac, which should be almost exactly like linux. Robert ----- Original Message ----- From: Vlatko Basic Sent: 05/01/14 01:23 PM To: Robert Weisser, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse Hi Robert, The two solutions below are exclusive. Either first or second. When you install ghc, it creates two directories in your $HOME. Those are .cabal and .ghc. In those directories cabal installs packages. cabal-dev and SourceGraph are executable programs only and you do not need their source files, just binary file. So, the proposed solution creates those executable programs, but without disturbing packages you already have installed. Let me know if you need further help. I'm assuming you're on Linux. But the solution for Windows should be similar, but not sure. vlatko -------- Original Message -------- Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse From: Robert Weisser Robert.Weisser at gmx.com To: vlatko.basic at gmail.com , The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell beginners at haskell.org Date: 01.05.2014 16:44 Thanks, Vlatko. I'm not sure I understand everything you said, but I'll give it a try. Robert ----- Original Message ----- From: Vlatko Basic Sent: 05/01/14 03:59 AM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse Hi Robert, Current version supports only cabal-dev for sandboxing. New version that is about to come out will support cabal for sandboxing as well. The problem is that installed ghc depends on cabal version being shipped with. And it looks like it'll stay like that for a while, and until it is solved, you'll depend on cabal being shipped with ghc. The problematic packages are bins only, so you can - build them sandboxed and move the bins to .cabal/bin or - move/rename your .cabal and .ghc, cabal update, build cabal-dev and sourcegraph as usual, copy the bins to moved/renamed .cabal/bin, delete newly created .cabal and .ghc, move/rename back your .cabal and .ghc You can go with 3. also, but for a bit larger project you'll run into cabal-hell, so better to use it. It is quite comfortable to work in eclipseFP, so I think it is worth some initial troubles. :-) Best regards, vlatko -------- Original Message -------- Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse From: Robert Weisser Robert.Weisser at gmx.com To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell beginners at haskell.org Date: 01.05.2014 05:17 Thanks. That's great info. ----- Original Message ----- From: Norbert Melzer Sent: 04/30/14 07:19 PM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Problem installing EclipseFp in Eclipse Omit cabal-dev it is not maintained anymore and most of its functionality is part of cabal already (cabal sandbox) Am 01.05.2014 00:18 schrieb "Robert Weisser" < Robert.Weisser at gmx.com >: I decided to try out Eclipse with the EclipseFP plug-in for Haskell. I downloaded Eclipse, and then started to install EclipseFP. At some point, I got a screen which said that I needed to install buildwrapper and scion-browser. It offered to install both of them, and optionally install hoogle, hlint, stylish-haskell, SourceGraph, and cabal-dev. The option to install the additional items was pre-checked. I left it checked. The installation of buildwrapper, etc. took a long time. It seemed to be going well until I saw the messages below, which say that installing SourceGraph will likely break several other packages, most of which were part of the EclipseFP installation. I am not a complete novice with Haskell but I am a complete novice with Cabal. I'm not sure what to do. Here are some options I have considered: 1. Use --force-reinstalls and hope for the best. 2. Try to resolve the problem with Cabal, although I don't know to do this. 3. Use EclipseFP without SourceGraph and cabal-dev. 4. Use EclipseFP without SourceGraph, but install cabal-dev using cabal on the command line. 5. Forget about Eclipse altogether, and go back to using vim. Any advice would be appreciated. By the way, I am using Haskell Platform 7.6.3. Here are the messages I saw in the Eclipse console window: Installing executable SourceGraph Resolving dependencies... In order, the following would be installed: asn1-types-0.2.3 (new package) asn1-encoding-0.8.1.3 (new package) asn1-parse-0.8.1 (new package) bktrees-0.3.1 (new package) byteable-0.1.1 (new package) cereal-0.4.0.1 (new package) colour-2.3.3 (new package) cpphs-1.18.2 (new version) crypto-pubkey-types-0.4.2.2 (new package) cryptohash-0.11.4 (new package) digest-0.0.1.2 (new package) dlist-0.5 (new version) aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-0.5.3 (reinstall) cookie-0.4.1.1 (new package) haskell-src-exts-1.13.5 (new version) hslua-0.3.12 (new package) mime-types-0.1.0.4 (new package) multiset-0.2.2 (new package) pandoc-types-1.12.3.2 (new package) pem-0.2.2 (new package) polyparse-1.8 (new version) publicsuffixlist-0.1 (new package) http-client-0.3.2 (new package) regex-pcre-builtin-0.94.4.8.8.34 tel:0.94.4.8.8.34 (new package) highlighting-kate-0.5.6.1 (new package) resourcet-0.4.10.2 (new version) securemem-0.1.3 (new package) crypto-cipher-types-0.0.9 (new package) cipher-aes-0.2.7 (new package) cipher-rc4-0.1.4 (new package) crypto-random-0.0.7 (new package) cprng-aes-0.5.2 (new package) crypto-numbers-0.2.3 (new package) crypto-pubkey-0.2.4 (new package) socks-0.5.4 (new package) temporary-1.1.2.5 (new package) text-stream-decode-0.1.0.5 (new package) conduit-1.0.17.1 (new version) http-client-conduit-0.2.0.1 (new package) wl-pprint-text-1.1.0.2 (new package) graphviz-2999.16.0.0 (new package) x509-1.4.11 (new package) x509-store-1.4.4 (new package) x509-system-1.4.5 (new package) x509-validation-1.5.0 (new package) tls-1.2.6 (new package) connection-0.2.1 (new package) http-client-tls-0.2.1.1 (new package) http-conduit-2.0.0.10 (new package) xml-1.3.13 (new package) texmath-0.6.6.1 (new package) yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, resourcet-1.1.2 -> 0.4.10.2 zip-archive-0.2.2.1 (new package) pandoc-1.12.3.3 (new package) Graphalyze-0.14.0.2 (new package) SourceGraph-0.7.0.5 (new package) cabal: The following packages are likely to be broken by the reinstalls: stylish-haskell-0.5.10.0 scion-browser-0.3.1 persistent-template-1.3.1.3 persistent-sqlite-1.3.0.5 persistent-1.3.0.6 buildwrapper-0.8.0 dynamic-cabal-0.3.1 Use --force-reinstalls if you want to install anyway. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From k.bleijenberg at lijbrandt.nl Sat May 3 18:09:09 2014 From: k.bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Sat, 3 May 2014 20:09:09 +0200 Subject: [Haskell-beginners] cabal problems Message-ID: <000001cf66fa$c77c1a10$56744e30$@bleijenberg@lijbrandt.nl> I don't know exactly how, but suddenly I get cabal errors. If I do cabal update there are no errors, everything is fine. Cabal install caball-install => cabal: internal error when reading packages index: could not read tar file entry. The package index or package cache is probably corrupt. Running cabal update might fix it. Every install or update shows the same error message. I'am using win7 64 bits, Haskell platform 2013.2.0.0. If i do cabal - - version => cabal install version 1.18.0.2 using version 1.18.1 of the cabal library. On Google the advice is to clear the directory c:\users\\appData\roaming\cabal empty. But that removes all my packages. Isn't there an easier way? I have never used cabal sandboxes (at least not on purpose) Kees -------------- next part -------------- An HTML attachment was scrubbed... URL: From alanbuxton at gmail.com Sun May 4 15:28:32 2014 From: alanbuxton at gmail.com (Alan Buxton) Date: Sun, 4 May 2014 16:28:32 +0100 Subject: [Haskell-beginners] Using Either and Maybe Message-ID: <000001cf67ad$82a50650$87ef12f0$@gmail.com> So I had a function that would leave me with a Nothing or a Just, and I wanted to use the result of this function to lookup in a Map. So something like this was very easy to do. h> let list = Data.Map.fromList [(1,2),(3,4),(5,6)] h> Just 4 >>= flip Data.Map.lookup list Nothing h> Just 3 >>= flip Data.Map.lookup list Just 4 Now, however, my source function gives me an Either. I ended up writing this function to let me chain Either with Data.Map.lookups: (>>?=) :: (Either a b, a) -> (b -> Maybe c) -> (Either a c,a) (>>?=) (Left x,e) _ = (Left x,e) (>>?=) (Right y,e) f = case f y of Nothing -> (Left e,e) Just z -> (Right z,e) But I can't help thinking I'm reinventing the wheel here somehow. There must be a better way, right? Thanks in advance for any pointers. Alan -------------- next part -------------- An HTML attachment was scrubbed... URL: From dserban01 at gmail.com Sun May 4 15:42:01 2014 From: dserban01 at gmail.com (Dan Serban) Date: Sun, 4 May 2014 18:42:01 +0300 Subject: [Haskell-beginners] Using Either and Maybe In-Reply-To: <000001cf67ad$82a50650$87ef12f0$@gmail.com> References: <000001cf67ad$82a50650$87ef12f0$@gmail.com> Message-ID: Hi Alan, Check out these two functions, they point you towards how to write more idiomatic code. They are called smart destructors, google that term for more information: ?> :i maybe maybe :: b -> (a -> b) -> Maybe a -> b -- Defined in `Data.Maybe' ?> :i either either :: (a -> c) -> (b -> c) -> Either a b -> c -- Defined in `Data.Either' From patrick.john.wheeler at gmail.com Sun May 4 21:35:20 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Sun, 4 May 2014 16:35:20 -0500 Subject: [Haskell-beginners] cabal problems In-Reply-To: <536530e0.c4020e0a.0351.1a00SMTPIN_ADDED_BROKEN@mx.google.com> References: <536530e0.c4020e0a.0351.1a00SMTPIN_ADDED_BROKEN@mx.google.com> Message-ID: I have not run into that problem before. I would download: http://hackage.haskell.org/packages/index.tar.gz There is a link for it near the bottom of the main page: http://hackage.haskell.org On my mac i would copy it to: ~/.cabal/packages/hackage.haskell.org/. I am not sure where the cabal directory is on windows though. If that does not work another option wold but to download the source for cabal-install and compile it. cabal unpack cabal-install, or grab the tarball at the bottom of: http://hackage.haskell.org/package/cabal-install cd into the directory and use `cabal install` That should work if you have all of the dependancies already on your computer. If you don't and the missing dependency list is large(cabal configure can tell you what you are missing) then it might just be quicker to reinstall the haskell platform. Hope that helps, Patrick On Sat, May 3, 2014 at 1:09 PM, Kees Bleijenberg wrote: > I don?t know exactly how, but suddenly I get cabal errors. > > If I do cabal update there are no errors, everything is fine. > > Cabal install caball-install => cabal: internal error when reading > packages index: could not read tar file entry. The package index or > package cache is probably corrupt. Running cabal update might fix it. > > Every install or update shows the same error message. > > > > I?am using win7 64 bits, Haskell platform 2013.2.0.0. If i do cabal - - > version => cabal install version 1.18.0.2 using version 1.18.1 of the cabal > library. > > On Google the advice is to clear the directory > c:\users\\appData\roaming\cabal empty. But that removes all my > packages. Isn?t there an easier way? > > I have never used cabal sandboxes (at least not on purpose) > > > > Kees > > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From edwards.benj at gmail.com Mon May 5 09:30:12 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Mon, 05 May 2014 09:30:12 +0000 Subject: [Haskell-beginners] Using Either and Maybe References: <000001cf67ad$82a50650$87ef12f0$@gmail.com> Message-ID: You might also consider looking at the errors package on hackage. specifically hush and note for converting between Either and Maybe. They are defined here . Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: From k.bleijenberg at lijbrandt.nl Mon May 5 09:32:02 2014 From: k.bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Mon, 5 May 2014 11:32:02 +0200 Subject: [Haskell-beginners] cabal problems In-Reply-To: References: <536530e0.c4020e0a.0351.1a00SMTPIN_ADDED_BROKEN@mx.google.com> Message-ID: <000c01cf6844$df4acd80$9de06880$@bleijenberg@lijbrandt.nl> Thanks Patrick. I think cabal on Windows is different. There is no index.tar.gz on my computer. Maybe I'll wait for a new release of the Haskell Platform to install GHC again :-( Additonal: a few days ago I tried to install eclipseFP and the installation failed with a cabal error (don't know what error). Maybe that is the source of my problems?.? Kees ========================= Van: Beginners [mailto:beginners-bounces at haskell.org] Namens Patrick Wheeler Verzonden: zondag 4 mei 2014 23:35 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: Re: [Haskell-beginners] cabal problems I have not run into that problem before. I would download: http://hackage.haskell.org/packages/index.tar.gz There is a link for it near the bottom of the main page: http://hackage.haskell.org On my mac i would copy it to: ~/.cabal/packages/hackage.haskell.org/. I am not sure where the cabal directory is on windows though. If that does not work another option wold but to download the source for cabal-install and compile it. cabal unpack cabal-install, or grab the tarball at the bottom of: http://hackage.haskell.org/package/cabal-install cd into the directory and use `cabal install` That should work if you have all of the dependancies already on your computer. If you don't and the missing dependency list is large(cabal configure can tell you what you are missing) then it might just be quicker to reinstall the haskell platform. Hope that helps, Patrick On Sat, May 3, 2014 at 1:09 PM, Kees Bleijenberg wrote: I don?t know exactly how, but suddenly I get cabal errors. If I do cabal update there are no errors, everything is fine. Cabal install caball-install => cabal: internal error when reading packages index: could not read tar file entry. The package index or package cache is probably corrupt. Running cabal update might fix it. Every install or update shows the same error message. I?am using win7 64 bits, Haskell platform 2013.2.0.0. If i do cabal - - version => cabal install version 1.18.0.2 using version 1.18.1 of the cabal library. On Google the advice is to clear the directory c:\users\\appData\roaming\cabal empty. But that removes all my packages. Isn?t there an easier way? I have never used cabal sandboxes (at least not on purpose) Kees _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu ________________________________________ No virus found in this message. Checked by AVG - www.avg.com Version: 2014.0.4570 / Virus Database: 3931/7439 - Release Date: 05/04/14 From alanbuxton at gmail.com Mon May 5 10:15:38 2014 From: alanbuxton at gmail.com (Alan Buxton) Date: Mon, 5 May 2014 11:15:38 +0100 Subject: [Haskell-beginners] Using Either and Maybe In-Reply-To: References: <000001cf67ad$82a50650$87ef12f0$@gmail.com> Message-ID: <020401cf684a$f6f05d50$e4d117f0$@gmail.com> Thanks Dan and Ben ? these are exactly what I was looking for. Alan From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Benjamin Edwards Sent: 05 May 2014 10:30 To: beginners at haskell.org Subject: Re: [Haskell-beginners] Using Either and Maybe You might also consider looking at the errors package on hackage. specifically hush and note for converting between Either and Maybe. They are defined here . Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.john.wheeler at gmail.com Mon May 5 12:20:56 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Mon, 5 May 2014 07:20:56 -0500 Subject: [Haskell-beginners] cabal problems In-Reply-To: <53675af3.495f0e0a.275f.ffff8f97SMTPIN_ADDED_BROKEN@mx.google.com> References: <536530e0.c4020e0a.0351.1a00SMTPIN_ADDED_BROKEN@mx.google.com> <53675af3.495f0e0a.275f.ffff8f97SMTPIN_ADDED_BROKEN@mx.google.com> Message-ID: You could try to download cabal binary from: http://www.haskell.org/cabal/download.html This might be a this issue, the error reported looks similar: https://github.com/haskell/cabal/issues/1561 If it is the same then using a newer version of cabal should fix the problem. Patrick On Mon, May 5, 2014 at 4:32 AM, Kees Bleijenberg wrote: > Thanks Patrick. I think cabal on Windows is different. There is no > index.tar.gz on my computer. Maybe I'll wait for a new release of the > Haskell Platform to install GHC again :-( > > Additonal: a few days ago I tried to install eclipseFP and the > installation failed with a cabal error (don't know what error). Maybe that > is the source of my problems?.? > > Kees > ========================= > > Van: Beginners [mailto:beginners-bounces at haskell.org] Namens Patrick > Wheeler > Verzonden: zondag 4 mei 2014 23:35 > Aan: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Onderwerp: Re: [Haskell-beginners] cabal problems > > I have not run into that problem before. > > I would download: > http://hackage.haskell.org/packages/index.tar.gz > > There is a link for it near the bottom of the main page: > http://hackage.haskell.org > > On my mac i would copy it to: > > ~/.cabal/packages/hackage.haskell.org/. > > I am not sure where the cabal directory is on windows though. > > If that does not work another option wold but to download the source for > cabal-install and compile it. > > cabal unpack cabal-install, or grab the tarball at the bottom of: > http://hackage.haskell.org/package/cabal-install > > cd into the directory and use `cabal install` > > That should work if you have all of the dependancies already on your > computer. If you don't and the missing dependency list is large(cabal > configure can tell you what you are missing) then it might just be quicker > to reinstall the haskell platform. > > Hope that helps, > > Patrick > > On Sat, May 3, 2014 at 1:09 PM, Kees Bleijenberg < > k.bleijenberg at lijbrandt.nl> wrote: > I don?t know exactly how, but suddenly I get cabal errors. > If I do cabal update there are no errors, everything is fine. > Cabal install caball-install => cabal: internal error when reading > packages index: could not read tar file entry. The package index or > package cache is probably corrupt. Running cabal update might fix it. > Every install or update shows the same error message. > > I?am using win7 64 bits, Haskell platform 2013.2.0.0. If i do cabal - - > version => cabal install version 1.18.0.2 using version 1.18.1 of the cabal > library. > On Google the advice is to clear the directory > c:\users\\appData\roaming\cabal empty. But that removes all my > packages. Isn?t there an easier way? > I have never used cabal sandboxes (at least not on purpose) > > Kees > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > > -- > Patrick Wheeler > Patrick.John.Wheeler at gmail.com > Patrick.J.Wheeler at rice.edu > Patrick.Wheeler at colorado.edu > ________________________________________ > No virus found in this message. > Checked by AVG - www.avg.com > Version: 2014.0.4570 / Virus Database: 3931/7439 - Release Date: 05/04/14 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From k.bleijenberg at lijbrandt.nl Mon May 5 12:48:52 2014 From: k.bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Mon, 5 May 2014 14:48:52 +0200 Subject: [Haskell-beginners] cabal problems In-Reply-To: References: <536530e0.c4020e0a.0351.1a00SMTPIN_ADDED_BROKEN@mx.google.com> <53675af3.495f0e0a.275f.ffff8f97SMTPIN_ADDED_BROKEN@mx.google.com> Message-ID: <000601cf6860$5e6f3810$1b4da830$@bleijenberg@lijbrandt.nl> Thanks Patrick. First I tried cabal install cabal cabal-install => errors. But downloading and installing the cabal.exe worked. Everything seems okay now. Thanks. Kees =========== Van: Beginners [mailto:beginners-bounces at haskell.org] Namens Patrick Wheeler Verzonden: maandag 5 mei 2014 14:21 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: Re: [Haskell-beginners] cabal problems You could try to download cabal binary from: http://www.haskell.org/cabal/download.html This might be a this issue, the error reported looks similar: https://github.com/haskell/cabal/issues/1561 If it is the same then using a newer version of cabal should fix the problem. Patrick --- snip --- From ari.brandeis.king at gmail.com Mon May 5 16:08:56 2014 From: ari.brandeis.king at gmail.com (Ari King) Date: Mon, 5 May 2014 12:08:56 -0400 Subject: [Haskell-beginners] Pattern Matching & Binding Message-ID: Hi, I've posted (on http://pastebin.com/R5MPNaHs) code I'm working on to practice reading/writing CSV and interacting with DBs. I'd appreciate help in better understanding how to destructure (pattern match) lists/vectors (see line 28 in post) and the difference in "binding" via let/where and <-. I'd also appreciate suggestions on how to improve the code put together thus far to make it more idiomatic. Thanks. Best, Ari -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Mon May 5 16:21:34 2014 From: toad3k at gmail.com (David McBride) Date: Mon, 5 May 2014 12:21:34 -0400 Subject: [Haskell-beginners] Pattern Matching & Binding In-Reply-To: References: Message-ID: I don't have pg installed so I can't run your code but I assume you are breaking on the vector pattern matching. Pattern matching using : only works because the : operator is a constructor in lists. >:i (:) data [] a = ... | a : [a] Remember that : is an infix operator, but it is comparable to Just, Left, or Right. You are trying to use a list constructor to pattern match on a vector which looks nothing like a list. However you can do this sort of pattern matching by using vector's (or almost any other collection's) toList function: hostaddr:port:dbname:username:password:rest = toList vec <- is a monadic binding. You use it when you are dealing with a datatype that happens to be an instance of Monad. It happens to be the only way to get anything out of an IO type, which is what you are using it for here. If something is just using plain types, Int, String, etc, just use lets. On Mon, May 5, 2014 at 12:08 PM, Ari King wrote: > Hi, > > I've posted (on http://pastebin.com/R5MPNaHs) code I'm working on to > practice reading/writing CSV and interacting with DBs. I'd appreciate help > in better understanding how to destructure (pattern match) lists/vectors > (see line 28 in post) and the difference in "binding" via let/where and <-. > I'd also appreciate suggestions on how to improve the code put together > thus far to make it more idiomatic. Thanks. > > Best, > Ari > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.john.wheeler at gmail.com Mon May 5 23:09:39 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Mon, 5 May 2014 18:09:39 -0500 Subject: [Haskell-beginners] cabal problems In-Reply-To: <536788ca.495f0e0a.2778.ffffa74fSMTPIN_ADDED_BROKEN@mx.google.com> References: <536530e0.c4020e0a.0351.1a00SMTPIN_ADDED_BROKEN@mx.google.com> <53675af3.495f0e0a.275f.ffff8f97SMTPIN_ADDED_BROKEN@mx.google.com> <536788ca.495f0e0a.2778.ffffa74fSMTPIN_ADDED_BROKEN@mx.google.com> Message-ID: > But downloading and installing the cabal.exe worked. Everything seems okay now. Thanks. Great glad it worked. On Mon, May 5, 2014 at 7:48 AM, Kees Bleijenberg wrote: > Thanks Patrick. > First I tried cabal install cabal cabal-install => errors. > But downloading and installing the cabal.exe worked. Everything seems okay > now. Thanks. > > Kees > > =========== > > Van: Beginners [mailto:beginners-bounces at haskell.org] Namens Patrick > Wheeler > Verzonden: maandag 5 mei 2014 14:21 > Aan: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Onderwerp: Re: [Haskell-beginners] cabal problems > > You could try to download cabal binary from: > http://www.haskell.org/cabal/download.html > > This might be a this issue, the error reported looks similar: > https://github.com/haskell/cabal/issues/1561 > > If it is the same then using a newer version of cabal should fix the > problem. > > Patrick > --- snip --- > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Mon May 5 00:34:19 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 04 May 2014 19:34:19 -0500 Subject: [Haskell-beginners] Using Either and Maybe In-Reply-To: References: <000001cf67ad$82a50650$87ef12f0$@gmail.com> Message-ID: On 5/4/2014 10:42 AM, Dan Serban wrote: > Hi Alan, > > Check out these two functions, they point you towards how to write > more idiomatic code. They are called smart destructors, google that > term for more information: > > ?> :i maybe > maybe :: b -> (a -> b) -> Maybe a -> b -- Defined in `Data.Maybe' > ?> :i either > either :: (a -> c) -> (b -> c) -> Either a b -> c > -- Defined in `Data.Either' > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > I'm following this thread, and the only "smart destructors" I turned up with Google is this post: The Haskellwiki has a mention but it's a blank page. From ky3 at atamo.com Tue May 6 18:16:04 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Wed, 7 May 2014 01:16:04 +0700 Subject: [Haskell-beginners] Using Either and Maybe In-Reply-To: References: <000001cf67ad$82a50650$87ef12f0$@gmail.com> Message-ID: On Mon, May 5, 2014 at 7:34 AM, John M. Dlugosz wrote: > I'm following this thread, and the only "smart destructors" I turned up > with Google is this post: questions/10161009/input-checks-in-haskell-data-constructors> > I think parent was thinking of smart constructors and mixed them up with catamorphisms, which, partly due to the mystique of category theory and how seemingly only smart people get it, can be seen as smart destructors. The thing to note is that they aren't smart in the same way as smart constructors are. Catas are just dumb destructors, dumb in the same way as plain vanilla "dumb" constructors. The catas for lists, Maybe, and Either are respectively, foldr, maybe, either. There's a body of literature on deriving catas automatically. Look under generics or (older) polytypic programming. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From ari.brandeis.king at gmail.com Tue May 6 18:29:26 2014 From: ari.brandeis.king at gmail.com (Ari King) Date: Tue, 6 May 2014 14:29:26 -0400 Subject: [Haskell-beginners] Pattern Matching & Binding In-Reply-To: References: Message-ID: > > I don't have pg installed so I can't run your code but I assume you are > breaking on the vector pattern matching. Pattern matching using : only > works because the : operator is a constructor in lists. > > >:i (:) > data [] a = ... | a : [a] > > Remember that : is an infix operator, but it is comparable to Just, Left, > or Right. You are trying to use a list constructor to pattern match on a > vector which looks nothing like a list. > > However you can do this sort of pattern matching by using vector's (or > almost any other collection's) toList function: > > hostaddr:port:dbname:username:password:rest = toList vec > > Thanks for the clarification; I was under the impression that vectors were essentially re-implemented lists. > <- is a monadic binding. You use it when you are dealing with a datatype > that happens to be an instance of Monad. It happens to be the only way to > get anything out of an IO type, which is what you are using it for here. > If something is just > using plain types, Int, String, etc, just use lets. > > So, <- is more like extract (from IO type) and bind, but scoping wise is the same as let or where, correct? Lastly, the code fails to read (see line 16 @ http://pastebin.com/R5MPNaHs) the provided file into a ByteString, ByteString readFile complains: Failed reading: conversion error: expected Char, got "127.0.0.1" The file contents are: 127.0.0.1,5432,sample,test_user,test_pwd Am I misusing, ByteString's readFile? Thanks. -Ari -------------- next part -------------- An HTML attachment was scrubbed... URL: From waltaskew at gmail.com Tue May 6 18:54:03 2014 From: waltaskew at gmail.com (Walter Askew) Date: Tue, 6 May 2014 13:54:03 -0500 Subject: [Haskell-beginners] Parsec Many Mishap Message-ID: <547C14DD-CEB0-4CB4-B296-31408A74B832@gmail.com> I am trying to use parsec to turn strings like this: ?#newch The Period #txt It was the best of times #newpar #txt it was the worst of times? into data structures like this: [NewChapter ?The Period?, Text "It was the best of times?, NewParagraph, Text "it was the worst of times?] My current attempt returns this, however: [NewChapter "The Period "] That is, my current implementation only parses out the first item in the string and does not find the remaining items. I?m not sure why that is. I?d suspect I am misusing parsec?s ?many? or ?manyTill? somehow, but it isn?t clear to me why my use of ?many" results in only single item lists. My code is pasted below ? any suggestions? This is my first exploration of parsec, so any general suggestion on using the library are welcome (for instance, I do note that I end up double-parsing the strings ?#newch, #txt and #newpar because both nextCommand and the newChapter newText and newPar parsers all parse those same strings, but I?m not sure how to avoid that elegantly.) Thanks for your help! parser = many command command = newChapter <|> newTxt <|> newPar newChapter = do try (string "#newch") spaces chapterName <- text return (NewChapter chapterName) newTxt = do try (string "#txt") spaces content <- text return (Text content) newPar = do try (string "#newpar") spaces return NewParagraph text = manyTill anyChar nextCommand nextCommand = try (string "#newch") <|> try (string "#txt") <|> try (string "#newpar") <|> eof *> return "" From toad3k at gmail.com Tue May 6 20:40:07 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 6 May 2014 16:40:07 -0400 Subject: [Haskell-beginners] Parsec Many Mishap In-Reply-To: <547C14DD-CEB0-4CB4-B296-31408A74B832@gmail.com> References: <547C14DD-CEB0-4CB4-B296-31408A74B832@gmail.com> Message-ID: First, when you post something like this, please put enough code to at least compile. I had to spend five minutes adding some code to make this compile. The reason it doesn't work is your manyTill anyChar nextCommand. In this case nextCommand actually slurps up the next hashtag, leaving you positioned at " It was ...", then you try to match another command, and it fails, and terminates. The fix is simple, you just have to prevent it from eating the next tag you intended to stop at, which you can do by changing it to manyTill anyChar (lookAhead nextCommand) On Tue, May 6, 2014 at 2:54 PM, Walter Askew wrote: > I am trying to use parsec to turn strings like this: > > ?#newch The Period #txt It was the best of times #newpar #txt it was the > worst of times? > > into data structures like this: > > [NewChapter ?The Period?, Text "It was the best of times?, NewParagraph, > Text "it was the worst of times?] > > My current attempt returns this, however: > > [NewChapter "The Period "] > > That is, my current implementation only parses out the first item in the > string and does not find the remaining items. > I?m not sure why that is. I?d suspect I am misusing parsec?s ?many? or > ?manyTill? somehow, but it isn?t clear to me why my use of ?many" results > in only single item lists. > > My code is pasted below ? any suggestions? This is my first exploration > of parsec, so any general suggestion on using the library are welcome (for > instance, I do note that I end up double-parsing the strings ?#newch, #txt > and #newpar because both nextCommand and the newChapter newText and newPar > parsers all parse those same strings, but I?m not sure how to avoid that > elegantly.) > > Thanks for your help! > > > parser = many command > > command = newChapter <|> newTxt <|> newPar > > newChapter = do > try (string "#newch") > spaces > chapterName <- text > return (NewChapter chapterName) > > newTxt = do > try (string "#txt") > spaces > content <- text > return (Text content) > > newPar = do > try (string "#newpar") > spaces > return NewParagraph > > text = manyTill anyChar nextCommand > > nextCommand = try (string "#newch") > <|> try (string "#txt") > <|> try (string "#newpar") > <|> eof *> return "" > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From waltaskew at gmail.com Tue May 6 21:09:42 2014 From: waltaskew at gmail.com (Walter Askew) Date: Tue, 6 May 2014 16:09:42 -0500 Subject: [Haskell-beginners] Parsec Many Mishap In-Reply-To: References: <547C14DD-CEB0-4CB4-B296-31408A74B832@gmail.com> Message-ID: <7719CE7F-0239-4FD1-9C6C-86A355CA0FFA@gmail.com> > First, when you post something like this, please put enough code to at least compile. I had to spend five minutes adding some code to make this compile. Sorry about that, but thank you very much for your help! > The reason it doesn't work is your manyTill anyChar nextCommand. In this case nextCommand actually slurps up the next hashtag, leaving you positioned at " It was ...", then you try to match another command, and it fails, and terminates. Thanks, that makes sense. I was foolishly looking at the try?s in nextCommand, imagining that they would solve all of my look ahead problems and apparently forgetting they gobble input on success. Thanks again! From emmanuel.surleau at gmail.com Thu May 8 06:13:23 2014 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Thu, 8 May 2014 08:13:23 +0200 Subject: [Haskell-beginners] Pattern Matching & Binding In-Reply-To: References: Message-ID: <20140508061323.GD26863@emm-laptop> On Tue, May 06, 2014 at 02:29:26PM -0400, Ari King wrote: > > > > I don't have pg installed so I can't run your code but I assume you are > > breaking on the vector pattern matching. Pattern matching using : only > > works because the : operator is a constructor in lists. > > > > >:i (:) > > data [] a = ... | a : [a] > > > > Remember that : is an infix operator, but it is comparable to Just, Left, > > or Right. You are trying to use a list constructor to pattern match on a > > vector which looks nothing like a list. > > > > However you can do this sort of pattern matching by using vector's (or > > almost any other collection's) toList function: > > > > hostaddr:port:dbname:username:password:rest = toList vec > > > > > Thanks for the clarification; I was under the impression that vectors were > essentially re-implemented lists. > > > > <- is a monadic binding. You use it when you are dealing with a datatype > > that happens to be an instance of Monad. It happens to be the only way to > > get anything out of an IO type, which is what you are using it for here. > > If something is just > > > using plain types, Int, String, etc, just use lets. > > > > > So, <- is more like extract (from IO type) and bind, but scoping wise is > the same as let or where, correct? It's more complicated than that. Haskell uses monads for IO, this is true, but it is used in many other places. The first example of monads you're likely to find is actually Maybe. For instance: example :: String -> Maybe String example = do -- if someFunctionReturningMaybeInt returns (Just anInt), -- binds 'anInt' to a, otherwise if None just exit the function and return -- None a <- someFunctionReturningMaybeInt b <- someFunctionTakingIntAndReturningMaybeString a -- if someFunctionTakingIntAndReturningMaybeString returns (Just str), -- binds 'str' to b, otherwise if None just exit the function and return -- None Just $ b ++ "_example" -- equivalent to: return $ b ++ "_example" This brings me to the second point: monads are used for sequencing. Here, 'b' is not available in the first line of the function, it hasn't been bound yet. On the other hand, the 'let x = foo in'/'where x = foo' syntax makes the name available in the scope of the function. > Lastly, the code fails to read (see line 16 @ http://pastebin.com/R5MPNaHs) > the provided file into a ByteString, ByteString readFile complains: > > Failed reading: conversion error: expected Char, got "127.0.0.1" > > The file contents are: 127.0.0.1,5432,sample,test_user,test_pwd > > Am I misusing, ByteString's readFile? Thanks. > > -Ari > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From toad3k at gmail.com Thu May 8 06:47:10 2014 From: toad3k at gmail.com (David McBride) Date: Thu, 8 May 2014 02:47:10 -0400 Subject: [Haskell-beginners] Pattern Matching & Binding In-Reply-To: References: Message-ID: The reason cassava is giving you an error is that you are telling it to expect a Vector of Strings. You happen to only have one, but a csv has multiple lines. So that should be a Vector of Vectors of Strings. The reason is says char is that it is assuming what is actually in the file is a Vector of a List of Chars which also fits the pattern, because String is a list of chars. Your original type would work with an input of a,b,c d,e,f which is potentially valid, but for what you actually want, try this: csvData <- DBL.readFile $ head args case decode NoHeader csvData of Left err -> putStrLn err Right vec -> print (vec :: DV.Vector (DV.Vector String)) On Tue, May 6, 2014 at 2:29 PM, Ari King wrote: > I don't have pg installed so I can't run your code but I assume you are >> breaking on the vector pattern matching. Pattern matching using : only >> works because the : operator is a constructor in lists. >> >> >:i (:) >> data [] a = ... | a : [a] >> >> Remember that : is an infix operator, but it is comparable to Just, Left, >> or Right. You are trying to use a list constructor to pattern match on a >> vector which looks nothing like a list. >> >> However you can do this sort of pattern matching by using vector's (or >> almost any other collection's) toList function: >> >> hostaddr:port:dbname:username:password:rest = toList vec >> >> > Thanks for the clarification; I was under the impression that vectors were > essentially re-implemented lists. > > >> <- is a monadic binding. You use it when you are dealing with a datatype >> that happens to be an instance of Monad. It happens to be the only way to >> get anything out of an IO type, which is what you are using it for here. >> If something is just >> > using plain types, Int, String, etc, just use lets. >> >> > So, <- is more like extract (from IO type) and bind, but scoping wise is > the same as let or where, correct? > > Lastly, the code fails to read (see line 16 @ http://pastebin.com/R5MPNaHs) > the provided file into a ByteString, ByteString readFile complains: > > Failed reading: conversion error: expected Char, got "127.0.0.1" > > The file contents are: 127.0.0.1,5432,sample,test_user,test_pwd > > Am I misusing, ByteString's readFile? Thanks. > > -Ari > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Thu May 8 08:26:27 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Thu, 08 May 2014 02:26:27 -0600 Subject: [Haskell-beginners] =?utf-8?q?How_do_I_do_inheritance_in_haskell?= =?utf-8?q?=3F?= Message-ID: Hi! I am trying to understand how to "factor out" common functionality in haskell. (All the code is available here https://github.com/defigueiredo/inheritance-in-haskell [1]) I am receiving orderbook data from a Bitcoin Exchange, through their JSON API (available at https://www.bitstamp.net/api/order_book/ [2]) I have a generic JSON parser that parses the text response and gives me back a generic JSON object I called Jvalue. This data type represents any JSON object and is defined as follows. (see json.org for full JSON specs) ------------------ import qualified Data.Map as Map data Jvalue = Jobject (Map.Map String Jvalue) | Jarray [Jvalue] | Jstring String | Jnumber Double | Jbool Bool | Jnull deriving Show ------------------- Now, I have to convert this (if possible) into an OrderBook type. An orderbook is basically two lists. One list of the orders placed by people who want to buy, the "bids", and the other with the orders of people who want to sell, the "asks". Each order specifies what price should be paid and how many bitcoins to buy/sell (the volume). The functions for generating the lists of bids and asks are essentially the same. However, they might need to know if they are creating bids or asks. So, I may need to pass a parameter with that information. I can make that parameter a function or a type (or I may not need it). These three different ideas give rise to 3 different ways to define the orderbook: 1) We can make each Order a type with two constructors (my initial idea): ------------------- newtype Volume = Volume Double deriving (Show) newtype Price = Price Double deriving (Show) data Order = Bid {price::Price, volume::Volume} | Ask {price::Price, volume::Volume} deriving (Show) data OrderBook = OrderBook{ bids::[Order] , asks::[Order] } deriving (Show) ------------------- 2) We can make Bid and Ask types themselves ------------------- newtype Volume = Volume Double deriving (Show) newtype Price = Price Double deriving (Show) data Order = Order {price::Price, volume::Volume} deriving (Show) newtype Bid = Bid Order deriving Show newtype Ask = Ask Order deriving Show data OrderBook = OrderBook{ bids::[Bid] , asks::[Ask] } deriving (Show) ------------------- 3) We can make Order a class ------------------- newtype Volume = Volume Double deriving (Show) newtype Price = Price Double deriving (Show) class Order a where makeOrder :: String -> String -> a data Ask = Ask {aprice::Price, avolume::Volume} deriving (Show) instance Order Ask where makeOrder = makeAsk data Bid = Bid {bprice::Price, bvolume::Volume} deriving (Show) instance Order Bid where makeOrder = makeBid data OrderBook = OrderBook{ bids::[Bid] , asks::[Ask] } deriving (Show) -- makeAsk and MakeBid defined later ------------------- It is not clear to me which one of these should be used and, most importantly, which criteria should be used to choose between them. I wrote up the code to do the conversion of the Jvalue object into an OrderBook in the 3 different ways. (The third one came out of a discussion from a haskell meetup group.) (All the code is available here https://github.com/defigueiredo/inheritance-in-haskell [1]) Any insights on how to choose between them would be *greatly* appreciated! Thanks, Dimitri Links: ------ [1] https://github.com/defigueiredo/inheritance-in-haskell [2] https://www.bitstamp.net/api/order_book/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dserban01 at gmail.com Thu May 8 08:58:28 2014 From: dserban01 at gmail.com (Dan Serban) Date: Thu, 8 May 2014 11:58:28 +0300 Subject: [Haskell-beginners] How do I do inheritance in haskell? In-Reply-To: References: Message-ID: >The functions for generating the lists of bids and asks are essentially the same. Create a typeclass umbrella for them. Your option 3 looks good, but it doesn't go far enough. Whenever you're presented with an opportunity to "harden" your design by encoding problem domain invariants into the type system, you should seize it - on principled grounds. This goes back to one of the unique selling point of Haskell, which is that by careful type design you can eliminate entire classes of bugs. I tend to favor designs that look like this: newtype AskVolume = AskVolume Double deriving (Show) newtype AskPrice = AskPrice Double deriving (Show) newtype BidVolume = BidVolume Double deriving (Show) newtype BidPrice = BidPrice Double deriving (Show) data AskOrder = AskOrder { aprice :: AskPrice, avolume :: AskVolume } deriving (Show) data BidOrder = BidOrder { bprice :: BidPrice, bvolume :: BidVolume } deriving (Show) data OrderBook = OrderBook { bids :: [BidOrder], asks :: [AskOrder] } deriving (Show) This is Haskell, so there are likely opportunities to move even more stuff into the type system, but I'm not aware of them. -Dan From daniel.trstenjak at gmail.com Thu May 8 14:21:48 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Thu, 8 May 2014 16:21:48 +0200 Subject: [Haskell-beginners] How do I do inheritance in haskell? In-Reply-To: References: Message-ID: <20140508142148.GA25718@machine> Hi Dimitri, > It is not clear to me which one of these should be used and, most importantly, > which criteria should be used to choose between them. One criteria should be what you want to achieve, how you want to operate with your data. > 1) > newtype Volume = Volume Double deriving (Show) > newtype Price = Price Double deriving (Show) > > data Order = Bid {price::Price, volume::Volume} > | Ask {price::Price, volume::Volume} > deriving (Show) > > data OrderBook = OrderBook{ bids::[Order] , asks::[Order] } deriving (Show) This represenation of an Order gives you the ability to put Bids and Asks e.g. into the same list, but if you always want to keep them separate, then there's no point to have an ADT in the first place. > 2) > newtype Volume = Volume Double deriving (Show) > newtype Price = Price Double deriving (Show) > > data Order = Order {price::Price, volume::Volume} deriving (Show) > > newtype Bid = Bid Order deriving Show > newtype Ask = Ask Order deriving Show > > data OrderBook = OrderBook{ bids::[Bid] , asks::[Ask] } deriving (Show) If you want to keep Bids and Asks always separate, then this represenation seems to be more fitting. > 3) > newtype Volume = Volume Double deriving (Show) > newtype Price = Price Double deriving (Show) > > class Order a where > makeOrder :: String -> String -> a > > data Ask = Ask {aprice::Price, avolume::Volume} deriving (Show) > instance Order Ask where > makeOrder = makeAsk > > data Bid = Bid {bprice::Price, bvolume::Volume} deriving (Show) > instance Order Bid where > makeOrder = makeBid > > data OrderBook = OrderBook{ bids::[Bid] , asks::[Ask] } deriving (Show) I might prefer the Asks and Bids data types of 2), because factoring out the common parts might have future benefits, by being able to reuse functionality on the common parts. The type class here fulfills something completely different to the previous two examples and is about the creation of your Asks and Bids. I don't think that you really need this type class, but you could just use makeAsk and makeBid without losing anything, because as long as your types in OrderBook are fixed you gain nothing. Greetings, Daniel From daniel.trstenjak at gmail.com Thu May 8 14:24:50 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Thu, 8 May 2014 16:24:50 +0200 Subject: [Haskell-beginners] How do I do inheritance in haskell? In-Reply-To: References: Message-ID: <20140508142450.GB25718@machine> Hi Dan, On Thu, May 08, 2014 at 11:58:28AM +0300, Dan Serban wrote: > newtype AskVolume = AskVolume Double deriving (Show) > newtype AskPrice = AskPrice Double deriving (Show) > > newtype BidVolume = BidVolume Double deriving (Show) > newtype BidPrice = BidPrice Double deriving (Show) I think that's a bit over the top, because why shouldn't prices and volumes be interchangeable between Asks and Bids? Greetings, Daniel From edwards.benj at gmail.com Thu May 8 14:28:21 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Thu, 08 May 2014 14:28:21 +0000 Subject: [Haskell-beginners] How do I do inheritance in haskell? References: <20140508142450.GB25718@machine> Message-ID: > > I think that's a bit over the top, because why shouldn't prices > and volumes be interchangeable between Asks and Bids? It's a good question. I guess as long as a price / volume is always wrapped in a type that's tagged bid / ask then everything is great. You just want to avoid being the next knight capital and ending up trading the wrong side of the spread. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davidleothomas at gmail.com Thu May 8 15:49:51 2014 From: davidleothomas at gmail.com (David Thomas) Date: Thu, 8 May 2014 08:49:51 -0700 Subject: [Haskell-beginners] How do I do inheritance in haskell? In-Reply-To: <20140508142450.GB25718@machine> References: <20140508142450.GB25718@machine> Message-ID: Because confusing them is disastrous, so there is motivation to go to extra lengths to keep them separate. I don't have particular thoughts on whether that motivation is sufficient in this case. On Thu, May 8, 2014 at 7:24 AM, Daniel Trstenjak wrote: > > Hi Dan, > > On Thu, May 08, 2014 at 11:58:28AM +0300, Dan Serban wrote: >> newtype AskVolume = AskVolume Double deriving (Show) >> newtype AskPrice = AskPrice Double deriving (Show) >> >> newtype BidVolume = BidVolume Double deriving (Show) >> newtype BidPrice = BidPrice Double deriving (Show) > > I think that's a bit over the top, because why shouldn't prices > and volumes be interchangeable between Asks and Bids? > > > Greetings, > Daniel > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From jdrube at mail.de Thu May 8 20:25:37 2014 From: jdrube at mail.de (Julian Drube) Date: Thu, 08 May 2014 22:25:37 +0200 Subject: [Haskell-beginners] Problem with installing SDL Message-ID: <7141757.TlrQdfbG5o@julianspc> Hi, i'm trying to install the SDL package with: cabal install sdl but the following error is showing up: Graphics/UI/SDL/Events.hsc:56:23: Module ?Data.Typeable? does not export ?Typeable(typeOf)? cabal: Error: some packages failed to install: SDL-0.6.5 failed during the building phase. The exception was: ExitFailure 1 GHC version is 7.8.2 Thanks in advance for any ideas Julian From patrick.john.wheeler at gmail.com Thu May 8 22:43:41 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Thu, 8 May 2014 17:43:41 -0500 Subject: [Haskell-beginners] Problem with installing SDL In-Reply-To: <7141757.TlrQdfbG5o@julianspc> References: <7141757.TlrQdfbG5o@julianspc> Message-ID: The error is saying that it can not find `typeOf` in the `Typeable` class. This is because that function is not longer part of the `Typeable` in ghc7.8 and moving forward. Error like this are still be sorted out for ghc 7.8. You are on a bleeding edge version of ghc so packaging problems like this are expected. Package stability is usually marked by when the Haskell Platform updates to the new ghc version. So if you want additional stability HP is where it is at. If you want to live and the bleeding edge then you can download sdl with cabal unpack and see if you can not fix the problem for yourself. Here is a link to the `Typeable` module in base: http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.htmlOf There is replacement function for backward compatibility. http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.html#v:typeOf If you fixed the import statement for all of the sdl modules, you might fix the incompatibility with the new Typeable. There will probably be a few other issues that need solving after this one. I have updated several packages on a case by case basis when I need them to new ghc version and has been a lesson in understanding ghc errors and fixing them. If you have the time I recommend giving it a shot. Patrick On Thu, May 8, 2014 at 3:25 PM, Julian Drube wrote: > Hi, > > i'm trying to install the SDL package with: > > cabal install sdl > > but the following error is showing up: > > Graphics/UI/SDL/Events.hsc:56:23: > Module ?Data.Typeable? does not export ?Typeable(typeOf)? > cabal: Error: some packages failed to install: > SDL-0.6.5 failed during the building phase. The exception was: > ExitFailure 1 > > > GHC version is 7.8.2 > > > Thanks in advance for any ideas > Julian > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Thu May 8 22:50:12 2014 From: karl at karlv.net (Karl Voelker) Date: Thu, 08 May 2014 15:50:12 -0700 Subject: [Haskell-beginners] How do I do inheritance in haskell? In-Reply-To: References: Message-ID: <1399589412.31911.115307721.41F5F025@webmail.messagingengine.com> On Thu, May 8, 2014, at 01:26 AM, Dimitri DeFigueiredo wrote: Now, I have to convert this (if possible) into an OrderBook type. An orderbook is basically two lists. One list of the orders placed by people who want to buy, the "bids", and the other with the orders of people who want to sell, the "asks". Each order specifies what price should be paid and how many bitcoins to buy/sell (the volume). There are more representations you could choose. One example, if you want bids and asks to have the same type: data OrderType = Bid | Ask data Order = Order OrderType Price Volume And if you don't want them to have the same type: data BidType -- requires -XEmptyDataDecls data AskType data Order a = Order Price Volume type Bid = Order BidType type Ask = Order AskType -Karl -------------- next part -------------- An HTML attachment was scrubbed... URL: From jdrube at mail.de Fri May 9 08:36:49 2014 From: jdrube at mail.de (Julian Drube) Date: Fri, 09 May 2014 10:36:49 +0200 Subject: [Haskell-beginners] Problem with installing SDL In-Reply-To: References: <7141757.TlrQdfbG5o@julianspc> Message-ID: <73665494.gc7JfWPL4j@julianspc> Thank you, In the file `Graphics/UI/SDL/Events.hsc` I changed the line import Data.Typeable (Typeable(typeOf),TypeRep) to just import Data.Typeable and it compiles without problems now. Julian On Thursday 08 May 2014 17:43:41 Patrick Wheeler wrote: > The error is saying that it can not find `typeOf` in the `Typeable` class. > This is because that function is not longer part of the `Typeable` in > ghc7.8 and moving forward. Error like this are still be sorted out for ghc > 7.8. You are on a bleeding edge version of ghc so packaging problems like > this are expected. Package stability is usually marked by when the Haskell > Platform updates to the new ghc version. > > So if you want additional stability HP is where it is at. If you want to > live and the bleeding edge then you can download sdl with cabal unpack and > see if you can not fix the problem for yourself. > > Here is a link to the `Typeable` module in base: > http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.htmlOf > > There is replacement function for backward compatibility. > http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.html#v:ty > peOf > > If you fixed the import statement for all of the sdl modules, you might fix > the incompatibility with the new Typeable. There will probably be a few > other issues that need solving after this one. I have updated several > packages on a case by case basis when I need them to new ghc version and > has been a lesson in understanding ghc errors and fixing them. If you have > the time I recommend giving it a shot. > > Patrick From patrick.john.wheeler at gmail.com Fri May 9 10:19:08 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Fri, 9 May 2014 05:19:08 -0500 Subject: [Haskell-beginners] Problem with installing SDL In-Reply-To: <73665494.gc7JfWPL4j@julianspc> References: <7141757.TlrQdfbG5o@julianspc> <73665494.gc7JfWPL4j@julianspc> Message-ID: Great, I'm glad that it was a simple fix. Have fun hacking on sdl! Patrick On Fri, May 9, 2014 at 3:36 AM, Julian Drube wrote: > Thank you, > > In the file `Graphics/UI/SDL/Events.hsc` I changed the line > > import Data.Typeable (Typeable(typeOf),TypeRep) > > to just > > import Data.Typeable > > and it compiles without problems now. > > Julian > > On Thursday 08 May 2014 17:43:41 Patrick Wheeler wrote: > > The error is saying that it can not find `typeOf` in the `Typeable` > class. > > This is because that function is not longer part of the `Typeable` in > > ghc7.8 and moving forward. Error like this are still be sorted out for > ghc > > 7.8. You are on a bleeding edge version of ghc so packaging problems > like > > this are expected. Package stability is usually marked by when the > Haskell > > Platform updates to the new ghc version. > > > > So if you want additional stability HP is where it is at. If you want to > > live and the bleeding edge then you can download sdl with cabal unpack > and > > see if you can not fix the problem for yourself. > > > > Here is a link to the `Typeable` module in base: > > > http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.htmlOf > > > > There is replacement function for backward compatibility. > > > http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.html#v:ty > > peOf > > > > If you fixed the import statement for all of the sdl modules, you might > fix > > the incompatibility with the new Typeable. There will probably be a few > > other issues that need solving after this one. I have updated several > > packages on a case by case basis when I need them to new ghc version and > > has been a lesson in understanding ghc errors and fixing them. If you > have > > the time I recommend giving it a shot. > > > > Patrick > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From michal at bazzle.me Sat May 10 12:05:08 2014 From: michal at bazzle.me (Michal Kawalec) Date: Sat, 10 May 2014 13:05:08 +0100 Subject: [Haskell-beginners] Learning Haskell with the help of trees Message-ID: <536E15F4.2070807@bazzle.me> Hi, I am beginning to learn Haskell, and I decided to try to implement a tree. Its code is available at https://gist.github.com/anonymous/dd3eaa8bc36025d7751c I would need some help with implementing the delete element procedure. I have a feeling I am doing something very unhaskelly there. Also, when deleting a node having two children, how do I run two actions (swap the values and delete the successor)? Also, I would be delighted if you could take a look on the style and similar aspects, as they are best addressed at the beginning. Thanks, Michal -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 555 bytes Desc: OpenPGP digital signature URL: From bob at redivi.com Sat May 10 15:27:10 2014 From: bob at redivi.com (Bob Ippolito) Date: Sat, 10 May 2014 08:27:10 -0700 Subject: [Haskell-beginners] Learning Haskell with the help of trees In-Reply-To: <536E15F4.2070807@bazzle.me> References: <536E15F4.2070807@bazzle.me> Message-ID: In order to implement delete you need to have some sort of merge operation for the cases where the node being deleted has more than one child. In this case your tree data structure doesn't appear to be a search tree or balanced in any way so it's much harder to generally decide how delete should work. On Sat, May 10, 2014 at 5:05 AM, Michal Kawalec wrote: > Hi, > > I am beginning to learn Haskell, and I decided to try to implement a > tree. Its code is available at > https://gist.github.com/anonymous/dd3eaa8bc36025d7751c > > I would need some help with implementing the delete element procedure. I > have a feeling I am doing something very unhaskelly there. Also, when > deleting a node having two children, how do I run two actions (swap the > values and delete the successor)? > > Also, I would be delighted if you could take a look on the style and > similar aspects, as they are best addressed at the beginning. > > > > Thanks, > Michal > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michal at bazzle.me Sat May 10 17:51:59 2014 From: michal at bazzle.me (Michal Kawalec) Date: Sat, 10 May 2014 18:51:59 +0100 Subject: [Haskell-beginners] Learning Haskell with the help of trees In-Reply-To: References: <536E15F4.2070807@bazzle.me> Message-ID: <536E673F.8030403@bazzle.me> On 10.05.2014 16:27, Bob Ippolito wrote: > In order to implement delete you need to have some sort of merge > operation for the cases where the node being deleted has more than one > child. In this case your tree data structure doesn't appear to be a > search tree or balanced in any way so it's much harder to generally > decide how delete should work. But this is a binary (albeit unbalanced, which changes nothing) tree :) Michal -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 555 bytes Desc: OpenPGP digital signature URL: From bob at redivi.com Sat May 10 18:25:47 2014 From: bob at redivi.com (Bob Ippolito) Date: Sat, 10 May 2014 11:25:47 -0700 Subject: [Haskell-beginners] Learning Haskell with the help of trees In-Reply-To: <536E673F.8030403@bazzle.me> References: <536E15F4.2070807@bazzle.me> <536E673F.8030403@bazzle.me> Message-ID: Sure, but you still have to make some arbitrary decisions that define the strategy for making one tree out of two. It's harder when the data structure doesn't make that decision for you by its definition. Write that function and then your delete is practically finished. On Saturday, May 10, 2014, Michal Kawalec wrote: > On 10.05.2014 16:27, Bob Ippolito wrote: > > In order to implement delete you need to have some sort of merge > > operation for the cases where the node being deleted has more than one > > child. In this case your tree data structure doesn't appear to be a > > search tree or balanced in any way so it's much harder to generally > > decide how delete should work. > > > > But this is a binary (albeit unbalanced, which changes nothing) tree :) > > > Michal > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jan.snajder at fer.hr Sun May 11 11:24:39 2014 From: jan.snajder at fer.hr (Jan Snajder) Date: Sun, 11 May 2014 13:24:39 +0200 Subject: [Haskell-beginners] Space leak while reading from a file? Message-ID: <536F5DF7.6000103@fer.hr> Dear all, I'm trying to implement a simple file-based database. I apparently have a space leak, but I have no clue where it comes from. Here's the file-based database implementation: http://pastebin.com/QqiqcXFw The idea to have a database table in a single textual file. One line equals one table row. The fields within a row are whitespace separated. The first field is the key. Because I'd like to work with large files, I don't want to load the whole file into memory. Instead, I'd like to be able to fetch the rows on demand, by keys. Thus I first create an index that links keys to file seeks. I use the readerT to add the index to the IO monad. For testing, I use a dummy table produced as follows: import System.IO import Text.Printf import Control.Monad row = unwords [printf "field%03d" (i::Int) | i <- [1..999]] main = do forM_ [1..250000] $ \i -> putStrLn $ printf "row%06d %s" (i::Int) row This generates a 2.1G textual file, which I store on my disk. The testing code: import FileDB import qualified Data.Text as T import Text.Printf import Control.Applicative import Control.Monad import Control.Monad.Trans import System.IO import System.Environment main = do (f:_) <- getArgs t <- openTable f runDB t $ do ks <- getKeys liftIO $ do putStrLn . printf "%d keys read" $ length ks putStrLn "Press any key to continue..." getChar forM_ ks $ \k -> do Just r <- getRow k liftIO . putStrLn $ printf "Row \"%s\" has %d fields" (T.unpack k) (length r) When I run the test on the 2.1GB file, the whole program consumes 10GB. 6GB seem to be allocated after the index is built (just before entering the forM_ function). The remaining 4GB are allocated while fetching all the rows. I find both things difficult to explain. 6GB seems too much for the index. Each key is 9 characters (stored as Data.Text), and I have 250K such keys in a Data.Map. Should this really add up to 6GB? Also, I have no idea why fetching all the rows, one by one, should consume any additional memory. Each row is fetched and its length is computed and printed out. I see no reason for the rows to be retained in the memory. Here's the memory allocation summary: > 1,093,931,338,632 bytes allocated in the heap > 2,225,144,704 bytes copied during GC > 4,533,898,000 bytes maximum residency (26 sample(s)) > 3,080,926,336 bytes maximum slop > 10004 MB total memory in use (0 MB lost due to fragmentation) > > Tot time (elapsed) Avg pause Max pause > Gen 0 2171739 colls, 0 par 45.29s 45.26s 0.0000s 0.0030s > Gen 1 26 colls, 0 par 1.50s 1.53s 0.0589s 0.7087s > > INIT time 0.00s ( 0.00s elapsed) > MUT time 279.92s (284.85s elapsed) > GC time 46.80s ( 46.79s elapsed) > EXIT time 0.68s ( 0.71s elapsed) > Total time 327.40s (332.35s elapsed) > > %GC time 14.3% (14.1% elapsed) > > Alloc rate 3,908,073,170 bytes per MUT second > > Productivity 85.7% of total user, 84.4% of total elapsed Btw., I don't get the "bytes allocated in the heap" figure, which is approx. 1000 GB (?). I'm obviously doing something wrong here. I'd be thankful for any help. Best, Jan From c.venu at aol.com Mon May 12 15:44:18 2014 From: c.venu at aol.com (Venu Chakravorty) Date: Mon, 12 May 2014 11:44:18 -0400 (EDT) Subject: [Haskell-beginners] Addition of "Float" and "Int". Message-ID: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> Hello everyone, I am just starting with Haskell so please bear with me. Here's my question: Consider the below definition / output: Prelude> :t (+) (+) :: (Num a) => a -> a -> a What I understand from the above is that "+" is a function that takes two args which are types of anything that IS-AN instance of "Num" (Int, Integer, Float, Double) and returns an instance of "Num". Hence this works fine: Prelude> 4.3 + 2 6.3 But I can't understand why this doesn't work: Prelude> 4.3 + 4 :: Int :1:0: No instance for (Fractional Int) arising from the literal `4.3' at :1:0-2 Possible fix: add an instance declaration for (Fractional Int) In the first argument of `(+)', namely `4.3' In the expression: 4.3 + 4 :: Int In the definition of `it': it = 4.3 + 4 :: Int I expected that the second addition would work as both "Float" and "Int" are instances of "Num". Is it that since both the formal args are defined as "a" they have to be exactly the same instances? Had "+" been defined something like: (+) :: (Num a, Num b) => a -> b -> a my second addition would have worked? Please let me know what I am missing. Regards, Venu Chakravorty. -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Mon May 12 16:28:32 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 12 May 2014 12:28:32 -0400 Subject: [Haskell-beginners] Addition of "Float" and "Int". In-Reply-To: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> References: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> Message-ID: On Mon, May 12, 2014 at 11:44 AM, Venu Chakravorty wrote: > Prelude> :t (+) > (+) :: (Num a) => a -> a -> a > > What I understand from the above is that "+" is a function that takes > two args > which are types of anything that IS-AN instance of "Num" (Int, Integer, > Float, Double) > and returns an instance of "Num". > Not exactly. It says that, given some type a that is an instance of Num, it will add two values of that type and produce a new value of that same type. You cannot mix and match types; it always works on some specific type, although those types may change between uses of (+). This is somewhat hidden by the way numeric literals are handled: a literal without a decimal point is handled as if you had wrapped it in fromIntegral, and one with a decimal point is handled as if you had wrapped it in fromRational. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Mon May 12 16:33:20 2014 From: toad3k at gmail.com (David McBride) Date: Mon, 12 May 2014 12:33:20 -0400 Subject: [Haskell-beginners] Space leak while reading from a file? In-Reply-To: <536F5DF7.6000103@fer.hr> References: <536F5DF7.6000103@fer.hr> Message-ID: This is a bit advanced for the beginners list. You would probably have better luck on stackoverflow. On Sun, May 11, 2014 at 7:24 AM, Jan Snajder wrote: > Dear all, > > I'm trying to implement a simple file-based database. I apparently have > a space leak, but I have no clue where it comes from. > > Here's the file-based database implementation: > http://pastebin.com/QqiqcXFw > > The idea to have a database table in a single textual file. One line > equals one table row. The fields within a row are whitespace separated. > The first field is the key. Because I'd like to work with large files, I > don't want to load the whole file into memory. Instead, I'd like to be > able to fetch the rows on demand, by keys. Thus I first create an index > that links keys to file seeks. I use the readerT to add the index to the > IO monad. > > For testing, I use a dummy table produced as follows: > > import System.IO > import Text.Printf > import Control.Monad > > row = unwords [printf "field%03d" (i::Int) | i <- [1..999]] > > main = do > forM_ [1..250000] $ \i -> > putStrLn $ printf "row%06d %s" (i::Int) row > > This generates a 2.1G textual file, which I store on my disk. > > The testing code: > > import FileDB > import qualified Data.Text as T > import Text.Printf > import Control.Applicative > import Control.Monad > import Control.Monad.Trans > import System.IO > import System.Environment > > main = do > (f:_) <- getArgs > t <- openTable f > runDB t $ do > ks <- getKeys > liftIO $ do > putStrLn . printf "%d keys read" $ length ks > putStrLn "Press any key to continue..." > getChar > forM_ ks $ \k -> do > Just r <- getRow k > liftIO . putStrLn $ printf "Row \"%s\" has %d fields" > (T.unpack k) (length r) > > When I run the test on the 2.1GB file, the whole program consumes 10GB. > > 6GB seem to be allocated after the index is built (just before entering > the forM_ function). The remaining 4GB are allocated while fetching all > the rows. > > I find both things difficult to explain. > > 6GB seems too much for the index. Each key is 9 characters (stored as > Data.Text), and I have 250K such keys in a Data.Map. Should this really > add up to 6GB? > > Also, I have no idea why fetching all the rows, one by one, should > consume any additional memory. Each row is fetched and its length is > computed and printed out. I see no reason for the rows to be retained in > the memory. > > Here's the memory allocation summary: > > > 1,093,931,338,632 bytes allocated in the heap > > 2,225,144,704 bytes copied during GC > > 4,533,898,000 bytes maximum residency (26 sample(s)) > > 3,080,926,336 bytes maximum slop > > 10004 MB total memory in use (0 MB lost due to fragmentation) > > > > Tot time (elapsed) Avg pause Max > pause > > Gen 0 2171739 colls, 0 par 45.29s 45.26s 0.0000s > 0.0030s > > Gen 1 26 colls, 0 par 1.50s 1.53s 0.0589s > 0.7087s > > > > INIT time 0.00s ( 0.00s elapsed) > > MUT time 279.92s (284.85s elapsed) > > GC time 46.80s ( 46.79s elapsed) > > EXIT time 0.68s ( 0.71s elapsed) > > Total time 327.40s (332.35s elapsed) > > > > %GC time 14.3% (14.1% elapsed) > > > > Alloc rate 3,908,073,170 bytes per MUT second > > > > Productivity 85.7% of total user, 84.4% of total elapsed > > > Btw., I don't get the "bytes allocated in the heap" figure, which is > approx. 1000 GB (?). > > I'm obviously doing something wrong here. I'd be thankful for any help. > > Best, > Jan > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Mon May 12 16:54:12 2014 From: bob at redivi.com (Bob Ippolito) Date: Mon, 12 May 2014 09:54:12 -0700 Subject: [Haskell-beginners] Space leak while reading from a file? In-Reply-To: <536F5DF7.6000103@fer.hr> References: <536F5DF7.6000103@fer.hr> Message-ID: I haven't looked closely but I suspect if you use foldM to build your Map rather than untilM it might consume less memory since you won't be allocating this big list. BUT? the real reason why the memory usage is so much higher than you expect is because slicing a Data.Text is an O(1) operation that shares the underlying buffer between the original and the slice. Calling T.words will ensure that the full original line stays around. You can see this in the implementation: http://hackage.haskell.org/package/text-0.11.2.0/docs/src/Data-Text.html#words This behavior is documented somewhat near there in the docs, but I think it should really be a top-level thing: http://hackage.haskell.org/package/text-0.11.2.0/docs/Data-Text.html#g:18 I don't know what function to use to force the array to be copied, hopefully there is one! Erlang's binaries work similarly and there is a copy function for them for exactly this purpose: http://www.erlang.org/doc/man/binary.html -bob On Sun, May 11, 2014 at 4:24 AM, Jan Snajder wrote: > Dear all, > > I'm trying to implement a simple file-based database. I apparently have > a space leak, but I have no clue where it comes from. > > Here's the file-based database implementation: > http://pastebin.com/QqiqcXFw > > The idea to have a database table in a single textual file. One line > equals one table row. The fields within a row are whitespace separated. > The first field is the key. Because I'd like to work with large files, I > don't want to load the whole file into memory. Instead, I'd like to be > able to fetch the rows on demand, by keys. Thus I first create an index > that links keys to file seeks. I use the readerT to add the index to the > IO monad. > > For testing, I use a dummy table produced as follows: > > import System.IO > import Text.Printf > import Control.Monad > > row = unwords [printf "field%03d" (i::Int) | i <- [1..999]] > > main = do > forM_ [1..250000] $ \i -> > putStrLn $ printf "row%06d %s" (i::Int) row > > This generates a 2.1G textual file, which I store on my disk. > > The testing code: > > import FileDB > import qualified Data.Text as T > import Text.Printf > import Control.Applicative > import Control.Monad > import Control.Monad.Trans > import System.IO > import System.Environment > > main = do > (f:_) <- getArgs > t <- openTable f > runDB t $ do > ks <- getKeys > liftIO $ do > putStrLn . printf "%d keys read" $ length ks > putStrLn "Press any key to continue..." > getChar > forM_ ks $ \k -> do > Just r <- getRow k > liftIO . putStrLn $ printf "Row \"%s\" has %d fields" > (T.unpack k) (length r) > > When I run the test on the 2.1GB file, the whole program consumes 10GB. > > 6GB seem to be allocated after the index is built (just before entering > the forM_ function). The remaining 4GB are allocated while fetching all > the rows. > > I find both things difficult to explain. > > 6GB seems too much for the index. Each key is 9 characters (stored as > Data.Text), and I have 250K such keys in a Data.Map. Should this really > add up to 6GB? > > Also, I have no idea why fetching all the rows, one by one, should > consume any additional memory. Each row is fetched and its length is > computed and printed out. I see no reason for the rows to be retained in > the memory. > > Here's the memory allocation summary: > > > 1,093,931,338,632 bytes allocated in the heap > > 2,225,144,704 bytes copied during GC > > 4,533,898,000 bytes maximum residency (26 sample(s)) > > 3,080,926,336 bytes maximum slop > > 10004 MB total memory in use (0 MB lost due to fragmentation) > > > > Tot time (elapsed) Avg pause Max > pause > > Gen 0 2171739 colls, 0 par 45.29s 45.26s 0.0000s > 0.0030s > > Gen 1 26 colls, 0 par 1.50s 1.53s 0.0589s > 0.7087s > > > > INIT time 0.00s ( 0.00s elapsed) > > MUT time 279.92s (284.85s elapsed) > > GC time 46.80s ( 46.79s elapsed) > > EXIT time 0.68s ( 0.71s elapsed) > > Total time 327.40s (332.35s elapsed) > > > > %GC time 14.3% (14.1% elapsed) > > > > Alloc rate 3,908,073,170 bytes per MUT second > > > > Productivity 85.7% of total user, 84.4% of total elapsed > > > Btw., I don't get the "bytes allocated in the heap" figure, which is > approx. 1000 GB (?). > > I'm obviously doing something wrong here. I'd be thankful for any help. > > Best, > Jan > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Mon May 12 18:32:54 2014 From: bob at redivi.com (Bob Ippolito) Date: Mon, 12 May 2014 11:32:54 -0700 Subject: [Haskell-beginners] Space leak while reading from a file? In-Reply-To: References: <536F5DF7.6000103@fer.hr> Message-ID: I don't know why I ended up looking at an old version of Data.Text but the copy function has been around for a year, since 0.11.3.0. http://hackage.haskell.org/package/text-1.1.1.2/docs/Data-Text.html#copy ? you'll want to use it for this use case. On Mon, May 12, 2014 at 9:54 AM, Bob Ippolito wrote: > I haven't looked closely but I suspect if you use foldM to build your Map > rather than untilM it might consume less memory since you won't be > allocating this big list. > > BUT? the real reason why the memory usage is so much higher than you > expect is because slicing a Data.Text is an O(1) operation that shares the > underlying buffer between the original and the slice. Calling T.words will > ensure that the full original line stays around. You can see this in the > implementation: > > http://hackage.haskell.org/package/text-0.11.2.0/docs/src/Data-Text.html#words > > This behavior is documented somewhat near there in the docs, but I think > it should really be a top-level thing: > http://hackage.haskell.org/package/text-0.11.2.0/docs/Data-Text.html#g:18 > > I don't know what function to use to force the array to be copied, > hopefully there is one! Erlang's binaries work similarly and there is a > copy function for them for exactly this purpose: > http://www.erlang.org/doc/man/binary.html > > -bob > > > > On Sun, May 11, 2014 at 4:24 AM, Jan Snajder wrote: > >> Dear all, >> >> I'm trying to implement a simple file-based database. I apparently have >> a space leak, but I have no clue where it comes from. >> >> Here's the file-based database implementation: >> http://pastebin.com/QqiqcXFw >> >> The idea to have a database table in a single textual file. One line >> equals one table row. The fields within a row are whitespace separated. >> The first field is the key. Because I'd like to work with large files, I >> don't want to load the whole file into memory. Instead, I'd like to be >> able to fetch the rows on demand, by keys. Thus I first create an index >> that links keys to file seeks. I use the readerT to add the index to the >> IO monad. >> >> For testing, I use a dummy table produced as follows: >> >> import System.IO >> import Text.Printf >> import Control.Monad >> >> row = unwords [printf "field%03d" (i::Int) | i <- [1..999]] >> >> main = do >> forM_ [1..250000] $ \i -> >> putStrLn $ printf "row%06d %s" (i::Int) row >> >> This generates a 2.1G textual file, which I store on my disk. >> >> The testing code: >> >> import FileDB >> import qualified Data.Text as T >> import Text.Printf >> import Control.Applicative >> import Control.Monad >> import Control.Monad.Trans >> import System.IO >> import System.Environment >> >> main = do >> (f:_) <- getArgs >> t <- openTable f >> runDB t $ do >> ks <- getKeys >> liftIO $ do >> putStrLn . printf "%d keys read" $ length ks >> putStrLn "Press any key to continue..." >> getChar >> forM_ ks $ \k -> do >> Just r <- getRow k >> liftIO . putStrLn $ printf "Row \"%s\" has %d fields" >> (T.unpack k) (length r) >> >> When I run the test on the 2.1GB file, the whole program consumes 10GB. >> >> 6GB seem to be allocated after the index is built (just before entering >> the forM_ function). The remaining 4GB are allocated while fetching all >> the rows. >> >> I find both things difficult to explain. >> >> 6GB seems too much for the index. Each key is 9 characters (stored as >> Data.Text), and I have 250K such keys in a Data.Map. Should this really >> add up to 6GB? >> >> Also, I have no idea why fetching all the rows, one by one, should >> consume any additional memory. Each row is fetched and its length is >> computed and printed out. I see no reason for the rows to be retained in >> the memory. >> >> Here's the memory allocation summary: >> >> > 1,093,931,338,632 bytes allocated in the heap >> > 2,225,144,704 bytes copied during GC >> > 4,533,898,000 bytes maximum residency (26 sample(s)) >> > 3,080,926,336 bytes maximum slop >> > 10004 MB total memory in use (0 MB lost due to fragmentation) >> > >> > Tot time (elapsed) Avg pause Max >> pause >> > Gen 0 2171739 colls, 0 par 45.29s 45.26s 0.0000s >> 0.0030s >> > Gen 1 26 colls, 0 par 1.50s 1.53s 0.0589s >> 0.7087s >> > >> > INIT time 0.00s ( 0.00s elapsed) >> > MUT time 279.92s (284.85s elapsed) >> > GC time 46.80s ( 46.79s elapsed) >> > EXIT time 0.68s ( 0.71s elapsed) >> > Total time 327.40s (332.35s elapsed) >> > >> > %GC time 14.3% (14.1% elapsed) >> > >> > Alloc rate 3,908,073,170 bytes per MUT second >> > >> > Productivity 85.7% of total user, 84.4% of total elapsed >> >> >> Btw., I don't get the "bytes allocated in the heap" figure, which is >> approx. 1000 GB (?). >> >> I'm obviously doing something wrong here. I'd be thankful for any help. >> >> Best, >> Jan >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Mon May 12 18:34:49 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Mon, 12 May 2014 13:34:49 -0500 Subject: [Haskell-beginners] Space leak while reading from a file? In-Reply-To: References: <536F5DF7.6000103@fer.hr> Message-ID: This Chrome plugin helps avoid the Hackage version problem btw: https://chrome.google.com/webstore/detail/hackage-fu/dnpldbohleinhdgfnhlkofpgkdcfcfmf?hl=en-US On Mon, May 12, 2014 at 1:32 PM, Bob Ippolito wrote: > I don't know why I ended up looking at an old version of Data.Text but the > copy function has been around for a year, since 0.11.3.0. > http://hackage.haskell.org/package/text-1.1.1.2/docs/Data-Text.html#copy? you'll want to use it for this use case. > > > On Mon, May 12, 2014 at 9:54 AM, Bob Ippolito wrote: > >> I haven't looked closely but I suspect if you use foldM to build your Map >> rather than untilM it might consume less memory since you won't be >> allocating this big list. >> >> BUT? the real reason why the memory usage is so much higher than you >> expect is because slicing a Data.Text is an O(1) operation that shares the >> underlying buffer between the original and the slice. Calling T.words will >> ensure that the full original line stays around. You can see this in the >> implementation: >> >> http://hackage.haskell.org/package/text-0.11.2.0/docs/src/Data-Text.html#words >> >> This behavior is documented somewhat near there in the docs, but I think >> it should really be a top-level thing: >> http://hackage.haskell.org/package/text-0.11.2.0/docs/Data-Text.html#g:18 >> >> I don't know what function to use to force the array to be copied, >> hopefully there is one! Erlang's binaries work similarly and there is a >> copy function for them for exactly this purpose: >> http://www.erlang.org/doc/man/binary.html >> >> -bob >> >> >> >> On Sun, May 11, 2014 at 4:24 AM, Jan Snajder wrote: >> >>> Dear all, >>> >>> I'm trying to implement a simple file-based database. I apparently have >>> a space leak, but I have no clue where it comes from. >>> >>> Here's the file-based database implementation: >>> http://pastebin.com/QqiqcXFw >>> >>> The idea to have a database table in a single textual file. One line >>> equals one table row. The fields within a row are whitespace separated. >>> The first field is the key. Because I'd like to work with large files, I >>> don't want to load the whole file into memory. Instead, I'd like to be >>> able to fetch the rows on demand, by keys. Thus I first create an index >>> that links keys to file seeks. I use the readerT to add the index to the >>> IO monad. >>> >>> For testing, I use a dummy table produced as follows: >>> >>> import System.IO >>> import Text.Printf >>> import Control.Monad >>> >>> row = unwords [printf "field%03d" (i::Int) | i <- [1..999]] >>> >>> main = do >>> forM_ [1..250000] $ \i -> >>> putStrLn $ printf "row%06d %s" (i::Int) row >>> >>> This generates a 2.1G textual file, which I store on my disk. >>> >>> The testing code: >>> >>> import FileDB >>> import qualified Data.Text as T >>> import Text.Printf >>> import Control.Applicative >>> import Control.Monad >>> import Control.Monad.Trans >>> import System.IO >>> import System.Environment >>> >>> main = do >>> (f:_) <- getArgs >>> t <- openTable f >>> runDB t $ do >>> ks <- getKeys >>> liftIO $ do >>> putStrLn . printf "%d keys read" $ length ks >>> putStrLn "Press any key to continue..." >>> getChar >>> forM_ ks $ \k -> do >>> Just r <- getRow k >>> liftIO . putStrLn $ printf "Row \"%s\" has %d fields" >>> (T.unpack k) (length r) >>> >>> When I run the test on the 2.1GB file, the whole program consumes 10GB. >>> >>> 6GB seem to be allocated after the index is built (just before entering >>> the forM_ function). The remaining 4GB are allocated while fetching all >>> the rows. >>> >>> I find both things difficult to explain. >>> >>> 6GB seems too much for the index. Each key is 9 characters (stored as >>> Data.Text), and I have 250K such keys in a Data.Map. Should this really >>> add up to 6GB? >>> >>> Also, I have no idea why fetching all the rows, one by one, should >>> consume any additional memory. Each row is fetched and its length is >>> computed and printed out. I see no reason for the rows to be retained in >>> the memory. >>> >>> Here's the memory allocation summary: >>> >>> > 1,093,931,338,632 bytes allocated in the heap >>> > 2,225,144,704 bytes copied during GC >>> > 4,533,898,000 bytes maximum residency (26 sample(s)) >>> > 3,080,926,336 bytes maximum slop >>> > 10004 MB total memory in use (0 MB lost due to >>> fragmentation) >>> > >>> > Tot time (elapsed) Avg pause Max >>> pause >>> > Gen 0 2171739 colls, 0 par 45.29s 45.26s 0.0000s >>> 0.0030s >>> > Gen 1 26 colls, 0 par 1.50s 1.53s 0.0589s >>> 0.7087s >>> > >>> > INIT time 0.00s ( 0.00s elapsed) >>> > MUT time 279.92s (284.85s elapsed) >>> > GC time 46.80s ( 46.79s elapsed) >>> > EXIT time 0.68s ( 0.71s elapsed) >>> > Total time 327.40s (332.35s elapsed) >>> > >>> > %GC time 14.3% (14.1% elapsed) >>> > >>> > Alloc rate 3,908,073,170 bytes per MUT second >>> > >>> > Productivity 85.7% of total user, 84.4% of total elapsed >>> >>> >>> Btw., I don't get the "bytes allocated in the heap" figure, which is >>> approx. 1000 GB (?). >>> >>> I'm obviously doing something wrong here. I'd be thankful for any help. >>> >>> Best, >>> Jan >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Tue May 13 23:17:52 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Wed, 14 May 2014 01:17:52 +0200 Subject: [Haskell-beginners] Addition of "Float" and "Int". In-Reply-To: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> References: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> Message-ID: <5372A820.1010504@fuuzetsu.co.uk> On 05/12/2014 05:44 PM, Venu Chakravorty wrote: > > > Hello everyone, > I am just starting with Haskell so please bear with me. > > > Here's my question: > > > Consider the below definition / output: > > > Prelude> :t (+) > (+) :: (Num a) => a -> a -> a > > > What I understand from the above is that "+" is a function that takes two args > which are types of anything that IS-AN instance of "Num" (Int, Integer, Float, Double) > and returns an instance of "Num". > Hence this works fine: > Prelude> 4.3 + 2 > 6.3 > > > But I can't understand why this doesn't work: > Prelude> 4.3 + 4 :: Int > > > :1:0: > No instance for (Fractional Int) > arising from the literal `4.3' at :1:0-2 > Possible fix: add an instance declaration for (Fractional Int) > In the first argument of `(+)', namely `4.3' > In the expression: 4.3 + 4 :: Int > In the definition of `it': it = 4.3 + 4 :: Int > > > I expected that the second addition would work as both "Float" and "Int" are > instances of "Num". Is it that since both the formal args are defined as "a" they > have to be exactly the same instances? Had "+" been defined something like: > (+) :: (Num a, Num b) => a -> b -> a > my second addition would have worked? > Just to follow up on the part Brandon didn't explain, no, (Num a, Num b) => a -> b -> a would not work either: there is not enough information there to do anything sensible. That is, we don't know how to turn any arbitrary Num into any other arbitrary Num. What if it was (4 :: Int) + (7.5 :: Double). The type signature would match but the result is fractional and we promised to return Int. This is the reason why it's Num a => a -> a -> a; we only know how to add things of the same type together. If we want more, we need to have more information. > Please let me know what I am missing. > > > Regards, > Venu Chakravorty. > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Mateusz K. From hsyl20 at gmail.com Wed May 14 00:02:38 2014 From: hsyl20 at gmail.com (Sylvain Henry) Date: Wed, 14 May 2014 02:02:38 +0200 Subject: [Haskell-beginners] Addition of "Float" and "Int". In-Reply-To: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> References: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> Message-ID: >Is it that since both the formal args are defined as "a" they have to be exactly the same instances? Exactly. >Had "+" been defined something like: (+) :: (Num a, Num b) => a -> b -> a > my second addition would have worked? Yes except that you cannot write this function given the definition of Num http://hackage.haskell.org/package/base-4.7.0.0/docs/Prelude.html#t:Num If you want your second example to work, you have to explicitly convert the floating-point value into an Int (using floor, ceiling or round for instance). -Sylvain 2014-05-12 17:44 GMT+02:00 Venu Chakravorty : > Hello everyone, > I am just starting with Haskell so please bear with me. > > Here's my question: > > Consider the below definition / output: > > Prelude> :t (+) > (+) :: (Num a) => a -> a -> a > > What I understand from the above is that "+" is a function that takes > two args > which are types of anything that IS-AN instance of "Num" (Int, Integer, > Float, Double) > and returns an instance of "Num". > Hence this works fine: > Prelude> 4.3 + 2 > 6.3 > > But I can't understand why this doesn't work: > Prelude> 4.3 + 4 :: Int > > :1:0: > No instance for (Fractional Int) > arising from the literal `4.3' at :1:0-2 > Possible fix: add an instance declaration for (Fractional Int) > In the first argument of `(+)', namely `4.3' > In the expression: 4.3 + 4 :: Int > In the definition of `it': it = 4.3 + 4 :: Int > > I expected that the second addition would work as both "Float" and "Int" > are > instances of "Num". Is it that since both the formal args are defined as > "a" they > have to be exactly the same instances? Had "+" been defined something > like: > (+) :: (Num a, Num b) => a -> b -> a > my second addition would have worked? > > Please let me know what I am missing. > > Regards, > Venu Chakravorty. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From matthewjwilliams101 at gmail.com Wed May 14 09:07:55 2014 From: matthewjwilliams101 at gmail.com (MJ Williams) Date: Wed, 14 May 2014 10:07:55 +0100 Subject: [Haskell-beginners] Addition of "Float" and "Int". In-Reply-To: <5372A820.1010504@fuuzetsu.co.uk> References: <8D13C102BA56961-DEC-20BA7@webmail-m243.sysops.aol.com> <5372A820.1010504@fuuzetsu.co.uk> Message-ID: <5373326b.4a51b40a.2b3e.4c11@mx.google.com> I'm sure someone else on this list will explain it much more eloquently than I, but for now, here goes: What you are effectively forcing haskell to do is to return a result that hasn't been defined. You can't force a result of type `Int' where `+' has not been defined to return such a result for the sum of a Float(s) and integers(s). Of course, in languages such as C you can use casting for the purpose, but we are talking about very different, in fact, entirely different programming paradigms. hth, Matthew At 00:17 14/05/2014, you wrote: >On 05/12/2014 05:44 PM, Venu Chakravorty wrote: > > > > > > Hello everyone, > > I am just starting with Haskell so please bear with me. > > > > > > Here's my question: > > > > > > Consider the below definition / output: > > > > > > Prelude> :t (+) > > (+) :: (Num a) => a -> a -> a > > > > > > What I understand from the above is that "+" is a function > that takes two args > > which are types of anything that IS-AN instance of "Num" (Int, > Integer, Float, Double) > > and returns an instance of "Num". > > Hence this works fine: > > Prelude> 4.3 + 2 > > 6.3 > > > > > > But I can't understand why this doesn't work: > > Prelude> 4.3 + 4 :: Int > > > > > > :1:0: > > No instance for (Fractional Int) > > arising from the literal `4.3' at :1:0-2 > > Possible fix: add an instance declaration for (Fractional Int) > > In the first argument of `(+)', namely `4.3' > > In the expression: 4.3 + 4 :: Int > > In the definition of `it': it = 4.3 + 4 :: Int > > > > > > I expected that the second addition would work as both > "Float" and "Int" are > > instances of "Num". Is it that since both the formal args are > defined as "a" they > > have to be exactly the same instances? Had "+" been defined > something like: > > (+) :: (Num a, Num b) => a -> b -> a > > my second addition would have worked? > > > >Just to follow up on the part Brandon didn't explain, no, (Num a, Num b) >=> a -> b -> a would not work either: there is not enough information >there to do anything sensible. That is, we don't know how to turn any >arbitrary Num into any other arbitrary Num. > >What if it was (4 :: Int) + (7.5 :: Double). The type signature would >match but the result is fractional and we promised to return Int. > >This is the reason why it's Num a => a -> a -> a; we only know how to >add things of the same type together. If we want more, we need to have >more information. > > > Please let me know what I am missing. > > > > > > Regards, > > Venu Chakravorty. > > > > > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > >-- >Mateusz K. >_______________________________________________ >Beginners mailing list >Beginners at haskell.org >http://www.haskell.org/mailman/listinfo/beginners From fuuzetsu at fuuzetsu.co.uk Wed May 14 16:32:31 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Wed, 14 May 2014 18:32:31 +0200 Subject: [Haskell-beginners] Learning Haskell with the help of trees In-Reply-To: <536E15F4.2070807@bazzle.me> References: <536E15F4.2070807@bazzle.me> Message-ID: <53739A9F.3030600@fuuzetsu.co.uk> On 05/10/2014 02:05 PM, Michal Kawalec wrote: > Hi, > > I am beginning to learn Haskell, and I decided to try to implement a > tree. Its code is available at > https://gist.github.com/anonymous/dd3eaa8bc36025d7751c > > I would need some help with implementing the delete element procedure. I > have a feeling I am doing something very unhaskelly there. Also, when > deleting a node having two children, how do I run two actions (swap the > values and delete the successor)? > > Also, I would be delighted if you could take a look on the style and > similar aspects, as they are best addressed at the beginning. > > > > Thanks, > Michal > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > I started a project which aims to implement paredit-like editing functionality which effectively involves AST manipulation. The final goal is to use it in Yi but that's far off and no Yi code should be in the library itself. There is very little code as I did not have much time for it, but perhaps it something you might be interested in as practice. See [1]. It basically involves implementing the functionality seen at [2] so that editors in the future can bind keys to the functions and manipulate the AST that way. Let me know if you're interested in working on this. [1]: https://github.com/Fuuzetsu/hs-paredit [2]: http://mumble.net/~campbell/emacs/paredit.html -- Mateusz K. From fuuzetsu at fuuzetsu.co.uk Wed May 14 19:06:55 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Wed, 14 May 2014 21:06:55 +0200 Subject: [Haskell-beginners] Problem with installing SDL In-Reply-To: <73665494.gc7JfWPL4j@julianspc> References: <7141757.TlrQdfbG5o@julianspc> <73665494.gc7JfWPL4j@julianspc> Message-ID: <5373BECF.7030500@fuuzetsu.co.uk> On 05/09/2014 10:36 AM, Julian Drube wrote: > Thank you, > > In the file `Graphics/UI/SDL/Events.hsc` I changed the line > > import Data.Typeable (Typeable(typeOf),TypeRep) > > to just > > import Data.Typeable > > and it compiles without problems now. > > Julian > > On Thursday 08 May 2014 17:43:41 Patrick Wheeler wrote: >> The error is saying that it can not find `typeOf` in the `Typeable` class. >> This is because that function is not longer part of the `Typeable` in >> ghc7.8 and moving forward. Error like this are still be sorted out for ghc >> 7.8. You are on a bleeding edge version of ghc so packaging problems like >> this are expected. Package stability is usually marked by when the Haskell >> Platform updates to the new ghc version. >> >> So if you want additional stability HP is where it is at. If you want to >> live and the bleeding edge then you can download sdl with cabal unpack and >> see if you can not fix the problem for yourself. >> >> Here is a link to the `Typeable` module in base: >> http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.htmlOf >> >> There is replacement function for backward compatibility. >> http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Typeable.html#v:ty >> peOf >> >> If you fixed the import statement for all of the sdl modules, you might fix >> the incompatibility with the new Typeable. There will probably be a few >> other issues that need solving after this one. I have updated several >> packages on a case by case basis when I need them to new ghc version and >> has been a lesson in understanding ghc errors and fixing them. If you have >> the time I recommend giving it a shot. >> >> Patrick > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Please send a patch or a pull request to the maintainer. -- Mateusz K. From omega2youall at gmail.com Thu May 15 13:46:32 2014 From: omega2youall at gmail.com (Eduard Nicodei) Date: Thu, 15 May 2014 14:46:32 +0100 Subject: [Haskell-beginners] "99 Haskell Problems" solutions page formatting Message-ID: Hello everybody! I apologize if I'm sending this email to the wrong mailing list / group. If so, does anybody know where I should be asking wiki-related questions? I think Haskell is a wonderful language and I would like to help the community in whatever way I can. Currently I was looking at the 99 Haskell problems from the wiki, trying to solve them in as many ways as possible: http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell_Problems I have noticed there is a note saying that some of the solutions could do with reformatting. Indeed I have noticed that the solution pages aren't very consistent. I edited "the solutions for problem nr. 4" page: http://www.haskell.org/haskellwiki/99_questions/Solutions/4 I added headlines, separated the alternative solutions into logical groups and there was one (the Prelude implementation) which was mentioned twice. Could somebody please have a look over the changes and confirm I didn't do anything wrong please? I have read the wiki editing guide, but I am unsure if this a good format to use for the "Solutions" pages. Many thanks! Eduard Nicodei -------------- next part -------------- An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Thu May 15 15:01:44 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Thu, 15 May 2014 17:01:44 +0200 Subject: [Haskell-beginners] "99 Haskell Problems" solutions page formatting In-Reply-To: References: Message-ID: <5374D6D8.9070809@fuuzetsu.co.uk> On 05/15/2014 03:46 PM, Eduard Nicodei wrote: > Hello everybody! > > I apologize if I'm sending this email to the wrong mailing list / group. If > so, does anybody know where I should be asking wiki-related questions? > I think Haskell is a wonderful language and I would like to help the > community in whatever way I can. > > Currently I was looking at the 99 Haskell problems from the wiki, trying to > solve them in as many ways as possible: > http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell_Problems > > I have noticed there is a note saying that some of the solutions could do > with reformatting. Indeed I have noticed that the solution pages aren't > very consistent. I edited "the solutions for problem nr. 4" page: > http://www.haskell.org/haskellwiki/99_questions/Solutions/4 > I added headlines, separated the alternative solutions into logical groups > and there was one (the Prelude implementation) which was mentioned twice. > > Could somebody please have a look over the changes and confirm I didn't do > anything wrong please? I have read the wiki editing guide, but I am unsure > if this a good format to use for the "Solutions" pages. > > Many thanks! > Eduard Nicodei > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Looks good to me. There is no strict standard. If someone has complaints, they'll either let it known or edit the page. -- Mateusz K. From jan.snajder at fer.hr Thu May 15 18:17:45 2014 From: jan.snajder at fer.hr (Jan Snajder) Date: Thu, 15 May 2014 20:17:45 +0200 Subject: [Haskell-beginners] Space leak while reading from a file? In-Reply-To: References: Message-ID: <537504C9.7020406@fer.hr> From: Bob Ippolito > I don't know why I ended up looking at an old version of Data.Text but the > copy function has been around for a year, since 0.11.3.0. > http://hackage.haskell.org/package/text-1.1.1.2/docs/Data-Text.html#copy ? > you'll want to use it for this use case. Great, thanks, that solved the problem! Best, Jan From timmelzer at gmail.com Fri May 16 15:53:31 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Fri, 16 May 2014 17:53:31 +0200 Subject: [Haskell-beginners] parallelizing a function for generating prime numbers Message-ID: Hi there! I am trying to enhence the speed of my Project Euler solutions? My original function is this: ```haskell problem10' :: Integer problem10' = sum $ takeWhile (<=2000000) primes where primes = filter isPrime possiblePrimes isPrime n = n == head (primeFactors n) possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- [1..] ]) primeFactors m = pf 2 m pf n m | n*n > m = [m] | n*n == m = [n,n] | m `mod` n == 0 = n:pf n (m `div` n) | otherwise = pf (n+1) m ``` Even if the generation of primes is relatively slow and could be much better, I want to focus on parallelization, so I tried the following: ```haskell parFilter :: (a -> Bool) -> [a] -> [a] parFilter _ [] = [] parFilter f (x:xs) = let px = f x pxs = parFilter f xs in par px $ par pxs $ case px of True -> x : pxs False -> pxs problem10' :: Integer problem10' = sum $ takeWhile (<=2000000) primes where primes = parFilter isPrime possiblePrimes isPrime n = n == head (primeFactors n) possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- [1..] ]) primeFactors m = pf 2 m pf n m | n*n > m = [m] | n*n == m = [n,n] | m `mod` n == 0 = n:pf n (m `div` n) | otherwise = pf (n+1) m ``` This approach was about half as slow as the first solution (~15 seconds old, ~30 the new one!). Trying to use `Control.Parallel.Strategies.evalList` for `possiblePrimes` resulted in a huge waste of memory, since it forced to generate an endless list, and does not stop? Trying the same for `primeFactors` did not gain any speed, but was not much slower at least, but I did not expect much, since I look at its head only? Only thing I could imagine to parallelize any further would be the takeWhile, but then I don't get how I should do it? Any ideas how to do it? TIA Norbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From edwards.benj at gmail.com Fri May 16 16:33:14 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Fri, 16 May 2014 16:33:14 +0000 Subject: [Haskell-beginners] parallelizing a function for generating prime numbers References: Message-ID: Given the linear dependencies in prime number generation, shy of using a probabilistic sieving method, I'm not sure that it's possible to hope for any kind of parallel number generation. All you are going to do is for yourself to eat the cost of synchronisation for no gain. Ben On Fri May 16 2014 at 16:53:38, Norbert Melzer wrote: > Hi there! > > I am trying to enhence the speed of my Project Euler solutions? > > My original function is this: > > ```haskell > problem10' :: Integer > problem10' = sum $ takeWhile (<=2000000) primes > where > primes = filter isPrime possiblePrimes > isPrime n = n == head (primeFactors n) > possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- [1..] > ]) > primeFactors m = pf 2 m > pf n m | n*n > m = [m] > | n*n == m = [n,n] > | m `mod` n == 0 = n:pf n (m `div` n) > | otherwise = pf (n+1) m > ``` > > Even if the generation of primes is relatively slow and could be much > better, I want to focus on parallelization, so I tried the following: > > ```haskell > parFilter :: (a -> Bool) -> [a] -> [a] > parFilter _ [] = [] > parFilter f (x:xs) = > let px = f x > pxs = parFilter f xs > in par px $ par pxs $ case px of True -> x : pxs > False -> pxs > > problem10' :: Integer > problem10' = sum $ takeWhile (<=2000000) primes > where > primes = parFilter isPrime possiblePrimes > isPrime n = n == head (primeFactors n) > possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- [1..] > ]) > primeFactors m = pf 2 m > pf n m | n*n > m = [m] > | n*n == m = [n,n] > | m `mod` n == 0 = n:pf n (m `div` n) > | otherwise = pf (n+1) m > ``` > > This approach was about half as slow as the first solution (~15 seconds > old, ~30 the new one!). > > Trying to use `Control.Parallel.Strategies.evalList` for `possiblePrimes` > resulted in a huge waste of memory, since it forced to generate an endless > list, and does not stop? > > Trying the same for `primeFactors` did not gain any speed, but was not > much slower at least, but I did not expect much, since I look at its head > only? > > Only thing I could imagine to parallelize any further would be the > takeWhile, but then I don't get how I should do it? > > Any ideas how to do it? > > TIA > Norbert > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Fri May 16 20:13:09 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Fri, 16 May 2014 22:13:09 +0200 Subject: [Haskell-beginners] parallelizing a function for generating prime numbers In-Reply-To: References: Message-ID: I had some fears, that there will be answers like this ;) The problem with improving the generation itself is, that I don't understand the faster implementations that I found (namely the implementation of `Data.Numbers.Primes` in the `primes`-package and some other Wheel-Sieves). And for the Project-Euler-Problems I only use code that I have created myself or at least I have a small idea how it works if it is from a package? 2014-05-16 18:33 GMT+02:00 Benjamin Edwards : > Given the linear dependencies in prime number generation, shy of using a > probabilistic sieving method, I'm not sure that it's possible to hope for > any kind of parallel number generation. All you are going to do is for > yourself to eat the cost of synchronisation for no gain. > > Ben > > On Fri May 16 2014 at 16:53:38, Norbert Melzer > wrote: > >> Hi there! >> >> I am trying to enhence the speed of my Project Euler solutions? >> >> My original function is this: >> >> ```haskell >> problem10' :: Integer >> problem10' = sum $ takeWhile (<=2000000) primes >> where >> primes = filter isPrime possiblePrimes >> isPrime n = n == head (primeFactors n) >> possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- >> [1..] ]) >> primeFactors m = pf 2 m >> pf n m | n*n > m = [m] >> | n*n == m = [n,n] >> | m `mod` n == 0 = n:pf n (m `div` n) >> | otherwise = pf (n+1) m >> ``` >> >> Even if the generation of primes is relatively slow and could be much >> better, I want to focus on parallelization, so I tried the following: >> >> ```haskell >> parFilter :: (a -> Bool) -> [a] -> [a] >> parFilter _ [] = [] >> parFilter f (x:xs) = >> let px = f x >> pxs = parFilter f xs >> in par px $ par pxs $ case px of True -> x : pxs >> False -> pxs >> >> problem10' :: Integer >> problem10' = sum $ takeWhile (<=2000000) primes >> where >> primes = parFilter isPrime possiblePrimes >> isPrime n = n == head (primeFactors n) >> possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- >> [1..] ]) >> primeFactors m = pf 2 m >> pf n m | n*n > m = [m] >> | n*n == m = [n,n] >> | m `mod` n == 0 = n:pf n (m `div` n) >> | otherwise = pf (n+1) m >> ``` >> >> This approach was about half as slow as the first solution (~15 seconds >> old, ~30 the new one!). >> >> Trying to use `Control.Parallel.Strategies.evalList` for `possiblePrimes` >> resulted in a huge waste of memory, since it forced to generate an endless >> list, and does not stop? >> >> Trying the same for `primeFactors` did not gain any speed, but was not >> much slower at least, but I did not expect much, since I look at its head >> only? >> >> Only thing I could imagine to parallelize any further would be the >> takeWhile, but then I don't get how I should do it? >> >> Any ideas how to do it? >> >> TIA >> Norbert >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Sat May 17 12:09:59 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Sat, 17 May 2014 14:09:59 +0200 Subject: [Haskell-beginners] parallelizing a function for generating prime numbers In-Reply-To: References: Message-ID: <53775197.7090309@fuuzetsu.co.uk> On 05/16/2014 10:13 PM, Norbert Melzer wrote: > I had some fears, that there will be answers like this ;) > > The problem with improving the generation itself is, that I don't > understand the faster implementations that I found (namely the > implementation of `Data.Numbers.Primes` in the `primes`-package and some > other Wheel-Sieves). > > And for the Project-Euler-Problems I only use code that I have created > myself or at least I have a small idea how it works if it is from a package? > Euler is a mathematics (mostly number theory) exercise, not a programming one, so it's understandable that in some cases the problems and some of their solutions are not suited for parallelism. It's not of much benefit to try and parallelise an inherently linear solution. > 2014-05-16 18:33 GMT+02:00 Benjamin Edwards : > >> Given the linear dependencies in prime number generation, shy of using a >> probabilistic sieving method, I'm not sure that it's possible to hope for >> any kind of parallel number generation. All you are going to do is for >> yourself to eat the cost of synchronisation for no gain. >> >> Ben >> >> On Fri May 16 2014 at 16:53:38, Norbert Melzer >> wrote: >> >>> Hi there! >>> >>> I am trying to enhence the speed of my Project Euler solutions? >>> >>> My original function is this: >>> >>> ```haskell >>> problem10' :: Integer >>> problem10' = sum $ takeWhile (<=2000000) primes >>> where >>> primes = filter isPrime possiblePrimes >>> isPrime n = n == head (primeFactors n) >>> possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- >>> [1..] ]) >>> primeFactors m = pf 2 m >>> pf n m | n*n > m = [m] >>> | n*n == m = [n,n] >>> | m `mod` n == 0 = n:pf n (m `div` n) >>> | otherwise = pf (n+1) m >>> ``` >>> >>> Even if the generation of primes is relatively slow and could be much >>> better, I want to focus on parallelization, so I tried the following: >>> >>> ```haskell >>> parFilter :: (a -> Bool) -> [a] -> [a] >>> parFilter _ [] = [] >>> parFilter f (x:xs) = >>> let px = f x >>> pxs = parFilter f xs >>> in par px $ par pxs $ case px of True -> x : pxs >>> False -> pxs >>> >>> problem10' :: Integer >>> problem10' = sum $ takeWhile (<=2000000) primes >>> where >>> primes = parFilter isPrime possiblePrimes >>> isPrime n = n == head (primeFactors n) >>> possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- >>> [1..] ]) >>> primeFactors m = pf 2 m >>> pf n m | n*n > m = [m] >>> | n*n == m = [n,n] >>> | m `mod` n == 0 = n:pf n (m `div` n) >>> | otherwise = pf (n+1) m >>> ``` >>> >>> This approach was about half as slow as the first solution (~15 seconds >>> old, ~30 the new one!). >>> >>> Trying to use `Control.Parallel.Strategies.evalList` for `possiblePrimes` >>> resulted in a huge waste of memory, since it forced to generate an endless >>> list, and does not stop? >>> >>> Trying the same for `primeFactors` did not gain any speed, but was not >>> much slower at least, but I did not expect much, since I look at its head >>> only? >>> >>> Only thing I could imagine to parallelize any further would be the >>> takeWhile, but then I don't get how I should do it? >>> >>> Any ideas how to do it? >>> >>> TIA >>> Norbert >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Mateusz K. From mukeshtiwari.iiitm at gmail.com Sun May 18 06:51:52 2014 From: mukeshtiwari.iiitm at gmail.com (mukesh tiwari) Date: Sun, 18 May 2014 12:21:52 +0530 Subject: [Haskell-beginners] parallelizing a function for generating prime numbers In-Reply-To: References: Message-ID: You can use Rabin-Miller[1] primality testing. The idea is, divide the range 1 to 2000000 in chunk of 10000 numbers and evaluate the all the chunks in parallel. import Data.Bits import Control.Parallel.Strategies powM :: Integer -> Integer -> Integer -> Integer powM a d n | d == 0 = 1 | d == 1 = mod a n | otherwise = mod q n where p = powM ( mod ( a^2 ) n ) ( shiftR d 1 ) n q = if (.&.) d 1 == 1 then mod ( a * p ) n else p calSd :: Integer -> ( Integer , Integer ) calSd n = ( s , d ) where s = until ( \x -> testBit ( n - 1) ( fromIntegral x ) ) ( +1 ) 0 d = div ( n - 1 ) ( shiftL 1 ( fromIntegral s ) ) rabinMiller::Integer->Integer->Integer->Integer-> Bool rabinMiller n s d a | n == a = True | otherwise = case powM a d n of 1 -> True x -> any ( == pred n ) . take ( fromIntegral s ) . iterate (\e -> mod ( e^2 ) n ) $ x isPrime::Integer-> Bool isPrime n | n <= 1 = False | n == 2 = True | even n = False | otherwise = all ( == True ) . map ( rabinMiller n s d ) $ [ 2 , 3 , 5 , 7 , 11 , 13 , 17 ] where ( s , d ) = calSd n primeRange :: Integer -> Integer -> [ Bool ] primeRange m n = ( map isPrime [ m .. n ] ) `using` parListChunk 10000 rdeepseq sum' :: Integer -> Integer -> Integer sum' m n = sum . map ( \( x, y ) -> if y then x else 0 ) . zip [ m .. n ] . primeRange m $ n main = print ( sum' 1 2000000 ) Mukeshs-MacBook-Pro:Puzzles mukeshtiwari$ time ./Proj10 +RTS -N2 142913828922 real 0m6.301s user 0m11.937s sys 0m0.609s Mukeshs-MacBook-Pro:Puzzles mukeshtiwari$ time ./Proj10 +RTS -N1 142913828922 real 0m8.202s user 0m8.026s sys 0m0.174s -Mukesh [1] http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test On Fri, May 16, 2014 at 9:23 PM, Norbert Melzer wrote: > Hi there! > > I am trying to enhence the speed of my Project Euler solutions? > > My original function is this: > > ```haskell > problem10' :: Integer > problem10' = sum $ takeWhile (<=2000000) primes > where > primes = filter isPrime possiblePrimes > isPrime n = n == head (primeFactors n) > possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- [1..] > ]) > primeFactors m = pf 2 m > pf n m | n*n > m = [m] > | n*n == m = [n,n] > | m `mod` n == 0 = n:pf n (m `div` n) > | otherwise = pf (n+1) m > ``` > > Even if the generation of primes is relatively slow and could be much > better, I want to focus on parallelization, so I tried the following: > > ```haskell > parFilter :: (a -> Bool) -> [a] -> [a] > parFilter _ [] = [] > parFilter f (x:xs) = > let px = f x > pxs = parFilter f xs > in par px $ par pxs $ case px of True -> x : pxs > False -> pxs > > problem10' :: Integer > problem10' = sum $ takeWhile (<=2000000) primes > where > primes = parFilter isPrime possiblePrimes > isPrime n = n == head (primeFactors n) > possiblePrimes = (2:3:concat [ [6*pp-1, 6*pp+1] | pp <- [1..] > ]) > primeFactors m = pf 2 m > pf n m | n*n > m = [m] > | n*n == m = [n,n] > | m `mod` n == 0 = n:pf n (m `div` n) > | otherwise = pf (n+1) m > ``` > > This approach was about half as slow as the first solution (~15 seconds > old, ~30 the new one!). > > Trying to use `Control.Parallel.Strategies.evalList` for `possiblePrimes` > resulted in a huge waste of memory, since it forced to generate an endless > list, and does not stop? > > Trying the same for `primeFactors` did not gain any speed, but was not > much slower at least, but I did not expect much, since I look at its head > only? > > Only thing I could imagine to parallelize any further would be the > takeWhile, but then I don't get how I should do it? > > Any ideas how to do it? > > TIA > Norbert > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From msherman77 at yahoo.com Tue May 20 19:10:20 2014 From: msherman77 at yahoo.com (Michael S) Date: Tue, 20 May 2014 12:10:20 -0700 (PDT) Subject: [Haskell-beginners] haskell on OpenBSD 5.5 Message-ID: <1400613020.10632.YahooMailNeo@web163102.mail.bf1.yahoo.com> Good day all, I installed OpenBSD 5.5 in order to learn haskell. There are a few glitches however. I installed the haskell-platform meta-package and the issues I noticed are: - hs-vector wouldn't install - ghci wouldn't start, the message: unable to load package `integer-gmp' Has anyone experienced the same issue? Is this happening on OpenBSD 5.4? Is this OpenBSD specific? Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Wed May 21 02:12:59 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Tue, 20 May 2014 20:12:59 -0600 Subject: [Haskell-beginners] A better way to "integrate" Message-ID: <537C0BAB.5050700@ucdavis.edu> Awesome haskellers, I am coding up a little function that aggregates "ask orders" in a currency exchange. Another way to look at it, is that the function takes as input a histogram or fdf (in list format) and outputs the cumulative distribution cdf (also in list format). So we are kind of "integrating" the input list. When given a list of asks in order of increasing price, the function returns a list of points in the graph of the total supply curve. Here's an example: asks: returned list: [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] the returned list gives us the total supply curve (price = y-axis, quantity/volume = x-axis, so the order is flipped) Summarizing * We're adding up the volumes. The last volume on the list is the total volume available for sale. * We calculate the total amount to be paid to buy the current volume (for each item in the list). I have written up a simple function to do this: aggregate :: Num a => [(a,a)] -> [(a,a)] aggregate xs = aggregate' 0 0 xs aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] aggregate' _ _ [] = [] aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y accY' = accY + y in (accX',accY') : aggregate' accX' accY' ls main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] However, this does not look very good to me and it feels like I'm reinventing the wheel. Question: Is there a better Haskell way to do this? I'm really anal about making it easy to read. Thanks! Dimitri From ngzhian at gmail.com Wed May 21 02:58:22 2014 From: ngzhian at gmail.com (Zhi An Ng) Date: Wed, 21 May 2014 10:58:22 +0800 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <537C0BAB.5050700@ucdavis.edu> References: <537C0BAB.5050700@ucdavis.edu> Message-ID: ?? Travesals can usually be implemented using folds, since your list is increasing, I would think to use a foldl, this is my attempt: foldl :: (a -> b -> a) -> a -> [b] -> a prices = [(42, 0.5), (50, 1), (55, 0.2)] agg = foldl accum [] where accum [] (price, volume) = [(price * volume, volume)] accum xs (price, volume) = let total = last xs total_price = fst total total_volume = snd total cost = price * volume in xs ++ [(total_price + cost, total_volume + volume)] main = print $ agg prices Best, Zhi An On Wed, May 21, 2014 at 10:12 AM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > Awesome haskellers, > > I am coding up a little function that aggregates "ask orders" in a > currency exchange. > Another way to look at it, is that the function takes as input a histogram > or fdf (in list format) and outputs the cumulative distribution cdf (also > in list format). So we are kind of "integrating" the input list. > > When given a list of asks in order of increasing price, the function > returns a list of points in the graph of the total supply curve. > > Here's an example: > > asks: returned list: > > [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), > (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), > (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] > > the returned list gives us the total supply curve (price = y-axis, > quantity/volume = x-axis, so the order is flipped) > > Summarizing > > * We're adding up the volumes. The last volume on the list is the total > volume available for sale. > * We calculate the total amount to be paid to buy the current volume (for > each item in the list). > > I have written up a simple function to do this: > > aggregate :: Num a => [(a,a)] -> [(a,a)] > aggregate xs = aggregate' 0 0 xs > > aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] > aggregate' _ _ [] = [] > aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y > accY' = accY + y > > in (accX',accY') : aggregate' accX' > accY' ls > > > main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] > > However, this does not look very good to me and it feels like I'm > reinventing the wheel. > > Question: Is there a better Haskell way to do this? I'm really anal about > making it easy to read. > > Thanks! > > Dimitri > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed May 21 03:07:46 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 20 May 2014 23:07:46 -0400 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <537C0BAB.5050700@ucdavis.edu> References: <537C0BAB.5050700@ucdavis.edu> Message-ID: Though I'm late to the party I might as well post what I have. Anyways yeah foldr/foldl is the answer when you find yourself iterating over a list with an accumulator. aggregate :: [(Price,Volume)] -> [(Price, Volume)] aggregate = reverse . snd . foldl' proc ((0,0),[]) where proc ((!totalp,!totalv),sofar) (Price !p,Volume !v) = let adjustedprice = round (fromIntegral p * v) in ((adjustedprice+totalp,v+totalv), ((Price $ adjustedprice + totalp, Volume $ v+totalv) : sofar)) Just make sure you use foldl' instead of foldl for performance reasons. And also try to prepend to lists whenever possible and then reverse if you need to. On Tue, May 20, 2014 at 10:12 PM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > Awesome haskellers, > > I am coding up a little function that aggregates "ask orders" in a > currency exchange. > Another way to look at it, is that the function takes as input a histogram > or fdf (in list format) and outputs the cumulative distribution cdf (also > in list format). So we are kind of "integrating" the input list. > > When given a list of asks in order of increasing price, the function > returns a list of points in the graph of the total supply curve. > > Here's an example: > > asks: returned list: > > [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), > (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), > (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] > > the returned list gives us the total supply curve (price = y-axis, > quantity/volume = x-axis, so the order is flipped) > > Summarizing > > * We're adding up the volumes. The last volume on the list is the total > volume available for sale. > * We calculate the total amount to be paid to buy the current volume (for > each item in the list). > > I have written up a simple function to do this: > > aggregate :: Num a => [(a,a)] -> [(a,a)] > aggregate xs = aggregate' 0 0 xs > > aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] > aggregate' _ _ [] = [] > aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y > accY' = accY + y > > in (accX',accY') : aggregate' accX' > accY' ls > > > main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] > > However, this does not look very good to me and it feels like I'm > reinventing the wheel. > > Question: Is there a better Haskell way to do this? I'm really anal about > making it easy to read. > > Thanks! > > Dimitri > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hckjsnzf at gmail.com Wed May 21 05:42:52 2014 From: hckjsnzf at gmail.com (yang.zhao) Date: Wed, 21 May 2014 13:42:52 +0800 Subject: [Haskell-beginners] Haskell network sample Message-ID: hi guys, I'm newbie, just begin to learn Haskell. now i write a very simple server and client . Server: import Control.Concurrent import System.IO import Network port :: Int port = 1234 main :: IO () main = withSocketsDo $ do s <- listenOn $ PortNumber $ fromIntegral port (h, _, _) <- accept s sline <- hGetLine h hPutStrLn h sline putStrLn $ "send "++sline threadDelay 1000000 hClose h sClose s Client : import System.IO import Network port :: Int port = 1234 main :: IO () main = withSocketsDo $ do h <- connectTo "localhost" $ PortNumber $ fromIntegral port hPutStrLn h "hello" bs <- hGetContents h putStrLn bs hClose h And, it doesn't work now . I run ./serv , then run ./cli , they will block all the time. but, when i run ./serv, and telnet localhost 1234 in another terminal, it works fine. so i don't know what's the wrong with my code. anybody can tell me about my problem? os is Debian 7, haskell-platform 2012.2.0.0 thanks a lot!!! -- K.I.S.S. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Wed May 21 05:47:51 2014 From: bob at redivi.com (Bob Ippolito) Date: Tue, 20 May 2014 22:47:51 -0700 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: How precisely are you trying to run the client? Are you typing it in to the same terminal? If so, then the client is never actually started, because when you type ./cli the input is going to ./serv and not the shell. Try running the client in a separate terminal. It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: > hi guys, > I'm newbie, just begin to learn Haskell. > now i write a very simple server and client . > > Server: > import Control.Concurrent > import System.IO > import Network > > port :: Int > port = 1234 > > main :: IO () > main = withSocketsDo $ do > s <- listenOn $ PortNumber $ fromIntegral port > (h, _, _) <- accept s > > sline <- hGetLine h > hPutStrLn h sline > putStrLn $ "send "++sline > threadDelay 1000000 > > hClose h > sClose s > > Client : > import System.IO > import Network > > port :: Int > port = 1234 > > main :: IO () > main = withSocketsDo $ do > h <- connectTo "localhost" $ PortNumber $ fromIntegral port > hPutStrLn h "hello" > bs <- hGetContents h > putStrLn bs > hClose h > > > And, it doesn't work now . I run ./serv , then run ./cli , they will block > all the time. > but, when i run ./serv, and telnet localhost 1234 in another terminal, it > works fine. > so i don't know what's the wrong with my code. > anybody can tell me about my problem? > > os is Debian 7, haskell-platform 2012.2.0.0 > > thanks a lot!!! > -- > K.I.S.S. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hckjsnzf at gmail.com Wed May 21 05:51:41 2014 From: hckjsnzf at gmail.com (yang.zhao) Date: Wed, 21 May 2014 13:51:41 +0800 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: thanks for your replay. i run the two program in two different teriminal for sure. it works for you? but why can't run well on my computer. make me creazy.... 2014-05-21 13:47 GMT+08:00 Bob Ippolito : > How precisely are you trying to run the client? Are you typing it in to > the same terminal? If so, then the client is never actually started, > because when you type ./cli the input is going to ./serv and not the shell. > Try running the client in a separate terminal. > > It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. > > > On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: > >> hi guys, >> I'm newbie, just begin to learn Haskell. >> now i write a very simple server and client . >> >> Server: >> import Control.Concurrent >> import System.IO >> import Network >> >> port :: Int >> port = 1234 >> >> main :: IO () >> main = withSocketsDo $ do >> s <- listenOn $ PortNumber $ fromIntegral port >> (h, _, _) <- accept s >> >> sline <- hGetLine h >> hPutStrLn h sline >> putStrLn $ "send "++sline >> threadDelay 1000000 >> >> hClose h >> sClose s >> >> Client : >> import System.IO >> import Network >> >> port :: Int >> port = 1234 >> >> main :: IO () >> main = withSocketsDo $ do >> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >> hPutStrLn h "hello" >> bs <- hGetContents h >> putStrLn bs >> hClose h >> >> >> And, it doesn't work now . I run ./serv , then run ./cli , they will >> block all the time. >> but, when i run ./serv, and telnet localhost 1234 in another terminal, >> it works fine. >> so i don't know what's the wrong with my code. >> anybody can tell me about my problem? >> >> os is Debian 7, haskell-platform 2012.2.0.0 >> >> thanks a lot!!! >> -- >> K.I.S.S. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- K.I.S.S. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Wed May 21 05:57:12 2014 From: bob at redivi.com (Bob Ippolito) Date: Tue, 20 May 2014 22:57:12 -0700 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora 20 and it worked fine there as well (both with runhaskell or compiled with -O). I might start adding putStrLn statements to the code to see where it's unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in case the issue is a DNS misconfiguration. On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: > thanks for your replay. > i run the two program in two different teriminal for sure. > > it works for you? > but why can't run well on my computer. > > make me creazy.... > > > 2014-05-21 13:47 GMT+08:00 Bob Ippolito : > > How precisely are you trying to run the client? Are you typing it in to >> the same terminal? If so, then the client is never actually started, >> because when you type ./cli the input is going to ./serv and not the shell. >> Try running the client in a separate terminal. >> >> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >> >> >> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >> >>> hi guys, >>> I'm newbie, just begin to learn Haskell. >>> now i write a very simple server and client . >>> >>> Server: >>> import Control.Concurrent >>> import System.IO >>> import Network >>> >>> port :: Int >>> port = 1234 >>> >>> main :: IO () >>> main = withSocketsDo $ do >>> s <- listenOn $ PortNumber $ fromIntegral port >>> (h, _, _) <- accept s >>> >>> sline <- hGetLine h >>> hPutStrLn h sline >>> putStrLn $ "send "++sline >>> threadDelay 1000000 >>> >>> hClose h >>> sClose s >>> >>> Client : >>> import System.IO >>> import Network >>> >>> port :: Int >>> port = 1234 >>> >>> main :: IO () >>> main = withSocketsDo $ do >>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>> hPutStrLn h "hello" >>> bs <- hGetContents h >>> putStrLn bs >>> hClose h >>> >>> >>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>> block all the time. >>> but, when i run ./serv, and telnet localhost 1234 in another terminal, >>> it works fine. >>> so i don't know what's the wrong with my code. >>> anybody can tell me about my problem? >>> >>> os is Debian 7, haskell-platform 2012.2.0.0 >>> >>> thanks a lot!!! >>> -- >>> K.I.S.S. >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > -- > K.I.S.S. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hckjsnzf at gmail.com Wed May 21 06:10:30 2014 From: hckjsnzf at gmail.com (yang.zhao) Date: Wed, 21 May 2014 14:10:30 +0800 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: Here's my new code: $ cat serv.hs import Control.Concurrent import System.IO import Network port :: Int port = 1234 main :: IO () main = withSocketsDo $ do s <- listenOn $ PortNumber $ fromIntegral port putStrLn "Listening..." (h, _, _) <- accept s putStrLn "After accept" sline <- hGetLine h putStrLn "get line from handle" hPutStrLn h sline putStrLn $ "send "++sline threadDelay 1000000 hClose h sClose s -------- $ cat clie.hs import System.IO import Network port :: Int port = 1234 main :: IO () main = withSocketsDo $ do h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port putStrLn "After connect" hPutStrLn h "hello" putStrLn "put hello to handle done" bs <- hGetContents h putStrLn "read from handle done" putStrLn bs hClose h === one terminal, run ./serv, then run ./clie in another terminal. Output is : $ ./serv Listening... After accept $ ./clie After connect put hello to handle done read from handle done it seems that client has read from then handle, but donesn't read anything, then block. and server donesn't receive anything, still wait for something... ghc version is 7.4.1, because of this?.. 2014-05-21 13:57 GMT+08:00 Bob Ippolito : > Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora > 20 and it worked fine there as well (both with runhaskell or compiled with > -O). > > I might start adding putStrLn statements to the code to see where it's > unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in > case the issue is a DNS misconfiguration. > > > > On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: > >> thanks for your replay. >> i run the two program in two different teriminal for sure. >> >> it works for you? >> but why can't run well on my computer. >> >> make me creazy.... >> >> >> 2014-05-21 13:47 GMT+08:00 Bob Ippolito : >> >> How precisely are you trying to run the client? Are you typing it in to >>> the same terminal? If so, then the client is never actually started, >>> because when you type ./cli the input is going to ./serv and not the shell. >>> Try running the client in a separate terminal. >>> >>> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >>> >>> >>> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >>> >>>> hi guys, >>>> I'm newbie, just begin to learn Haskell. >>>> now i write a very simple server and client . >>>> >>>> Server: >>>> import Control.Concurrent >>>> import System.IO >>>> import Network >>>> >>>> port :: Int >>>> port = 1234 >>>> >>>> main :: IO () >>>> main = withSocketsDo $ do >>>> s <- listenOn $ PortNumber $ fromIntegral port >>>> (h, _, _) <- accept s >>>> >>>> sline <- hGetLine h >>>> hPutStrLn h sline >>>> putStrLn $ "send "++sline >>>> threadDelay 1000000 >>>> >>>> hClose h >>>> sClose s >>>> >>>> Client : >>>> import System.IO >>>> import Network >>>> >>>> port :: Int >>>> port = 1234 >>>> >>>> main :: IO () >>>> main = withSocketsDo $ do >>>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>>> hPutStrLn h "hello" >>>> bs <- hGetContents h >>>> putStrLn bs >>>> hClose h >>>> >>>> >>>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>>> block all the time. >>>> but, when i run ./serv, and telnet localhost 1234 in another terminal, >>>> it works fine. >>>> so i don't know what's the wrong with my code. >>>> anybody can tell me about my problem? >>>> >>>> os is Debian 7, haskell-platform 2012.2.0.0 >>>> >>>> thanks a lot!!! >>>> -- >>>> K.I.S.S. >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> >> -- >> K.I.S.S. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- K.I.S.S. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Wed May 21 07:11:59 2014 From: bob at redivi.com (Bob Ippolito) Date: Wed, 21 May 2014 00:11:59 -0700 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: I don't have a GHC installation that old, but I think that a bug in that version of GHC is the only reasonable explanation for this. I highly recommend that you upgrade to GHC 7.8.x or at least 7.6.x. Lazy IO is a bit unsafe and error-prone in some scenarios. hGetContents doesn't really do any reading from the handle until the input is demanded, which is why you can print "read from handle done" but not the contents of bs. On Tue, May 20, 2014 at 11:10 PM, yang.zhao wrote: > Here's my new code: > > $ cat serv.hs > > import Control.Concurrent > import System.IO > import Network > > port :: Int > port = 1234 > > main :: IO () > main = withSocketsDo $ do > s <- listenOn $ PortNumber $ fromIntegral port > putStrLn "Listening..." > > (h, _, _) <- accept s > putStrLn "After accept" > sline <- hGetLine h > putStrLn "get line from handle" > > hPutStrLn h sline > putStrLn $ "send "++sline > threadDelay 1000000 > > hClose h > sClose s > > -------- > $ cat clie.hs > > import System.IO > import Network > > port :: Int > port = 1234 > > main :: IO () > main = withSocketsDo $ do > h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port > putStrLn "After connect" > hPutStrLn h "hello" > putStrLn "put hello to handle done" > bs <- hGetContents h > putStrLn "read from handle done" > putStrLn bs > hClose h > > > === > one terminal, run ./serv, then run ./clie in another terminal. Output is : > > $ ./serv > Listening... > After accept > > $ ./clie > After connect > put hello to handle done > read from handle done > > > it seems that client has read from then handle, but donesn't read > anything, then block. > and server donesn't receive anything, still wait for something... > > ghc version is 7.4.1, because of this?.. > > > 2014-05-21 13:57 GMT+08:00 Bob Ippolito : > > Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora >> 20 and it worked fine there as well (both with runhaskell or compiled with >> -O). >> >> I might start adding putStrLn statements to the code to see where it's >> unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in >> case the issue is a DNS misconfiguration. >> >> >> >> On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: >> >>> thanks for your replay. >>> i run the two program in two different teriminal for sure. >>> >>> it works for you? >>> but why can't run well on my computer. >>> >>> make me creazy.... >>> >>> >>> 2014-05-21 13:47 GMT+08:00 Bob Ippolito : >>> >>> How precisely are you trying to run the client? Are you typing it in to >>>> the same terminal? If so, then the client is never actually started, >>>> because when you type ./cli the input is going to ./serv and not the shell. >>>> Try running the client in a separate terminal. >>>> >>>> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >>>> >>>> >>>> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >>>> >>>>> hi guys, >>>>> I'm newbie, just begin to learn Haskell. >>>>> now i write a very simple server and client . >>>>> >>>>> Server: >>>>> import Control.Concurrent >>>>> import System.IO >>>>> import Network >>>>> >>>>> port :: Int >>>>> port = 1234 >>>>> >>>>> main :: IO () >>>>> main = withSocketsDo $ do >>>>> s <- listenOn $ PortNumber $ fromIntegral port >>>>> (h, _, _) <- accept s >>>>> >>>>> sline <- hGetLine h >>>>> hPutStrLn h sline >>>>> putStrLn $ "send "++sline >>>>> threadDelay 1000000 >>>>> >>>>> hClose h >>>>> sClose s >>>>> >>>>> Client : >>>>> import System.IO >>>>> import Network >>>>> >>>>> port :: Int >>>>> port = 1234 >>>>> >>>>> main :: IO () >>>>> main = withSocketsDo $ do >>>>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>>>> hPutStrLn h "hello" >>>>> bs <- hGetContents h >>>>> putStrLn bs >>>>> hClose h >>>>> >>>>> >>>>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>>>> block all the time. >>>>> but, when i run ./serv, and telnet localhost 1234 in another >>>>> terminal, it works fine. >>>>> so i don't know what's the wrong with my code. >>>>> anybody can tell me about my problem? >>>>> >>>>> os is Debian 7, haskell-platform 2012.2.0.0 >>>>> >>>>> thanks a lot!!! >>>>> -- >>>>> K.I.S.S. >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> >>> -- >>> K.I.S.S. >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > -- > K.I.S.S. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hckjsnzf at gmail.com Wed May 21 07:15:04 2014 From: hckjsnzf at gmail.com (yang.zhao) Date: Wed, 21 May 2014 15:15:04 +0800 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: omg, i test it on Windows, it's ok now. GHC is 7.6.3. i really don't konw why this happen.. but there is another question on Windows main = withSocketsDo $ do s <- listenOn $ PortNumber $ fromIntegral port putStrLn "Listening..." (h, _, _) <- accept s putStrLn "After accept" sline <- hGetLine h putStrLn $ "get line from handle"++sline hPutStrLn h "xxxx...." putStrLn "send first done" --second recv slinea <- hGetLine h putStrLn $ "get line from handle"++slinea hPutStrLn h "yyyy....." putStrLn "send second done" threadDelay 1000000 -- cli: main = withSocketsDo $ do h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port putStrLn "After connect" hPutStrLn h "xxx" putStrLn "put first to handle done" bs <- hGetContents h putStrLn "read from handle done " putStrLn bs --second send hPutStrLn h "yyy" putStrLn "put second to handle done" bfs <- hGetContents h putStrLn "read from handle done " putStrLn bs the second send and recv is blocking... server's output: Listening... After accept get line from handlexxx send first done client's output: After connect put first to handle done read from handle done xxx.... ..... T_T 2014-05-21 14:10 GMT+08:00 yang.zhao : > Here's my new code: > > $ cat serv.hs > > import Control.Concurrent > import System.IO > import Network > > port :: Int > port = 1234 > > main :: IO () > main = withSocketsDo $ do > s <- listenOn $ PortNumber $ fromIntegral port > putStrLn "Listening..." > > (h, _, _) <- accept s > putStrLn "After accept" > sline <- hGetLine h > putStrLn "get line from handle" > > hPutStrLn h sline > putStrLn $ "send "++sline > threadDelay 1000000 > > hClose h > sClose s > > -------- > $ cat clie.hs > > import System.IO > import Network > > port :: Int > port = 1234 > > main :: IO () > main = withSocketsDo $ do > h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port > putStrLn "After connect" > hPutStrLn h "hello" > putStrLn "put hello to handle done" > bs <- hGetContents h > putStrLn "read from handle done" > putStrLn bs > hClose h > > > === > one terminal, run ./serv, then run ./clie in another terminal. Output is : > > $ ./serv > Listening... > After accept > > $ ./clie > After connect > put hello to handle done > read from handle done > > > it seems that client has read from then handle, but donesn't read > anything, then block. > and server donesn't receive anything, still wait for something... > > ghc version is 7.4.1, because of this?.. > > > 2014-05-21 13:57 GMT+08:00 Bob Ippolito : > > Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora >> 20 and it worked fine there as well (both with runhaskell or compiled with >> -O). >> >> I might start adding putStrLn statements to the code to see where it's >> unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in >> case the issue is a DNS misconfiguration. >> >> >> >> On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: >> >>> thanks for your replay. >>> i run the two program in two different teriminal for sure. >>> >>> it works for you? >>> but why can't run well on my computer. >>> >>> make me creazy.... >>> >>> >>> 2014-05-21 13:47 GMT+08:00 Bob Ippolito : >>> >>> How precisely are you trying to run the client? Are you typing it in to >>>> the same terminal? If so, then the client is never actually started, >>>> because when you type ./cli the input is going to ./serv and not the shell. >>>> Try running the client in a separate terminal. >>>> >>>> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >>>> >>>> >>>> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >>>> >>>>> hi guys, >>>>> I'm newbie, just begin to learn Haskell. >>>>> now i write a very simple server and client . >>>>> >>>>> Server: >>>>> import Control.Concurrent >>>>> import System.IO >>>>> import Network >>>>> >>>>> port :: Int >>>>> port = 1234 >>>>> >>>>> main :: IO () >>>>> main = withSocketsDo $ do >>>>> s <- listenOn $ PortNumber $ fromIntegral port >>>>> (h, _, _) <- accept s >>>>> >>>>> sline <- hGetLine h >>>>> hPutStrLn h sline >>>>> putStrLn $ "send "++sline >>>>> threadDelay 1000000 >>>>> >>>>> hClose h >>>>> sClose s >>>>> >>>>> Client : >>>>> import System.IO >>>>> import Network >>>>> >>>>> port :: Int >>>>> port = 1234 >>>>> >>>>> main :: IO () >>>>> main = withSocketsDo $ do >>>>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>>>> hPutStrLn h "hello" >>>>> bs <- hGetContents h >>>>> putStrLn bs >>>>> hClose h >>>>> >>>>> >>>>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>>>> block all the time. >>>>> but, when i run ./serv, and telnet localhost 1234 in another >>>>> terminal, it works fine. >>>>> so i don't know what's the wrong with my code. >>>>> anybody can tell me about my problem? >>>>> >>>>> os is Debian 7, haskell-platform 2012.2.0.0 >>>>> >>>>> thanks a lot!!! >>>>> -- >>>>> K.I.S.S. >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> >>> -- >>> K.I.S.S. >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > -- > K.I.S.S. > -- K.I.S.S. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Wed May 21 08:06:36 2014 From: bob at redivi.com (Bob Ippolito) Date: Wed, 21 May 2014 01:06:36 -0700 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: hGetContents reads a handle until EOF, so you've implement a deadlock. The server is waiting to read a second line, but the client is waiting for the server to close the socket in order to do the first print. Perhaps you meant to use hGetLine instead? On Wed, May 21, 2014 at 12:15 AM, yang.zhao wrote: > omg, i test it on Windows, it's ok now. GHC is 7.6.3. > i really don't konw why this happen.. > > but there is another question on Windows > > > main = withSocketsDo $ do > s <- listenOn $ PortNumber $ fromIntegral port > putStrLn "Listening..." > (h, _, _) <- accept s > putStrLn "After accept" > sline <- hGetLine h > putStrLn $ "get line from handle"++sline > hPutStrLn h "xxxx...." > putStrLn "send first done" > > --second recv > slinea <- hGetLine h > putStrLn $ "get line from handle"++slinea > > hPutStrLn h "yyyy....." > putStrLn "send second done" > > threadDelay 1000000 > -- > cli: > > main = withSocketsDo $ do > h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port > putStrLn "After connect" > hPutStrLn h "xxx" > putStrLn "put first to handle done" > > bs <- hGetContents h > putStrLn "read from handle done " > putStrLn bs > > --second send > hPutStrLn h "yyy" > putStrLn "put second to handle done" > bfs <- hGetContents h > > putStrLn "read from handle done " > putStrLn bs > > > the second send and recv is blocking... > server's output: > Listening... > After accept > get line from handlexxx > send first done > > client's output: > After connect > put first to handle done > read from handle done > xxx.... > > > ..... T_T > > > > > 2014-05-21 14:10 GMT+08:00 yang.zhao : > > Here's my new code: >> >> $ cat serv.hs >> >> import Control.Concurrent >> import System.IO >> import Network >> >> port :: Int >> port = 1234 >> >> main :: IO () >> main = withSocketsDo $ do >> s <- listenOn $ PortNumber $ fromIntegral port >> putStrLn "Listening..." >> >> (h, _, _) <- accept s >> putStrLn "After accept" >> sline <- hGetLine h >> putStrLn "get line from handle" >> >> hPutStrLn h sline >> putStrLn $ "send "++sline >> threadDelay 1000000 >> >> hClose h >> sClose s >> >> -------- >> $ cat clie.hs >> >> import System.IO >> import Network >> >> port :: Int >> port = 1234 >> >> main :: IO () >> main = withSocketsDo $ do >> h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port >> putStrLn "After connect" >> hPutStrLn h "hello" >> putStrLn "put hello to handle done" >> bs <- hGetContents h >> putStrLn "read from handle done" >> putStrLn bs >> hClose h >> >> >> === >> one terminal, run ./serv, then run ./clie in another terminal. Output is : >> >> $ ./serv >> Listening... >> After accept >> >> $ ./clie >> After connect >> put hello to handle done >> read from handle done >> >> >> it seems that client has read from then handle, but donesn't read >> anything, then block. >> and server donesn't receive anything, still wait for something... >> >> ghc version is 7.4.1, because of this?.. >> >> >> 2014-05-21 13:57 GMT+08:00 Bob Ippolito : >> >> Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora >>> 20 and it worked fine there as well (both with runhaskell or compiled with >>> -O). >>> >>> I might start adding putStrLn statements to the code to see where it's >>> unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in >>> case the issue is a DNS misconfiguration. >>> >>> >>> >>> On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: >>> >>>> thanks for your replay. >>>> i run the two program in two different teriminal for sure. >>>> >>>> it works for you? >>>> but why can't run well on my computer. >>>> >>>> make me creazy.... >>>> >>>> >>>> 2014-05-21 13:47 GMT+08:00 Bob Ippolito : >>>> >>>> How precisely are you trying to run the client? Are you typing it in to >>>>> the same terminal? If so, then the client is never actually started, >>>>> because when you type ./cli the input is going to ./serv and not the shell. >>>>> Try running the client in a separate terminal. >>>>> >>>>> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >>>>> >>>>> >>>>> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >>>>> >>>>>> hi guys, >>>>>> I'm newbie, just begin to learn Haskell. >>>>>> now i write a very simple server and client . >>>>>> >>>>>> Server: >>>>>> import Control.Concurrent >>>>>> import System.IO >>>>>> import Network >>>>>> >>>>>> port :: Int >>>>>> port = 1234 >>>>>> >>>>>> main :: IO () >>>>>> main = withSocketsDo $ do >>>>>> s <- listenOn $ PortNumber $ fromIntegral port >>>>>> (h, _, _) <- accept s >>>>>> >>>>>> sline <- hGetLine h >>>>>> hPutStrLn h sline >>>>>> putStrLn $ "send "++sline >>>>>> threadDelay 1000000 >>>>>> >>>>>> hClose h >>>>>> sClose s >>>>>> >>>>>> Client : >>>>>> import System.IO >>>>>> import Network >>>>>> >>>>>> port :: Int >>>>>> port = 1234 >>>>>> >>>>>> main :: IO () >>>>>> main = withSocketsDo $ do >>>>>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>>>>> hPutStrLn h "hello" >>>>>> bs <- hGetContents h >>>>>> putStrLn bs >>>>>> hClose h >>>>>> >>>>>> >>>>>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>>>>> block all the time. >>>>>> but, when i run ./serv, and telnet localhost 1234 in another >>>>>> terminal, it works fine. >>>>>> so i don't know what's the wrong with my code. >>>>>> anybody can tell me about my problem? >>>>>> >>>>>> os is Debian 7, haskell-platform 2012.2.0.0 >>>>>> >>>>>> thanks a lot!!! >>>>>> -- >>>>>> K.I.S.S. >>>>>> >>>>>> _______________________________________________ >>>>>> Beginners mailing list >>>>>> Beginners at haskell.org >>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> >>>> -- >>>> K.I.S.S. >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> >> -- >> K.I.S.S. >> > > > > -- > K.I.S.S. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hsyl20 at gmail.com Wed May 21 08:07:27 2014 From: hsyl20 at gmail.com (Sylvain Henry) Date: Wed, 21 May 2014 10:07:27 +0200 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: hGetContents puts the handle in semi-closed state and reads until there is nothing left to read. Hence you have a deadlock. Use hGetLine in "cli". Sylvain 2014-05-21 9:15 GMT+02:00 yang.zhao : > omg, i test it on Windows, it's ok now. GHC is 7.6.3. > i really don't konw why this happen.. > > but there is another question on Windows > > > main = withSocketsDo $ do > s <- listenOn $ PortNumber $ fromIntegral port > putStrLn "Listening..." > (h, _, _) <- accept s > putStrLn "After accept" > sline <- hGetLine h > putStrLn $ "get line from handle"++sline > hPutStrLn h "xxxx...." > putStrLn "send first done" > > --second recv > slinea <- hGetLine h > putStrLn $ "get line from handle"++slinea > > hPutStrLn h "yyyy....." > putStrLn "send second done" > > threadDelay 1000000 > -- > cli: > > main = withSocketsDo $ do > h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port > putStrLn "After connect" > hPutStrLn h "xxx" > putStrLn "put first to handle done" > > bs <- hGetContents h > putStrLn "read from handle done " > putStrLn bs > > --second send > hPutStrLn h "yyy" > putStrLn "put second to handle done" > bfs <- hGetContents h > > putStrLn "read from handle done " > putStrLn bs > > > the second send and recv is blocking... > server's output: > Listening... > After accept > get line from handlexxx > send first done > > client's output: > After connect > put first to handle done > read from handle done > xxx.... > > > ..... T_T > > > > > 2014-05-21 14:10 GMT+08:00 yang.zhao : > > Here's my new code: >> >> $ cat serv.hs >> >> import Control.Concurrent >> import System.IO >> import Network >> >> port :: Int >> port = 1234 >> >> main :: IO () >> main = withSocketsDo $ do >> s <- listenOn $ PortNumber $ fromIntegral port >> putStrLn "Listening..." >> >> (h, _, _) <- accept s >> putStrLn "After accept" >> sline <- hGetLine h >> putStrLn "get line from handle" >> >> hPutStrLn h sline >> putStrLn $ "send "++sline >> threadDelay 1000000 >> >> hClose h >> sClose s >> >> -------- >> $ cat clie.hs >> >> import System.IO >> import Network >> >> port :: Int >> port = 1234 >> >> main :: IO () >> main = withSocketsDo $ do >> h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port >> putStrLn "After connect" >> hPutStrLn h "hello" >> putStrLn "put hello to handle done" >> bs <- hGetContents h >> putStrLn "read from handle done" >> putStrLn bs >> hClose h >> >> >> === >> one terminal, run ./serv, then run ./clie in another terminal. Output is : >> >> $ ./serv >> Listening... >> After accept >> >> $ ./clie >> After connect >> put hello to handle done >> read from handle done >> >> >> it seems that client has read from then handle, but donesn't read >> anything, then block. >> and server donesn't receive anything, still wait for something... >> >> ghc version is 7.4.1, because of this?.. >> >> >> 2014-05-21 13:57 GMT+08:00 Bob Ippolito : >> >> Not sure why you're having issues, I just tried it on GHC 7.6.3 on Fedora >>> 20 and it worked fine there as well (both with runhaskell or compiled with >>> -O). >>> >>> I might start adding putStrLn statements to the code to see where it's >>> unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in >>> case the issue is a DNS misconfiguration. >>> >>> >>> >>> On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: >>> >>>> thanks for your replay. >>>> i run the two program in two different teriminal for sure. >>>> >>>> it works for you? >>>> but why can't run well on my computer. >>>> >>>> make me creazy.... >>>> >>>> >>>> 2014-05-21 13:47 GMT+08:00 Bob Ippolito : >>>> >>>> How precisely are you trying to run the client? Are you typing it in to >>>>> the same terminal? If so, then the client is never actually started, >>>>> because when you type ./cli the input is going to ./serv and not the shell. >>>>> Try running the client in a separate terminal. >>>>> >>>>> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >>>>> >>>>> >>>>> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >>>>> >>>>>> hi guys, >>>>>> I'm newbie, just begin to learn Haskell. >>>>>> now i write a very simple server and client . >>>>>> >>>>>> Server: >>>>>> import Control.Concurrent >>>>>> import System.IO >>>>>> import Network >>>>>> >>>>>> port :: Int >>>>>> port = 1234 >>>>>> >>>>>> main :: IO () >>>>>> main = withSocketsDo $ do >>>>>> s <- listenOn $ PortNumber $ fromIntegral port >>>>>> (h, _, _) <- accept s >>>>>> >>>>>> sline <- hGetLine h >>>>>> hPutStrLn h sline >>>>>> putStrLn $ "send "++sline >>>>>> threadDelay 1000000 >>>>>> >>>>>> hClose h >>>>>> sClose s >>>>>> >>>>>> Client : >>>>>> import System.IO >>>>>> import Network >>>>>> >>>>>> port :: Int >>>>>> port = 1234 >>>>>> >>>>>> main :: IO () >>>>>> main = withSocketsDo $ do >>>>>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>>>>> hPutStrLn h "hello" >>>>>> bs <- hGetContents h >>>>>> putStrLn bs >>>>>> hClose h >>>>>> >>>>>> >>>>>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>>>>> block all the time. >>>>>> but, when i run ./serv, and telnet localhost 1234 in another >>>>>> terminal, it works fine. >>>>>> so i don't know what's the wrong with my code. >>>>>> anybody can tell me about my problem? >>>>>> >>>>>> os is Debian 7, haskell-platform 2012.2.0.0 >>>>>> >>>>>> thanks a lot!!! >>>>>> -- >>>>>> K.I.S.S. >>>>>> >>>>>> _______________________________________________ >>>>>> Beginners mailing list >>>>>> Beginners at haskell.org >>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> >>>> -- >>>> K.I.S.S. >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> >> -- >> K.I.S.S. >> > > > > -- > K.I.S.S. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hckjsnzf at gmail.com Wed May 21 08:17:04 2014 From: hckjsnzf at gmail.com (yang.zhao) Date: Wed, 21 May 2014 16:17:04 +0800 Subject: [Haskell-beginners] Haskell network sample In-Reply-To: References: Message-ID: Thank you very much, Bob and Sylvain. i test hGetLine instead of hGetContents. 2014-05-21 16:07 GMT+08:00 Sylvain Henry : > hGetContents puts the handle in semi-closed state and reads until there is > nothing left to read. Hence you have a deadlock. Use hGetLine in "cli". > > Sylvain > > > 2014-05-21 9:15 GMT+02:00 yang.zhao : > > omg, i test it on Windows, it's ok now. GHC is 7.6.3. >> i really don't konw why this happen.. >> >> but there is another question on Windows >> >> >> main = withSocketsDo $ do >> s <- listenOn $ PortNumber $ fromIntegral port >> putStrLn "Listening..." >> (h, _, _) <- accept s >> putStrLn "After accept" >> sline <- hGetLine h >> putStrLn $ "get line from handle"++sline >> hPutStrLn h "xxxx...." >> putStrLn "send first done" >> >> --second recv >> slinea <- hGetLine h >> putStrLn $ "get line from handle"++slinea >> >> hPutStrLn h "yyyy....." >> putStrLn "send second done" >> >> threadDelay 1000000 >> -- >> cli: >> >> main = withSocketsDo $ do >> h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port >> putStrLn "After connect" >> hPutStrLn h "xxx" >> putStrLn "put first to handle done" >> >> bs <- hGetContents h >> putStrLn "read from handle done " >> putStrLn bs >> >> --second send >> hPutStrLn h "yyy" >> putStrLn "put second to handle done" >> bfs <- hGetContents h >> >> putStrLn "read from handle done " >> putStrLn bs >> >> >> the second send and recv is blocking... >> server's output: >> Listening... >> After accept >> get line from handlexxx >> send first done >> >> client's output: >> After connect >> put first to handle done >> read from handle done >> xxx.... >> >> >> ..... T_T >> >> >> >> >> 2014-05-21 14:10 GMT+08:00 yang.zhao : >> >> Here's my new code: >>> >>> $ cat serv.hs >>> >>> import Control.Concurrent >>> import System.IO >>> import Network >>> >>> port :: Int >>> port = 1234 >>> >>> main :: IO () >>> main = withSocketsDo $ do >>> s <- listenOn $ PortNumber $ fromIntegral port >>> putStrLn "Listening..." >>> >>> (h, _, _) <- accept s >>> putStrLn "After accept" >>> sline <- hGetLine h >>> putStrLn "get line from handle" >>> >>> hPutStrLn h sline >>> putStrLn $ "send "++sline >>> threadDelay 1000000 >>> >>> hClose h >>> sClose s >>> >>> -------- >>> $ cat clie.hs >>> >>> import System.IO >>> import Network >>> >>> port :: Int >>> port = 1234 >>> >>> main :: IO () >>> main = withSocketsDo $ do >>> h <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port >>> putStrLn "After connect" >>> hPutStrLn h "hello" >>> putStrLn "put hello to handle done" >>> bs <- hGetContents h >>> putStrLn "read from handle done" >>> putStrLn bs >>> hClose h >>> >>> >>> === >>> one terminal, run ./serv, then run ./clie in another terminal. Output is >>> : >>> >>> $ ./serv >>> Listening... >>> After accept >>> >>> $ ./clie >>> After connect >>> put hello to handle done >>> read from handle done >>> >>> >>> it seems that client has read from then handle, but donesn't read >>> anything, then block. >>> and server donesn't receive anything, still wait for something... >>> >>> ghc version is 7.4.1, because of this?.. >>> >>> >>> 2014-05-21 13:57 GMT+08:00 Bob Ippolito : >>> >>> Not sure why you're having issues, I just tried it on GHC 7.6.3 on >>>> Fedora 20 and it worked fine there as well (both with runhaskell or >>>> compiled with -O). >>>> >>>> I might start adding putStrLn statements to the code to see where it's >>>> unexpectedly blocking, or perhaps use 127.0.0.1 instead of "localhost" in >>>> case the issue is a DNS misconfiguration. >>>> >>>> >>>> >>>> On Tue, May 20, 2014 at 10:51 PM, yang.zhao wrote: >>>> >>>>> thanks for your replay. >>>>> i run the two program in two different teriminal for sure. >>>>> >>>>> it works for you? >>>>> but why can't run well on my computer. >>>>> >>>>> make me creazy.... >>>>> >>>>> >>>>> 2014-05-21 13:47 GMT+08:00 Bob Ippolito : >>>>> >>>>> How precisely are you trying to run the client? Are you typing it in >>>>>> to the same terminal? If so, then the client is never actually started, >>>>>> because when you type ./cli the input is going to ./serv and not the shell. >>>>>> Try running the client in a separate terminal. >>>>>> >>>>>> It works for me here on Mac OS X 10.9.2 with GHC 7.8.2. >>>>>> >>>>>> >>>>>> On Tue, May 20, 2014 at 10:42 PM, yang.zhao wrote: >>>>>> >>>>>>> hi guys, >>>>>>> I'm newbie, just begin to learn Haskell. >>>>>>> now i write a very simple server and client . >>>>>>> >>>>>>> Server: >>>>>>> import Control.Concurrent >>>>>>> import System.IO >>>>>>> import Network >>>>>>> >>>>>>> port :: Int >>>>>>> port = 1234 >>>>>>> >>>>>>> main :: IO () >>>>>>> main = withSocketsDo $ do >>>>>>> s <- listenOn $ PortNumber $ fromIntegral port >>>>>>> (h, _, _) <- accept s >>>>>>> >>>>>>> sline <- hGetLine h >>>>>>> hPutStrLn h sline >>>>>>> putStrLn $ "send "++sline >>>>>>> threadDelay 1000000 >>>>>>> >>>>>>> hClose h >>>>>>> sClose s >>>>>>> >>>>>>> Client : >>>>>>> import System.IO >>>>>>> import Network >>>>>>> >>>>>>> port :: Int >>>>>>> port = 1234 >>>>>>> >>>>>>> main :: IO () >>>>>>> main = withSocketsDo $ do >>>>>>> h <- connectTo "localhost" $ PortNumber $ fromIntegral port >>>>>>> hPutStrLn h "hello" >>>>>>> bs <- hGetContents h >>>>>>> putStrLn bs >>>>>>> hClose h >>>>>>> >>>>>>> >>>>>>> And, it doesn't work now . I run ./serv , then run ./cli , they will >>>>>>> block all the time. >>>>>>> but, when i run ./serv, and telnet localhost 1234 in another >>>>>>> terminal, it works fine. >>>>>>> so i don't know what's the wrong with my code. >>>>>>> anybody can tell me about my problem? >>>>>>> >>>>>>> os is Debian 7, haskell-platform 2012.2.0.0 >>>>>>> >>>>>>> thanks a lot!!! >>>>>>> -- >>>>>>> K.I.S.S. >>>>>>> >>>>>>> _______________________________________________ >>>>>>> Beginners mailing list >>>>>>> Beginners at haskell.org >>>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>>> >>>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> Beginners mailing list >>>>>> Beginners at haskell.org >>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>> >>>>>> >>>>> >>>>> >>>>> -- >>>>> K.I.S.S. >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> >>> -- >>> K.I.S.S. >>> >> >> >> >> -- >> K.I.S.S. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- K.I.S.S. -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Wed May 21 09:18:59 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 21 May 2014 03:18:59 -0600 Subject: [Haskell-beginners] Instance declaration needs more info? Message-ID: <537C6F83.9090103@ucdavis.edu> Hi All, I'm trying to write a simplified dimensional library where where quantities in meters, seconds and meters/second can all co-exist adjusting their respective units when multiplied and/or divided. Also, meter+meter is allowed, but meter+second should cause the type checker to complain. This is a bit like a *much* simplified version of the Units library. However, I am having trouble understanding why my instance declaration below appears to be under specified. Here's the code: ------------ module Dimensional where import qualified Prelude -- A Group allows you to add and subtract (but not multiply or divide) class Group a where (+) :: a -> a -> a (-) :: a -> a -> a x - y = x + negate y negate :: a -> a negate x = fromInteger 0 - x fromInteger :: Prelude.Integer -> a class Unit a where (*) :: (Unit a, Unit b, Unit c) => a -> b -> c (/) :: (Unit a, Unit b, Unit c) => a -> b -> c toDouble :: a -> Prelude.Double instance Unit Prelude.Double where (*) x y = (Prelude.*) x (toDouble y) -- <----- Error here (/) x y = (Prelude./) x (toDouble y) toDouble x = x ------------ GHC complains as follows: Could not deduce (c ~ Prelude.Double) from the context (Unit Prelude.Double, Unit b, Unit c) bound by the type signature for * :: (Unit Prelude.Double, Unit b, Unit c) => Prelude.Double -> b -> c at /code/haskell/dimensional.hs:25:5-43 `c' is a rigid type variable bound by the type signature for * :: (Unit Prelude.Double, Unit b, Unit c) => Prelude.Double -> b -> c at /code/haskell/dimensional.hs:25:5 In the first argument of `(Prelude.*)', namely `x' In the expression: (Prelude.*) x (toDouble y) In an equation for `*': * x y = (Prelude.*) x (toDouble y) Any pointers would be much appreciated! Thanks, Dimitri From bob at redivi.com Wed May 21 09:28:41 2014 From: bob at redivi.com (Bob Ippolito) Date: Wed, 21 May 2014 02:28:41 -0700 Subject: [Haskell-beginners] Instance declaration needs more info? In-Reply-To: <537C6F83.9090103@ucdavis.edu> References: <537C6F83.9090103@ucdavis.edu> Message-ID: The trouble is that your specification says that Unit c is the return type (the caller can choose any Unit instance), but this implementation can only evaluate to a Prelude.Double. One way to solve this is to add a fromDouble :: Double -> Unit a and wrap the expression with that in order to satisfy Unit c. On Wednesday, May 21, 2014, Dimitri DeFigueiredo wrote: > Hi All, > > I'm trying to write a simplified dimensional library where where > quantities in meters, seconds and meters/second can all co-exist adjusting > their respective units when multiplied and/or divided. > > Also, meter+meter is allowed, but meter+second should cause the type > checker to complain. > > This is a bit like a *much* simplified version of the Units library. > However, I am having trouble understanding why my instance declaration > below appears to be under specified. Here's the code: > > ------------ > module Dimensional where > import qualified Prelude > > -- A Group allows you to add and subtract (but not multiply or divide) > class Group a where > > (+) :: a -> a -> a > > (-) :: a -> a -> a > x - y = x + negate y > > negate :: a -> a > negate x = fromInteger 0 - x > > fromInteger :: Prelude.Integer -> a > > class Unit a where > > (*) :: (Unit a, Unit b, Unit c) => a -> b -> c > (/) :: (Unit a, Unit b, Unit c) => a -> b -> c > toDouble :: a -> Prelude.Double > > instance Unit Prelude.Double where > > (*) x y = (Prelude.*) x (toDouble y) -- <----- Error here > (/) x y = (Prelude./) x (toDouble y) > toDouble x = x > > ------------ > GHC complains as follows: > > Could not deduce (c ~ Prelude.Double) > from the context (Unit Prelude.Double, Unit b, Unit c) > bound by the type signature for > * :: (Unit Prelude.Double, Unit b, Unit c) => > Prelude.Double -> b -> c > at /code/haskell/dimensional.hs:25:5-43 > `c' is a rigid type variable bound by > the type signature for > * :: (Unit Prelude.Double, Unit b, Unit c) => > Prelude.Double -> b -> c > at /code/haskell/dimensional.hs:25:5 > In the first argument of `(Prelude.*)', namely `x' > In the expression: (Prelude.*) x (toDouble y) > In an equation for `*': * x y = (Prelude.*) x (toDouble y) > > Any pointers would be much appreciated! > > Thanks, > > Dimitri > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Wed May 21 16:39:33 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed, 21 May 2014 12:39:33 -0400 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <537C0BAB.5050700@ucdavis.edu> References: <537C0BAB.5050700@ucdavis.edu> Message-ID: <20140521163933.GA4026@seas.upenn.edu> Others have given examples of implementing this using a fold. I'd like to point out something else: by representing all these prices and volumes etc. as a bare numeric type, you are simply asking for trouble! The reason is that it allows many nonsensical operations. For example, you could add a price and a volume. Adding a price and a volume makes no sense, but if they are the same type then the compiler cannot help you catch such a silly mistake. I would do something like this: {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Price = Price Double -- you could also do newtype Price a = Price a if you want the -- flexibility to be able to use any numeric type, though it's probably not necessary. newtype Volume = Volume Double deriving Num newtype Cost = Cost Double deriving Num Notice I made a third type Cost, which is the result of multiplying a Price by a Volume. If I understand the domain correctly, multiplying a Price by a Volume does not give you another Price (for example, would it make sense to multiply a Price by a Volume, and then take the result and multiply it by another Volume?). A Price represents the value of a single share or unit of currency, whereas a Cost just represents some arbitrary amount of money. Now, what sorts of operations can one do on these types? Notice I put "deriving Num" after Volume and Cost, which means that two Volumes can be added or subtracted to give another Volume, and similarly for Cost (unfortunately, it means they can also be multiplied, which is probably not sensible, but that's more a failing of the Num class which is not granular enough). We also should implement (.*) :: Price -> Volume -> Cost Price p .* Volume v = Cost (p * v) And now you can implement essentially any of the suggested solutions, but with more descriptive types like aggregate :: [(Price, Volume)] -> [(Cost, Volume)] and using (.*) in the key place instead of (*). And now the type checker will make sure you don't do silly things like add a Price and a Volume, or multiply a Cost by a Price! Hooray! -Brent On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: > Awesome haskellers, > > I am coding up a little function that aggregates "ask orders" in a > currency exchange. > Another way to look at it, is that the function takes as input a > histogram or fdf (in list format) and outputs the cumulative > distribution cdf (also in list format). So we are kind of > "integrating" the input list. > > When given a list of asks in order of increasing price, the function > returns a list of points in the graph of the total supply curve. > > Here's an example: > > asks: returned list: > > [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), > (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), > (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] > > the returned list gives us the total supply curve (price = y-axis, > quantity/volume = x-axis, so the order is flipped) > > Summarizing > > * We're adding up the volumes. The last volume on the list is the > total volume available for sale. > * We calculate the total amount to be paid to buy the current volume > (for each item in the list). > > I have written up a simple function to do this: > > aggregate :: Num a => [(a,a)] -> [(a,a)] > aggregate xs = aggregate' 0 0 xs > > aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] > aggregate' _ _ [] = [] > aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y > accY' = accY + y > > in (accX',accY') : aggregate' > accX' accY' ls > > > main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] > > However, this does not look very good to me and it feels like I'm > reinventing the wheel. > > Question: Is there a better Haskell way to do this? I'm really anal > about making it easy to read. > > Thanks! > > Dimitri > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From defigueiredo at ucdavis.edu Wed May 21 16:51:24 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 21 May 2014 10:51:24 -0600 Subject: [Haskell-beginners] Instance declaration needs more info? In-Reply-To: References: <537C6F83.9090103@ucdavis.edu> Message-ID: <537CD98C.8050809@ucdavis.edu> Got it. I was mistaken in what the final "Unit c =>" meant. Thanks Bob! Em 21/05/14 03:28, Bob Ippolito escreveu: > The trouble is that your specification says that Unit c is the return > type (the caller can choose any Unit instance), but this > implementation can only evaluate to a Prelude.Double. One way to solve > this is to add a fromDouble :: Double -> Unit a and wrap the > expression with that in order to satisfy Unit c. > > On Wednesday, May 21, 2014, Dimitri DeFigueiredo > > wrote: > > Hi All, > > I'm trying to write a simplified dimensional library where where > quantities in meters, seconds and meters/second can all co-exist > adjusting their respective units when multiplied and/or divided. > > Also, meter+meter is allowed, but meter+second should cause the > type checker to complain. > > This is a bit like a *much* simplified version of the Units > library. However, I am having trouble understanding why my > instance declaration below appears to be under specified. Here's > the code: > > ------------ > module Dimensional where > import qualified Prelude > > -- A Group allows you to add and subtract (but not multiply or divide) > class Group a where > > (+) :: a -> a -> a > > (-) :: a -> a -> a > x - y = x + negate y > > negate :: a -> a > negate x = fromInteger 0 - x > > fromInteger :: Prelude.Integer -> a > > class Unit a where > > (*) :: (Unit a, Unit b, Unit c) => a -> b -> c > (/) :: (Unit a, Unit b, Unit c) => a -> b -> c > toDouble :: a -> Prelude.Double > > instance Unit Prelude.Double where > > (*) x y = (Prelude.*) x (toDouble y) -- <----- Error here > (/) x y = (Prelude./) x (toDouble y) > toDouble x = x > > ------------ > GHC complains as follows: > > Could not deduce (c ~ Prelude.Double) > from the context (Unit Prelude.Double, Unit b, Unit c) > bound by the type signature for > * :: (Unit Prelude.Double, Unit b, Unit c) => > Prelude.Double -> b -> c > at /code/haskell/dimensional.hs:25:5-43 > `c' is a rigid type variable bound by > the type signature for > * :: (Unit Prelude.Double, Unit b, Unit c) => > Prelude.Double -> b -> c > at /code/haskell/dimensional.hs:25:5 > In the first argument of `(Prelude.*)', namely `x' > In the expression: (Prelude.*) x (toDouble y) > In an equation for `*': * x y = (Prelude.*) x (toDouble y) > > Any pointers would be much appreciated! > > Thanks, > > Dimitri > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Wed May 21 16:57:39 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 21 May 2014 10:57:39 -0600 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <20140521163933.GA4026@seas.upenn.edu> References: <537C0BAB.5050700@ucdavis.edu> <20140521163933.GA4026@seas.upenn.edu> Message-ID: <537CDB03.5020200@ucdavis.edu> Thanks, the realization that price, cost and volume are different quantities is exactly what led me to play around with the Units library. I have a problem with Num. Shouldn't it be broken up into the fundamentals of abstract algebra: Group, Ring, Field, etc? Just like is done for Functors, Applicative Functors, Monads, etc. It would avoid having the typechecker allow me to multiply a Meter by Meter to get another Meter (instead of the correct unit Meter^2). Cheers, Dimitri Em 21/05/14 10:39, Brent Yorgey escreveu: > Others have given examples of implementing this using a fold. I'd > like to point out something else: by representing all these prices and > volumes etc. as a bare numeric type, you are simply asking for > trouble! The reason is that it allows many nonsensical operations. > For example, you could add a price and a volume. Adding a price and a > volume makes no sense, but if they are the same type then the compiler > cannot help you catch such a silly mistake. > > I would do something like this: > > {-# LANGUAGE GeneralizedNewtypeDeriving #-} > > newtype Price = Price Double > -- you could also do newtype Price a = Price a if you want the > -- flexibility to be able to use any numeric type, though it's > probably not necessary. > > newtype Volume = Volume Double > deriving Num > newtype Cost = Cost Double > deriving Num > > Notice I made a third type Cost, which is the result of multiplying a > Price by a Volume. If I understand the domain correctly, multiplying > a Price by a Volume does not give you another Price (for example, > would it make sense to multiply a Price by a Volume, and then take the > result and multiply it by another Volume?). A Price represents the > value of a single share or unit of currency, whereas a Cost just > represents some arbitrary amount of money. > > Now, what sorts of operations can one do on these types? Notice I put > "deriving Num" after Volume and Cost, which means that two Volumes can > be added or subtracted to give another Volume, and similarly for Cost > (unfortunately, it means they can also be multiplied, which is > probably not sensible, but that's more a failing of the Num class > which is not granular enough). We also should implement > > (.*) :: Price -> Volume -> Cost > Price p .* Volume v = Cost (p * v) > > And now you can implement essentially any of the suggested solutions, > but with more descriptive types like > > aggregate :: [(Price, Volume)] -> [(Cost, Volume)] > > and using (.*) in the key place instead of (*). And now the type > checker will make sure you don't do silly things like add a Price and > a Volume, or multiply a Cost by a Price! Hooray! > > -Brent > > On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: >> Awesome haskellers, >> >> I am coding up a little function that aggregates "ask orders" in a >> currency exchange. >> Another way to look at it, is that the function takes as input a >> histogram or fdf (in list format) and outputs the cumulative >> distribution cdf (also in list format). So we are kind of >> "integrating" the input list. >> >> When given a list of asks in order of increasing price, the function >> returns a list of points in the graph of the total supply curve. >> >> Here's an example: >> >> asks: returned list: >> >> [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), >> (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), >> (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] >> >> the returned list gives us the total supply curve (price = y-axis, >> quantity/volume = x-axis, so the order is flipped) >> >> Summarizing >> >> * We're adding up the volumes. The last volume on the list is the >> total volume available for sale. >> * We calculate the total amount to be paid to buy the current volume >> (for each item in the list). >> >> I have written up a simple function to do this: >> >> aggregate :: Num a => [(a,a)] -> [(a,a)] >> aggregate xs = aggregate' 0 0 xs >> >> aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] >> aggregate' _ _ [] = [] >> aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y >> accY' = accY + y >> >> in (accX',accY') : aggregate' >> accX' accY' ls >> >> >> main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] >> >> However, this does not look very good to me and it feels like I'm >> reinventing the wheel. >> >> Question: Is there a better Haskell way to do this? I'm really anal >> about making it easy to read. >> >> Thanks! >> >> Dimitri >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From davidleothomas at gmail.com Wed May 21 17:01:22 2014 From: davidleothomas at gmail.com (David Thomas) Date: Wed, 21 May 2014 10:01:22 -0700 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <537CDB03.5020200@ucdavis.edu> References: <537C0BAB.5050700@ucdavis.edu> <20140521163933.GA4026@seas.upenn.edu> <537CDB03.5020200@ucdavis.edu> Message-ID: Yes, it "should" - in that it would be more expressive and allow types to catch more errors. The current situation trades that away for reduced complexity. Make whatever judgement call you want regarding whether that was appropriate. On Wed, May 21, 2014 at 9:57 AM, Dimitri DeFigueiredo wrote: > Thanks, the realization that price, cost and volume are different quantities > is exactly what led me to play around with the Units library. > > I have a problem with Num. Shouldn't it be broken up into the fundamentals > of abstract algebra: Group, Ring, Field, etc? Just like is done for > Functors, > Applicative Functors, Monads, etc. It would avoid having the > typechecker allow me to multiply a Meter by Meter to get another Meter > (instead of the correct unit Meter^2). > > Cheers, > > Dimitri > > > Em 21/05/14 10:39, Brent Yorgey escreveu: > >> Others have given examples of implementing this using a fold. I'd >> like to point out something else: by representing all these prices and >> volumes etc. as a bare numeric type, you are simply asking for >> trouble! The reason is that it allows many nonsensical operations. >> For example, you could add a price and a volume. Adding a price and a >> volume makes no sense, but if they are the same type then the compiler >> cannot help you catch such a silly mistake. >> >> I would do something like this: >> >> {-# LANGUAGE GeneralizedNewtypeDeriving #-} >> >> newtype Price = Price Double >> -- you could also do newtype Price a = Price a if you want the >> -- flexibility to be able to use any numeric type, though it's >> probably not necessary. >> >> newtype Volume = Volume Double >> deriving Num >> newtype Cost = Cost Double >> deriving Num >> >> Notice I made a third type Cost, which is the result of multiplying a >> Price by a Volume. If I understand the domain correctly, multiplying >> a Price by a Volume does not give you another Price (for example, >> would it make sense to multiply a Price by a Volume, and then take the >> result and multiply it by another Volume?). A Price represents the >> value of a single share or unit of currency, whereas a Cost just >> represents some arbitrary amount of money. >> >> Now, what sorts of operations can one do on these types? Notice I put >> "deriving Num" after Volume and Cost, which means that two Volumes can >> be added or subtracted to give another Volume, and similarly for Cost >> (unfortunately, it means they can also be multiplied, which is >> probably not sensible, but that's more a failing of the Num class >> which is not granular enough). We also should implement >> >> (.*) :: Price -> Volume -> Cost >> Price p .* Volume v = Cost (p * v) >> >> And now you can implement essentially any of the suggested solutions, >> but with more descriptive types like >> >> aggregate :: [(Price, Volume)] -> [(Cost, Volume)] >> >> and using (.*) in the key place instead of (*). And now the type >> checker will make sure you don't do silly things like add a Price and >> a Volume, or multiply a Cost by a Price! Hooray! >> >> -Brent >> >> On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: >>> >>> Awesome haskellers, >>> >>> I am coding up a little function that aggregates "ask orders" in a >>> currency exchange. >>> Another way to look at it, is that the function takes as input a >>> histogram or fdf (in list format) and outputs the cumulative >>> distribution cdf (also in list format). So we are kind of >>> "integrating" the input list. >>> >>> When given a list of asks in order of increasing price, the function >>> returns a list of points in the graph of the total supply curve. >>> >>> Here's an example: >>> >>> asks: returned list: >>> >>> [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), >>> (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), >>> (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] >>> >>> the returned list gives us the total supply curve (price = y-axis, >>> quantity/volume = x-axis, so the order is flipped) >>> >>> Summarizing >>> >>> * We're adding up the volumes. The last volume on the list is the >>> total volume available for sale. >>> * We calculate the total amount to be paid to buy the current volume >>> (for each item in the list). >>> >>> I have written up a simple function to do this: >>> >>> aggregate :: Num a => [(a,a)] -> [(a,a)] >>> aggregate xs = aggregate' 0 0 xs >>> >>> aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] >>> aggregate' _ _ [] = [] >>> aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y >>> accY' = accY + y >>> >>> in (accX',accY') : aggregate' >>> accX' accY' ls >>> >>> >>> main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] >>> >>> However, this does not look very good to me and it feels like I'm >>> reinventing the wheel. >>> >>> Question: Is there a better Haskell way to do this? I'm really anal >>> about making it easy to read. >>> >>> Thanks! >>> >>> Dimitri >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From matthewjwilliams101 at gmail.com Wed May 21 18:46:34 2014 From: matthewjwilliams101 at gmail.com (MJ Williams) Date: Wed, 21 May 2014 19:46:34 +0100 Subject: [Haskell-beginners] haskell on OpenBSD 5.5 In-Reply-To: <1400613020.10632.YahooMailNeo@web163102.mail.bf1.yahoo.com > References: <1400613020.10632.YahooMailNeo@web163102.mail.bf1.yahoo.com> Message-ID: <537cf489.2315c20a.21e1.07a0@mx.google.com> Any reason for installing OpenBSD and not NetBSD or FreeBSD? I think you can run any Linux package including ghc on both of those. At 20:10 20/05/2014, you wrote: >Good day all, > >I installed OpenBSD 5.5 in order to learn haskell. >There are a few glitches however. I installed the haskell-platform >meta-package and the issues I noticed are: >- hs-vector wouldn't install >- ghci wouldn't start, the message: unable to load package `integer-gmp' > >Has anyone experienced the same issue? Is this happening on OpenBSD 5.4? >Is this OpenBSD specific? > >Thanks in advance. > > > >_______________________________________________ >Beginners mailing list >Beginners at haskell.org >http://www.haskell.org/mailman/listinfo/beginners From giacomo at tesio.it Wed May 21 20:03:01 2014 From: giacomo at tesio.it (Giacomo Tesio) Date: Wed, 21 May 2014 22:03:01 +0200 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: References: <537C0BAB.5050700@ucdavis.edu> <20140521163933.GA4026@seas.upenn.edu> <537CDB03.5020200@ucdavis.edu> Message-ID: Actually I wonder why we don't have both Num and Group, Ring, Field, etc... With GeneralizedNewtypeDeriving it would be awesome! Giacomo On Wed, May 21, 2014 at 7:01 PM, David Thomas wrote: > Yes, it "should" - in that it would be more expressive and allow types > to catch more errors. The current situation trades that away for > reduced complexity. Make whatever judgement call you want regarding > whether that was appropriate. > > On Wed, May 21, 2014 at 9:57 AM, Dimitri DeFigueiredo > wrote: > > Thanks, the realization that price, cost and volume are different > quantities > > is exactly what led me to play around with the Units library. > > > > I have a problem with Num. Shouldn't it be broken up into the > fundamentals > > of abstract algebra: Group, Ring, Field, etc? Just like is done for > > Functors, > > Applicative Functors, Monads, etc. It would avoid having the > > typechecker allow me to multiply a Meter by Meter to get another Meter > > (instead of the correct unit Meter^2). > > > > Cheers, > > > > Dimitri > > > > > > Em 21/05/14 10:39, Brent Yorgey escreveu: > > > >> Others have given examples of implementing this using a fold. I'd > >> like to point out something else: by representing all these prices and > >> volumes etc. as a bare numeric type, you are simply asking for > >> trouble! The reason is that it allows many nonsensical operations. > >> For example, you could add a price and a volume. Adding a price and a > >> volume makes no sense, but if they are the same type then the compiler > >> cannot help you catch such a silly mistake. > >> > >> I would do something like this: > >> > >> {-# LANGUAGE GeneralizedNewtypeDeriving #-} > >> > >> newtype Price = Price Double > >> -- you could also do newtype Price a = Price a if you want the > >> -- flexibility to be able to use any numeric type, though it's > >> probably not necessary. > >> > >> newtype Volume = Volume Double > >> deriving Num > >> newtype Cost = Cost Double > >> deriving Num > >> > >> Notice I made a third type Cost, which is the result of multiplying a > >> Price by a Volume. If I understand the domain correctly, multiplying > >> a Price by a Volume does not give you another Price (for example, > >> would it make sense to multiply a Price by a Volume, and then take the > >> result and multiply it by another Volume?). A Price represents the > >> value of a single share or unit of currency, whereas a Cost just > >> represents some arbitrary amount of money. > >> > >> Now, what sorts of operations can one do on these types? Notice I put > >> "deriving Num" after Volume and Cost, which means that two Volumes can > >> be added or subtracted to give another Volume, and similarly for Cost > >> (unfortunately, it means they can also be multiplied, which is > >> probably not sensible, but that's more a failing of the Num class > >> which is not granular enough). We also should implement > >> > >> (.*) :: Price -> Volume -> Cost > >> Price p .* Volume v = Cost (p * v) > >> > >> And now you can implement essentially any of the suggested solutions, > >> but with more descriptive types like > >> > >> aggregate :: [(Price, Volume)] -> [(Cost, Volume)] > >> > >> and using (.*) in the key place instead of (*). And now the type > >> checker will make sure you don't do silly things like add a Price and > >> a Volume, or multiply a Cost by a Price! Hooray! > >> > >> -Brent > >> > >> On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: > >>> > >>> Awesome haskellers, > >>> > >>> I am coding up a little function that aggregates "ask orders" in a > >>> currency exchange. > >>> Another way to look at it, is that the function takes as input a > >>> histogram or fdf (in list format) and outputs the cumulative > >>> distribution cdf (also in list format). So we are kind of > >>> "integrating" the input list. > >>> > >>> When given a list of asks in order of increasing price, the function > >>> returns a list of points in the graph of the total supply curve. > >>> > >>> Here's an example: > >>> > >>> asks: returned list: > >>> > >>> [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), > >>> (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), > >>> (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] > >>> > >>> the returned list gives us the total supply curve (price = y-axis, > >>> quantity/volume = x-axis, so the order is flipped) > >>> > >>> Summarizing > >>> > >>> * We're adding up the volumes. The last volume on the list is the > >>> total volume available for sale. > >>> * We calculate the total amount to be paid to buy the current volume > >>> (for each item in the list). > >>> > >>> I have written up a simple function to do this: > >>> > >>> aggregate :: Num a => [(a,a)] -> [(a,a)] > >>> aggregate xs = aggregate' 0 0 xs > >>> > >>> aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] > >>> aggregate' _ _ [] = [] > >>> aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y > >>> accY' = accY + y > >>> > >>> in (accX',accY') : aggregate' > >>> accX' accY' ls > >>> > >>> > >>> main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] > >>> > >>> However, this does not look very good to me and it feels like I'm > >>> reinventing the wheel. > >>> > >>> Question: Is there a better Haskell way to do this? I'm really anal > >>> about making it easy to read. > >>> > >>> Thanks! > >>> > >>> Dimitri > >>> _______________________________________________ > >>> Beginners mailing list > >>> Beginners at haskell.org > >>> http://www.haskell.org/mailman/listinfo/beginners > >> > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://www.haskell.org/mailman/listinfo/beginners > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin.drautzburg at web.de Wed May 21 20:09:24 2014 From: martin.drautzburg at web.de (martin) Date: Wed, 21 May 2014 22:09:24 +0200 Subject: [Haskell-beginners] Project euler question Message-ID: <537D07F4.3000606@web.de> Hello all, I tried to solve Problem 24 (https://projecteuler.net/problem=24) and came up with the following solution: import Data.List.Ordered import Data.Char elems = [0,1,2,3,4,5,6,7,8,9] :: [Int] x = do a <- elems b <- elems `without` [a] c <- elems `without` [a,b] d <- elems `without` [a,b,c] e <- elems `without` [a,b,c,d] f <- elems `without` [a,b,c,d,e] g <- elems `without` [a,b,c,d,e,f] h <- elems `without` [a,b,c,d,e,f,g] i <- elems `without` [a,b,c,d,e,f,g,h] j <- elems `without` [a,b,c,d,e,f,g,h,i] return [a,b,c,d,e,f,g,h,i,j] without a b = minus a ( sort b) solution = filter isDigit $ show $ (x !! 1000001) -- "2783915640" PE tells me that this is wrong, and I peeked the correct answer, which is 2783915460 (the 4 and 6 are swapped). So I tried to find out where the correct answer is in my list x and added y = filter (\(x,y) -> x == "2783915460") $ zip (map (filter isDigit . show) x) [1..] -- [("2783915460",1000000)] How can that be? "solution" tells me that the millionth element is "2783915640" but "y" tells me that "2783915460" is at the millionth position? I just cannot see it. From cma at bitemyapp.com Wed May 21 20:12:35 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Wed, 21 May 2014 15:12:35 -0500 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: References: <537C0BAB.5050700@ucdavis.edu> <20140521163933.GA4026@seas.upenn.edu> <537CDB03.5020200@ucdavis.edu> Message-ID: > Actually I wonder why we don't have both Num and Group, Ring, Field, etc... There are mathy preludes if that's what you're into. On Wed, May 21, 2014 at 3:03 PM, Giacomo Tesio wrote: > Actually I wonder why we don't have both Num and Group, Ring, Field, > etc... > > With GeneralizedNewtypeDeriving it would be awesome! > > Giacomo > > > On Wed, May 21, 2014 at 7:01 PM, David Thomas wrote: > >> Yes, it "should" - in that it would be more expressive and allow types >> to catch more errors. The current situation trades that away for >> reduced complexity. Make whatever judgement call you want regarding >> whether that was appropriate. >> >> On Wed, May 21, 2014 at 9:57 AM, Dimitri DeFigueiredo >> wrote: >> > Thanks, the realization that price, cost and volume are different >> quantities >> > is exactly what led me to play around with the Units library. >> > >> > I have a problem with Num. Shouldn't it be broken up into the >> fundamentals >> > of abstract algebra: Group, Ring, Field, etc? Just like is done for >> > Functors, >> > Applicative Functors, Monads, etc. It would avoid having the >> > typechecker allow me to multiply a Meter by Meter to get another Meter >> > (instead of the correct unit Meter^2). >> > >> > Cheers, >> > >> > Dimitri >> > >> > >> > Em 21/05/14 10:39, Brent Yorgey escreveu: >> > >> >> Others have given examples of implementing this using a fold. I'd >> >> like to point out something else: by representing all these prices and >> >> volumes etc. as a bare numeric type, you are simply asking for >> >> trouble! The reason is that it allows many nonsensical operations. >> >> For example, you could add a price and a volume. Adding a price and a >> >> volume makes no sense, but if they are the same type then the compiler >> >> cannot help you catch such a silly mistake. >> >> >> >> I would do something like this: >> >> >> >> {-# LANGUAGE GeneralizedNewtypeDeriving #-} >> >> >> >> newtype Price = Price Double >> >> -- you could also do newtype Price a = Price a if you want the >> >> -- flexibility to be able to use any numeric type, though it's >> >> probably not necessary. >> >> >> >> newtype Volume = Volume Double >> >> deriving Num >> >> newtype Cost = Cost Double >> >> deriving Num >> >> >> >> Notice I made a third type Cost, which is the result of multiplying a >> >> Price by a Volume. If I understand the domain correctly, multiplying >> >> a Price by a Volume does not give you another Price (for example, >> >> would it make sense to multiply a Price by a Volume, and then take the >> >> result and multiply it by another Volume?). A Price represents the >> >> value of a single share or unit of currency, whereas a Cost just >> >> represents some arbitrary amount of money. >> >> >> >> Now, what sorts of operations can one do on these types? Notice I put >> >> "deriving Num" after Volume and Cost, which means that two Volumes can >> >> be added or subtracted to give another Volume, and similarly for Cost >> >> (unfortunately, it means they can also be multiplied, which is >> >> probably not sensible, but that's more a failing of the Num class >> >> which is not granular enough). We also should implement >> >> >> >> (.*) :: Price -> Volume -> Cost >> >> Price p .* Volume v = Cost (p * v) >> >> >> >> And now you can implement essentially any of the suggested solutions, >> >> but with more descriptive types like >> >> >> >> aggregate :: [(Price, Volume)] -> [(Cost, Volume)] >> >> >> >> and using (.*) in the key place instead of (*). And now the type >> >> checker will make sure you don't do silly things like add a Price and >> >> a Volume, or multiply a Cost by a Price! Hooray! >> >> >> >> -Brent >> >> >> >> On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: >> >>> >> >>> Awesome haskellers, >> >>> >> >>> I am coding up a little function that aggregates "ask orders" in a >> >>> currency exchange. >> >>> Another way to look at it, is that the function takes as input a >> >>> histogram or fdf (in list format) and outputs the cumulative >> >>> distribution cdf (also in list format). So we are kind of >> >>> "integrating" the input list. >> >>> >> >>> When given a list of asks in order of increasing price, the function >> >>> returns a list of points in the graph of the total supply curve. >> >>> >> >>> Here's an example: >> >>> >> >>> asks: returned list: >> >>> >> >>> [ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), >> >>> (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), >> >>> (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] >> >>> >> >>> the returned list gives us the total supply curve (price = y-axis, >> >>> quantity/volume = x-axis, so the order is flipped) >> >>> >> >>> Summarizing >> >>> >> >>> * We're adding up the volumes. The last volume on the list is the >> >>> total volume available for sale. >> >>> * We calculate the total amount to be paid to buy the current volume >> >>> (for each item in the list). >> >>> >> >>> I have written up a simple function to do this: >> >>> >> >>> aggregate :: Num a => [(a,a)] -> [(a,a)] >> >>> aggregate xs = aggregate' 0 0 xs >> >>> >> >>> aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] >> >>> aggregate' _ _ [] = [] >> >>> aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y >> >>> accY' = accY + y >> >>> >> >>> in (accX',accY') : aggregate' >> >>> accX' accY' ls >> >>> >> >>> >> >>> main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] >> >>> >> >>> However, this does not look very good to me and it feels like I'm >> >>> reinventing the wheel. >> >>> >> >>> Question: Is there a better Haskell way to do this? I'm really anal >> >>> about making it easy to read. >> >>> >> >>> Thanks! >> >>> >> >>> Dimitri >> >>> _______________________________________________ >> >>> Beginners mailing list >> >>> Beginners at haskell.org >> >>> http://www.haskell.org/mailman/listinfo/beginners >> >> >> >> _______________________________________________ >> >> Beginners mailing list >> >> Beginners at haskell.org >> >> http://www.haskell.org/mailman/listinfo/beginners >> > >> > >> > _______________________________________________ >> > Beginners mailing list >> > Beginners at haskell.org >> > http://www.haskell.org/mailman/listinfo/beginners >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed May 21 21:10:41 2014 From: toad3k at gmail.com (David McBride) Date: Wed, 21 May 2014 17:10:41 -0400 Subject: [Haskell-beginners] Project euler question In-Reply-To: <537D07F4.3000606@web.de> References: <537D07F4.3000606@web.de> Message-ID: For what it is worth, I'm getting the same answer as you are. > head $ drop (1000000-1) $ sort $ Data.List.permutations [0..9] [2,7,8,3,9,1,5,4,6,0] >(sort $ Data.List.permutations [0..9]) !! (1000000-1) [2,7,8,3,9,1,5,4,6,0] I guess either euler is wrong or we are both crazy. On Wed, May 21, 2014 at 4:09 PM, martin wrote: > Hello all, > > I tried to solve Problem 24 (https://projecteuler.net/problem=24) and > came up with the following solution: > > import Data.List.Ordered > import Data.Char > > elems = [0,1,2,3,4,5,6,7,8,9] :: [Int] > > x = do > a <- elems > b <- elems `without` [a] > c <- elems `without` [a,b] > d <- elems `without` [a,b,c] > e <- elems `without` [a,b,c,d] > f <- elems `without` [a,b,c,d,e] > g <- elems `without` [a,b,c,d,e,f] > h <- elems `without` [a,b,c,d,e,f,g] > i <- elems `without` [a,b,c,d,e,f,g,h] > j <- elems `without` [a,b,c,d,e,f,g,h,i] > return [a,b,c,d,e,f,g,h,i,j] > > without a b = minus a ( sort b) > > solution = filter isDigit $ show $ (x !! 1000001) > -- "2783915640" > > PE tells me that this is wrong, and I peeked the correct answer, which is > 2783915460 (the 4 and 6 are swapped). So I > tried to find out where the correct answer is in my list x and added > > y = filter (\(x,y) -> x == "2783915460") $ zip (map (filter isDigit . > show) x) [1..] > -- [("2783915460",1000000)] > > How can that be? "solution" tells me that the millionth element is > "2783915640" but "y" tells me that "2783915460" is at > the millionth position? I just cannot see it. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed May 21 21:14:14 2014 From: toad3k at gmail.com (David McBride) Date: Wed, 21 May 2014 17:14:14 -0400 Subject: [Haskell-beginners] Project euler question In-Reply-To: References: <537D07F4.3000606@web.de> Message-ID: Err actually I guess I got the euler answer, I guess I don't understand your solution without the "minus" function definition. On Wed, May 21, 2014 at 5:10 PM, David McBride wrote: > For what it is worth, I'm getting the same answer as you are. > > > head $ drop (1000000-1) $ sort $ Data.List.permutations [0..9] > [2,7,8,3,9,1,5,4,6,0] > > >(sort $ Data.List.permutations [0..9]) !! (1000000-1) > [2,7,8,3,9,1,5,4,6,0] > > I guess either euler is wrong or we are both crazy. > > > On Wed, May 21, 2014 at 4:09 PM, martin wrote: > >> Hello all, >> >> I tried to solve Problem 24 (https://projecteuler.net/problem=24) and >> came up with the following solution: >> >> import Data.List.Ordered >> import Data.Char >> >> elems = [0,1,2,3,4,5,6,7,8,9] :: [Int] >> >> x = do >> a <- elems >> b <- elems `without` [a] >> c <- elems `without` [a,b] >> d <- elems `without` [a,b,c] >> e <- elems `without` [a,b,c,d] >> f <- elems `without` [a,b,c,d,e] >> g <- elems `without` [a,b,c,d,e,f] >> h <- elems `without` [a,b,c,d,e,f,g] >> i <- elems `without` [a,b,c,d,e,f,g,h] >> j <- elems `without` [a,b,c,d,e,f,g,h,i] >> return [a,b,c,d,e,f,g,h,i,j] >> >> without a b = minus a ( sort b) >> >> solution = filter isDigit $ show $ (x !! 1000001) >> -- "2783915640" >> >> PE tells me that this is wrong, and I peeked the correct answer, which is >> 2783915460 (the 4 and 6 are swapped). So I >> tried to find out where the correct answer is in my list x and added >> >> y = filter (\(x,y) -> x == "2783915460") $ zip (map (filter isDigit . >> show) x) [1..] >> -- [("2783915460",1000000)] >> >> How can that be? "solution" tells me that the millionth element is >> "2783915640" but "y" tells me that "2783915460" is at >> the millionth position? I just cannot see it. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.is.fischer at googlemail.com Wed May 21 21:18:15 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Wed, 21 May 2014 23:18:15 +0200 Subject: [Haskell-beginners] Project euler question In-Reply-To: <537D07F4.3000606@web.de> References: <537D07F4.3000606@web.de> Message-ID: <1818062.CPUOuW5Maq@linux-hpeb.site> On Wednesday 21 May 2014, 22:09:24, martin wrote: > Hello all, > > I tried to solve Problem 24 (https://projecteuler.net/problem=24) and came > up with the following solution: > > solution = filter isDigit $ show $ (x !! 1000001) > -- "2783915640" > > PE tells me that this is wrong, Yes, you took the 1000002nd element. List indexing is 0-based, so the millionth element is at index (1000000 - 1), not (1000000 + 1). From msherman77 at yahoo.com Thu May 22 00:17:13 2014 From: msherman77 at yahoo.com (Michael S) Date: Wed, 21 May 2014 17:17:13 -0700 (PDT) Subject: [Haskell-beginners] haskell on OpenBSD 5.5 In-Reply-To: <537cf489.2315c20a.21e1.07a0@mx.google.com> References: <1400613020.10632.YahooMailNeo@web163102.mail.bf1.yahoo.com> <537cf489.2315c20a.21e1.07a0@mx.google.com> Message-ID: <1400717833.25977.YahooMailNeo@web163101.mail.bf1.yahoo.com> I never? tried? NetBSD, however my initial thought was to install FreeBSD. Unfortunately FreeBSD installer did not pick up the built-in SATA controller and therefore I couldn't install it. OpenBSD installed withouth a hitch. No need to run Linux packages, they all have native ghc and many other related packages. The issue I asked about is not present on OpenBSD amd64 port. On Wednesday, May 21, 2014 2:46:35 PM, MJ Williams wrote: Any reason for installing OpenBSD and not NetBSD or FreeBSD? I think you can run any Linux package including ghc on both of those. At 20:10 20/05/2014, you wrote: >Good day all, > >I installed OpenBSD 5.5 in order to learn haskell. >There are a few glitches however. I installed the haskell-platform >meta-package and the issues I noticed are: >- hs-vector wouldn't install >- ghci wouldn't start, the message: unable to load package `integer-gmp' > >Has anyone experienced the same issue? Is this happening on OpenBSD 5.4? >Is this OpenBSD specific? > >Thanks in advance. > > > >_______________________________________________ >Beginners mailing list >Beginners at haskell.org >http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin.drautzburg at web.de Thu May 22 06:36:48 2014 From: martin.drautzburg at web.de (martin) Date: Thu, 22 May 2014 08:36:48 +0200 Subject: [Haskell-beginners] Project euler question In-Reply-To: <1818062.CPUOuW5Maq@linux-hpeb.site> References: <537D07F4.3000606@web.de> <1818062.CPUOuW5Maq@linux-hpeb.site> Message-ID: <537D9B00.2050805@web.de> Am 05/21/2014 11:18 PM, schrieb Daniel Fischer: > On Wednesday 21 May 2014, 22:09:24, martin wrote: >> Hello all, >> >> I tried to solve Problem 24 (https://projecteuler.net/problem=24) and came >> up with the following solution: > >> >> solution = filter isDigit $ show $ (x !! 1000001) >> -- "2783915640" >> >> PE tells me that this is wrong, > > Yes, you took the 1000002nd element. List indexing is 0-based, so the > millionth element is at index (1000000 - 1), not (1000000 + 1). Oops. From martin.drautzburg at web.de Thu May 22 06:47:12 2014 From: martin.drautzburg at web.de (martin) Date: Thu, 22 May 2014 08:47:12 +0200 Subject: [Haskell-beginners] Project euler question In-Reply-To: References: <537D07F4.3000606@web.de> Message-ID: <537D9D70.4060209@web.de> Am 05/21/2014 11:14 PM, schrieb David McBride: > Err actually I guess I got the euler answer, I guess I don't understand your solution without the "minus" function > definition. "minus" is from Data.List.Ordered. It it like the standard set operation "minus" when both lists are ordered. > > > On Wed, May 21, 2014 at 5:10 PM, David McBride > wrote: > > For what it is worth, I'm getting the same answer as you are. > > > head $ drop (1000000-1) $ sort $ Data.List.permutations [0..9] > [2,7,8,3,9,1,5,4,6,0] > > >(sort $ Data.List.permutations [0..9]) !! (1000000-1) > [2,7,8,3,9,1,5,4,6,0] > > I guess either euler is wrong or we are both crazy. > > > On Wed, May 21, 2014 at 4:09 PM, martin > wrote: > > Hello all, > > I tried to solve Problem 24 (https://projecteuler.net/problem=24) and came up with the following solution: > > import Data.List.Ordered > import Data.Char > > elems = [0,1,2,3,4,5,6,7,8,9] :: [Int] > > x = do > a <- elems > b <- elems `without` [a] > c <- elems `without` [a,b] > d <- elems `without` [a,b,c] > e <- elems `without` [a,b,c,d] > f <- elems `without` [a,b,c,d,e] > g <- elems `without` [a,b,c,d,e,f] > h <- elems `without` [a,b,c,d,e,f,g] > i <- elems `without` [a,b,c,d,e,f,g,h] > j <- elems `without` [a,b,c,d,e,f,g,h,i] > return [a,b,c,d,e,f,g,h,i,j] > > without a b = minus a ( sort b) > > solution = filter isDigit $ show $ (x !! 1000001) > -- "2783915640" > > PE tells me that this is wrong, and I peeked the correct answer, which is 2783915460 (the 4 and 6 are swapped). So I > tried to find out where the correct answer is in my list x and added > > y = filter (\(x,y) -> x == "2783915460") $ zip (map (filter isDigit . show) x) [1..] > -- [("2783915460",1000000)] > > How can that be? "solution" tells me that the millionth element is "2783915640" but "y" tells me that > "2783915460" is at > the millionth position? I just cannot see it. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From byorgey at seas.upenn.edu Thu May 22 17:14:30 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Thu, 22 May 2014 13:14:30 -0400 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <537CDB03.5020200@ucdavis.edu> References: <537C0BAB.5050700@ucdavis.edu> <20140521163933.GA4026@seas.upenn.edu> <537CDB03.5020200@ucdavis.edu> Message-ID: <20140522171430.GA29503@seas.upenn.edu> On Wed, May 21, 2014 at 10:57:39AM -0600, Dimitri DeFigueiredo wrote: > > I have a problem with Num. Shouldn't it be broken up into the fundamentals > of abstract algebra: Group, Ring, Field, etc? It is well-known that the current Num class is not all that great. However, doing things "right" is surprisingly tricky, for several reasons. One is the simple question of how you know when to stop. How finely should concepts be broken down? Should there be separate type classes for Semirings and Semigroups? For Rngs? etc. etc... The other problem is that once you start breaking things down like this you quickly run into problems (1) managing namespaces and (2) the language doesn't provide the right abstraction mechanisms for breaking things down as finely as you might like. For example, many mathematical structures are instances of some class in more than one way (e.g. Integers are Monoids under both sum and product), but the nature of type classes makes this annoying to deal with. You end up littering newtypes everywhere. For attempts in this direction in Haskell, see http://hackage.haskell.org/package/numeric%2Dprelude http://hackage.haskell.org/package/algebra In general, how to structure languages/libraries to make this kind of thing work nicely in practice is an ongoing area of research; see, for example http://www.cas.mcmaster.ca/~carette/publications/tpc.pdf Anyway, my point is not that we *shouldn't* improve the Num type class, we certainly should! My point is just that it's a far more complicated design space than you might think, which helps explain why Num is still the way it is. -Brent > Em 21/05/14 10:39, Brent Yorgey escreveu: > >Others have given examples of implementing this using a fold. I'd > >like to point out something else: by representing all these prices and > >volumes etc. as a bare numeric type, you are simply asking for > >trouble! The reason is that it allows many nonsensical operations. > >For example, you could add a price and a volume. Adding a price and a > >volume makes no sense, but if they are the same type then the compiler > >cannot help you catch such a silly mistake. > > > >I would do something like this: > > > > {-# LANGUAGE GeneralizedNewtypeDeriving #-} > > > > newtype Price = Price Double > > -- you could also do newtype Price a = Price a if you want the > > -- flexibility to be able to use any numeric type, though it's > > probably not necessary. > > > > newtype Volume = Volume Double > > deriving Num > > newtype Cost = Cost Double > > deriving Num > > > >Notice I made a third type Cost, which is the result of multiplying a > >Price by a Volume. If I understand the domain correctly, multiplying > >a Price by a Volume does not give you another Price (for example, > >would it make sense to multiply a Price by a Volume, and then take the > >result and multiply it by another Volume?). A Price represents the > >value of a single share or unit of currency, whereas a Cost just > >represents some arbitrary amount of money. > > > >Now, what sorts of operations can one do on these types? Notice I put > >"deriving Num" after Volume and Cost, which means that two Volumes can > >be added or subtracted to give another Volume, and similarly for Cost > >(unfortunately, it means they can also be multiplied, which is > >probably not sensible, but that's more a failing of the Num class > >which is not granular enough). We also should implement > > > > (.*) :: Price -> Volume -> Cost > > Price p .* Volume v = Cost (p * v) > > > >And now you can implement essentially any of the suggested solutions, > >but with more descriptive types like > > > > aggregate :: [(Price, Volume)] -> [(Cost, Volume)] > > > >and using (.*) in the key place instead of (*). And now the type > >checker will make sure you don't do silly things like add a Price and > >a Volume, or multiply a Cost by a Price! Hooray! > > > >-Brent > > > >On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: > >>Awesome haskellers, > >> > >>I am coding up a little function that aggregates "ask orders" in a > >>currency exchange. > >>Another way to look at it, is that the function takes as input a > >>histogram or fdf (in list format) and outputs the cumulative > >>distribution cdf (also in list format). So we are kind of > >>"integrating" the input list. > >> > >>When given a list of asks in order of increasing price, the function > >>returns a list of points in the graph of the total supply curve. > >> > >>Here's an example: > >> > >>asks: returned list: > >> > >>[ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), > >> (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), > >> (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] > >> > >>the returned list gives us the total supply curve (price = y-axis, > >>quantity/volume = x-axis, so the order is flipped) > >> > >>Summarizing > >> > >>* We're adding up the volumes. The last volume on the list is the > >>total volume available for sale. > >>* We calculate the total amount to be paid to buy the current volume > >>(for each item in the list). > >> > >>I have written up a simple function to do this: > >> > >>aggregate :: Num a => [(a,a)] -> [(a,a)] > >>aggregate xs = aggregate' 0 0 xs > >> > >>aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] > >>aggregate' _ _ [] = [] > >>aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y > >> accY' = accY + y > >> > >> in (accX',accY') : aggregate' > >>accX' accY' ls > >> > >> > >>main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] > >> > >>However, this does not look very good to me and it feels like I'm > >>reinventing the wheel. > >> > >>Question: Is there a better Haskell way to do this? I'm really anal > >>about making it easy to read. > >> > >>Thanks! > >> > >>Dimitri > >>_______________________________________________ > >>Beginners mailing list > >>Beginners at haskell.org > >>http://www.haskell.org/mailman/listinfo/beginners > >_______________________________________________ > >Beginners mailing list > >Beginners at haskell.org > >http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From sapristi at gmail.com Thu May 22 17:50:10 2014 From: sapristi at gmail.com (Albert Cardona) Date: Thu, 22 May 2014 13:50:10 -0400 Subject: [Haskell-beginners] using withAnnotations from Graphics.Rendering.Plot.Figure Message-ID: Hi all, given two Vector and a list of strings, I generate a plot of one vector as a function of the other [1]. At each x,y point, I am trying to place the corresponding string. For the purpose, I zip3 all three sequences, define an annotation function, and invoke it with mapM_ in the context of withAnnotations [2]: withPlot (1, 1) $ do ... let annotation (x, y, label) = text (x + 2, y) (setText label) in withAnnotations $ do mapM_ annotation $ zip3 (toList vx) (toList vy) labels The code above compiles and runs, the plot shows, but no text annotations appear at all. I understand that I am not using the Annote monad correctly: the mapM_ drops every returned value, so the state is not altered. What is the correct way to invoke withAnnotations over a list of (x, y, String) ? Needless to say, I am a beginner in Haskell. Apologies if the above is too trivial. Thanks very much in advance. Albert [1] https://github.com/acardona/haskell-lib/blob/master/CATMAID/Analysis/Graph/Plot.hs [2] http://hackage.haskell.org/package/plot-0.2.3/docs/Graphics-Rendering-Plot-Figure.html#v:withAnnotations PS: unfortunately I had sent this message to haskell-cafe; sorry! -- http://albert.rierol.net http://www.ini.uzh.ch/~acardona/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dedgrant at gmail.com Thu May 22 18:11:44 2014 From: dedgrant at gmail.com (Darren Grant) Date: Thu, 22 May 2014 11:11:44 -0700 Subject: [Haskell-beginners] A better way to "integrate" In-Reply-To: <20140522171430.GA29503@seas.upenn.edu> References: <537C0BAB.5050700@ucdavis.edu> <20140521163933.GA4026@seas.upenn.edu> <537CDB03.5020200@ucdavis.edu> <20140522171430.GA29503@seas.upenn.edu> Message-ID: Isn't symbolic programming better suited to this sort of exploration? It's not so much a final taxonomy of types that need to be visibly quantified, as a gradual exploration of the rules that make connections between concepts. Cheers, Darren On May 22, 2014 10:14 AM, "Brent Yorgey" wrote: > On Wed, May 21, 2014 at 10:57:39AM -0600, Dimitri DeFigueiredo wrote: > > > > I have a problem with Num. Shouldn't it be broken up into the > fundamentals > > of abstract algebra: Group, Ring, Field, etc? > > It is well-known that the current Num class is not all that great. > However, doing things "right" is surprisingly tricky, for several > reasons. One is the simple question of how you know when to stop. > How finely should concepts be broken down? Should there be separate > type classes for Semirings and Semigroups? For Rngs? etc. etc... The > other problem is that once you start breaking things down like this > you quickly run into problems (1) managing namespaces and (2) the > language doesn't provide the right abstraction mechanisms for breaking > things down as finely as you might like. For example, many > mathematical structures are instances of some class in more than one > way (e.g. Integers are Monoids under both sum and product), but the > nature of type classes makes this annoying to deal with. You end up > littering newtypes everywhere. > > For attempts in this direction in Haskell, see > > http://hackage.haskell.org/package/numeric%2Dprelude > http://hackage.haskell.org/package/algebra > > In general, how to structure languages/libraries to make this kind of > thing work nicely in practice is an ongoing area of research; see, for > example > > http://www.cas.mcmaster.ca/~carette/publications/tpc.pdf > > Anyway, my point is not that we *shouldn't* improve the Num type > class, we certainly should! My point is just that it's a far more > complicated design space than you might think, which helps explain why > Num is still the way it is. > > -Brent > > > Em 21/05/14 10:39, Brent Yorgey escreveu: > > >Others have given examples of implementing this using a fold. I'd > > >like to point out something else: by representing all these prices and > > >volumes etc. as a bare numeric type, you are simply asking for > > >trouble! The reason is that it allows many nonsensical operations. > > >For example, you could add a price and a volume. Adding a price and a > > >volume makes no sense, but if they are the same type then the compiler > > >cannot help you catch such a silly mistake. > > > > > >I would do something like this: > > > > > > {-# LANGUAGE GeneralizedNewtypeDeriving #-} > > > > > > newtype Price = Price Double > > > -- you could also do newtype Price a = Price a if you want the > > > -- flexibility to be able to use any numeric type, though it's > > > probably not necessary. > > > > > > newtype Volume = Volume Double > > > deriving Num > > > newtype Cost = Cost Double > > > deriving Num > > > > > >Notice I made a third type Cost, which is the result of multiplying a > > >Price by a Volume. If I understand the domain correctly, multiplying > > >a Price by a Volume does not give you another Price (for example, > > >would it make sense to multiply a Price by a Volume, and then take the > > >result and multiply it by another Volume?). A Price represents the > > >value of a single share or unit of currency, whereas a Cost just > > >represents some arbitrary amount of money. > > > > > >Now, what sorts of operations can one do on these types? Notice I put > > >"deriving Num" after Volume and Cost, which means that two Volumes can > > >be added or subtracted to give another Volume, and similarly for Cost > > >(unfortunately, it means they can also be multiplied, which is > > >probably not sensible, but that's more a failing of the Num class > > >which is not granular enough). We also should implement > > > > > > (.*) :: Price -> Volume -> Cost > > > Price p .* Volume v = Cost (p * v) > > > > > >And now you can implement essentially any of the suggested solutions, > > >but with more descriptive types like > > > > > > aggregate :: [(Price, Volume)] -> [(Cost, Volume)] > > > > > >and using (.*) in the key place instead of (*). And now the type > > >checker will make sure you don't do silly things like add a Price and > > >a Volume, or multiply a Cost by a Price! Hooray! > > > > > >-Brent > > > > > >On Tue, May 20, 2014 at 08:12:59PM -0600, Dimitri DeFigueiredo wrote: > > >>Awesome haskellers, > > >> > > >>I am coding up a little function that aggregates "ask orders" in a > > >>currency exchange. > > >>Another way to look at it, is that the function takes as input a > > >>histogram or fdf (in list format) and outputs the cumulative > > >>distribution cdf (also in list format). So we are kind of > > >>"integrating" the input list. > > >> > > >>When given a list of asks in order of increasing price, the function > > >>returns a list of points in the graph of the total supply curve. > > >> > > >>Here's an example: > > >> > > >>asks: returned list: > > >> > > >>[ (Price 42, Volume 0.5), [ (Price 21, Volume 0.5), > > >> (Price 50, Volume 1 ), (Price 21+50=71, Volume 1.5), > > >> (Price 55, Volume 0.2)] (Price 21+50+11=82,Volume 1.7)] > > >> > > >>the returned list gives us the total supply curve (price = y-axis, > > >>quantity/volume = x-axis, so the order is flipped) > > >> > > >>Summarizing > > >> > > >>* We're adding up the volumes. The last volume on the list is the > > >>total volume available for sale. > > >>* We calculate the total amount to be paid to buy the current volume > > >>(for each item in the list). > > >> > > >>I have written up a simple function to do this: > > >> > > >>aggregate :: Num a => [(a,a)] -> [(a,a)] > > >>aggregate xs = aggregate' 0 0 xs > > >> > > >>aggregate' :: Num a => a -> a -> [(a,a)] -> [(a,a)] > > >>aggregate' _ _ [] = [] > > >>aggregate' accX accY ((x,y):ls) = let accX' = accX + x * y > > >> accY' = accY + y > > >> > > >> in (accX',accY') : aggregate' > > >>accX' accY' ls > > >> > > >> > > >>main = print $ aggregate [(42,0.5),(50,1),(55,0.2)] > > >> > > >>However, this does not look very good to me and it feels like I'm > > >>reinventing the wheel. > > >> > > >>Question: Is there a better Haskell way to do this? I'm really anal > > >>about making it easy to read. > > >> > > >>Thanks! > > >> > > >>Dimitri > > >>_______________________________________________ > > >>Beginners mailing list > > >>Beginners at haskell.org > > >>http://www.haskell.org/mailman/listinfo/beginners > > >_______________________________________________ > > >Beginners mailing list > > >Beginners at haskell.org > > >http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From derek.mcloughlin at gmail.com Mon May 26 09:00:33 2014 From: derek.mcloughlin at gmail.com (Derek McLoughlin) Date: Mon, 26 May 2014 10:00:33 +0100 Subject: [Haskell-beginners] Evaluate function In-Reply-To: References: Message-ID: Reading "Parallel and Concurrent Programming in Haskell", at the bottom of page 27 the author wants to force the evaluation of a list of Strings: evaluate ( length puzzles ) Why not just evaluate puzzles ? https://github.com/simonmar/parconc-examples/blob/master/sudoku4.hs -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.is.fischer at googlemail.com Mon May 26 10:21:54 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Mon, 26 May 2014 12:21:54 +0200 Subject: [Haskell-beginners] Evaluate function In-Reply-To: References: Message-ID: <2021885.39xMIW5ZDr@linux-hpeb.site> On Monday 26 May 2014, 10:00:33, Derek McLoughlin wrote: > Reading "Parallel and Concurrent Programming in Haskell", at the bottom of > page 27 the author wants to force the evaluation of a list of Strings: > > evaluate ( length puzzles ) > > Why not just > > evaluate puzzles > > ? > > https://github.com/simonmar/parconc-examples/blob/master/sudoku4.hs Because "evaluate" means "evaluate to weak head normal form", that is, to the outermost constructor or lambda. evaluate puzzles would evaluate the list just so far that it is known whether the list is empty or not. To evaluate something completely, one needs to evaluate a value that depends on the complete structure. Presumably, to determine the length of the list, one needs to evaluate all paths completely to see whether they lead to a valid puzzle, hence the evaluation is forced by demanding the evaluation of the length. From derek.mcloughlin at gmail.com Mon May 26 13:03:37 2014 From: derek.mcloughlin at gmail.com (Derek McLoughlin) Date: Mon, 26 May 2014 14:03:37 +0100 Subject: [Haskell-beginners] Evaluate function In-Reply-To: <2021885.39xMIW5ZDr@linux-hpeb.site> References: <2021885.39xMIW5ZDr@linux-hpeb.site> Message-ID: Thanks. Reading back, this is actually explained on page 14 - sorry for that. On 26 May 2014 11:22, "Daniel Fischer" wrote: > On Monday 26 May 2014, 10:00:33, Derek McLoughlin wrote: > > Reading "Parallel and Concurrent Programming in Haskell", at the bottom > of > > page 27 the author wants to force the evaluation of a list of Strings: > > > > evaluate ( length puzzles ) > > > > Why not just > > > > evaluate puzzles > > > > ? > > > > https://github.com/simonmar/parconc-examples/blob/master/sudoku4.hs > > Because "evaluate" means "evaluate to weak head normal form", that is, to > the > outermost constructor or lambda. > > evaluate puzzles > > would evaluate the list just so far that it is known whether the list is > empty > or not. > > To evaluate something completely, one needs to evaluate a value that > depends > on the complete structure. Presumably, to determine the length of the list, > one needs to evaluate all paths completely to see whether they lead to a > valid > puzzle, hence the evaluation is forced by demanding the evaluation of the > length. > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Mon May 26 13:21:36 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Mon, 26 May 2014 20:21:36 +0700 Subject: [Haskell-beginners] Evaluate function In-Reply-To: <2021885.39xMIW5ZDr@linux-hpeb.site> References: <2021885.39xMIW5ZDr@linux-hpeb.site> Message-ID: On Mon, May 26, 2014 at 5:21 PM, Daniel Fischer < daniel.is.fischer at googlemail.com> wrote: > To evaluate something completely, one needs to evaluate a value that > depends > on the complete structure. Presumably, to determine the length of the list, > one needs to evaluate all paths completely to see whether they lead to a > valid > puzzle, hence the evaluation is forced by demanding the evaluation of the > length. > To avoid that presumption, you could also write evaluate $ rnf x whenever x is an instance of NFData. The length function strikes me as a kludgy stand-in for rnf when rnf is really what's meant. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Mon May 26 14:25:35 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Mon, 26 May 2014 16:25:35 +0200 Subject: [Haskell-beginners] Evaluate function In-Reply-To: References: <2021885.39xMIW5ZDr@linux-hpeb.site> Message-ID: <20140526142535.GA18240@machine> On Mon, May 26, 2014 at 08:21:36PM +0700, Kim-Ee Yeoh wrote: > The length function strikes me as a kludgy stand-in for rnf when rnf is really > what's meant. And depending on the type of the list entries, calling length won't always have the same effect then calling rnf on the list and therefore on all entries of the list. Greetings, Daniel From k.bleijenberg at lijbrandt.nl Mon May 26 14:46:29 2014 From: k.bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Mon, 26 May 2014 16:46:29 +0200 Subject: [Haskell-beginners] apply a function to all params of another function Message-ID: <001201cf78f1$474cf130$d5e6d390$@bleijenberg@lijbrandt.nl> In have a lot of functions in a program with params that are alle Int's. f :: Int -> Int -> Int g :: Int -> Int -> Int h :: Int -> Int -> Int -> Int ... and convertParam :: Int -> Int -- say (+1) I want to call functions like f, g and h, but apply convertParam to all params first. f2 a b c = f (convertParam a) (convertParam b) (convertParam c) I tried: run1p f conv = \a -> f (conv a) -- for functions with one param run2p f conv = \a b -> f (conv a) (conv b) -- for functions with two params . Can you do this in a more generalized way (using currying?) Any ideas? Kees From byorgey at seas.upenn.edu Mon May 26 16:21:57 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon, 26 May 2014 12:21:57 -0400 Subject: [Haskell-beginners] Evaluate function In-Reply-To: <20140526142535.GA18240@machine> References: <2021885.39xMIW5ZDr@linux-hpeb.site> <20140526142535.GA18240@machine> Message-ID: <20140526162157.GA8523@seas.upenn.edu> On Mon, May 26, 2014 at 04:25:35PM +0200, Daniel Trstenjak wrote: > On Mon, May 26, 2014 at 08:21:36PM +0700, Kim-Ee Yeoh wrote: > > The length function strikes me as a kludgy stand-in for rnf when rnf is really > > what's meant. > > And depending on the type of the list entries, calling length won't > always have the same effect then calling rnf on the list and therefore > on all entries of the list. > Though I could imagine a situation where one wants to force the spine of the list but not the elements it contains. In that case rnf would do too much, and 'length' would do the right thing. However, 'length' still feels kludgy here. What is really wanted is a domain-specific language for expressing which parts of a structure should be forced. Such a domain-specific language can be found here: http://hackage.haskell.org/package/parallel-3.2.0.4/docs/Control-Seq.html In particular, forcing the spine of a list but not its elements can be expressed by evaluate (puzzles `using` seqList r0) . -Brent From byorgey at seas.upenn.edu Mon May 26 16:25:27 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon, 26 May 2014 12:25:27 -0400 Subject: [Haskell-beginners] apply a function to all params of another function In-Reply-To: <001201cf78f1$474cf130$d5e6d390$@bleijenberg@lijbrandt.nl> References: <001201cf78f1$474cf130$d5e6d390$@bleijenberg@lijbrandt.nl> Message-ID: <20140526162527.GB8523@seas.upenn.edu> On Mon, May 26, 2014 at 04:46:29PM +0200, Kees Bleijenberg wrote: > In have a lot of functions in a program with params that are alle Int's. > f :: Int -> Int -> Int > g :: Int -> Int -> Int > h :: Int -> Int -> Int -> Int > ... > and convertParam :: Int -> Int -- say (+1) > I want to call functions like f, g and h, but apply convertParam to all > params first. > f2 a b c = f (convertParam a) (convertParam b) (convertParam c) > > I tried: > run1p f conv = \a -> f (conv a) -- for functions with one > param > run2p f conv = \a b -> f (conv a) (conv b) -- for functions with two > params > . > Can you do this in a more generalized way (using currying?) > Any ideas? class Convertible t where convert :: t -> t instance Convertible Int where ... instance Convertible t => Convertible (Int -> t) where ... I'll let you fill in the ... ! =) Note this only works well because the base case is Int. You could also add some extra base cases for other concrete return types. However, it is quite difficult to give a Convertible instance which applies to "all types which are not functions", so this will only work if you are willing to make one instance for each concrete result type your functions use. -Brent From daniel.zidan.king at gmail.com Mon May 26 18:01:38 2014 From: daniel.zidan.king at gmail.com (Daniel King) Date: Mon, 26 May 2014 14:01:38 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" Message-ID: Hi all, I created a cabal project with the attached cabal file. I run: cabal configure --enable-tests cabal build cabal test and then I get the error: cabal: Prelude.read: no parse I'm not sure how to debug this any further. If I execute: ./dist/build/scientific-pl-testsStub/scientific-pl-testsStub with any input I've tried (numbers, array notation, string notation, etc.) and I get that same error. I can post the whole project if that is helpful. Thanks for the debugging tips! The full log is: danking at spock # cabal configure --enable-tests Resolving dependencies... Configuring scientific-pl-0.1.0.0... danking at spock # cabal build Building scientific-pl-0.1.0.0... Preprocessing test suite 'scientific-pl-tests' for scientific-pl-0.1.0.0... [1 of 3] Compiling SPLData ( SPLData.hs, dist/build/SPLData.o ) [2 of 3] Compiling SPLEval ( SPLEval.hs, dist/build/SPLEval.o ) [3 of 3] Compiling Tests ( tests/Tests.hs, dist/build/Tests.o ) In-place registering scientific-pl-tests-0.1.0.0... [1 of 1] Compiling Main ( dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/scientific-pl-testsStub.hs, dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/Main.o ) Linking dist/build/scientific-pl-testsStub/scientific-pl-testsStub ... Preprocessing executable 'scientific-pl' for scientific-pl-0.1.0.0... [1 of 3] Compiling SPLData ( SPLData.hs, dist/build/scientific-pl/scientific-pl-tmp/SPLData.o ) [2 of 3] Compiling SPLEval ( SPLEval.hs, dist/build/scientific-pl/scientific-pl-tmp/SPLEval.o ) [3 of 3] Compiling Main ( Main.hs, dist/build/scientific-pl/scientific-pl-tmp/Main.o ) Linking dist/build/scientific-pl/scientific-pl ... danking at spock # cabal test Running 1 test suites... Test suite scientific-pl-tests: RUNNING... cabal: Prelude.read: no parse 1 danking at spock # -- Dan King -------------- next part -------------- A non-text attachment was scrubbed... Name: scientific-pl.cabal Type: application/octet-stream Size: 999 bytes Desc: not available URL: From Boris.Daix at gmail.com Mon May 26 19:13:51 2014 From: Boris.Daix at gmail.com (Boris) Date: Mon, 26 May 2014 21:13:51 +0200 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: (Daniel King's message of "Mon, 26 May 2014 14:01:38 -0400") References: Message-ID: <878upobbf4.fsf@gmail.com> Hello, Last time I got such an error, it was because I was using 'read' for a type I had defined myself, deriving Read, but on something that was not reelated to the type. For instance: data Foo = Foo deriving (Read, Show) x :: Foo x = read "Bar" Do you use read somwhere? HTH Daniel King writes: > Hi all, > > I created a cabal project with the attached cabal file. I run: > > cabal configure --enable-tests > cabal build > cabal test > > and then I get the error: > > cabal: Prelude.read: no parse > > I'm not sure how to debug this any further. If I execute: > > ./dist/build/scientific-pl-testsStub/scientific-pl-testsStub > > with any input I've tried (numbers, array notation, string notation, > etc.) and I get that same error. > > I can post the whole project if that is helpful. > > Thanks for the debugging tips! > > The full log is: > > danking at spock # cabal configure --enable-tests > Resolving dependencies... > Configuring scientific-pl-0.1.0.0... > danking at spock # cabal build > Building scientific-pl-0.1.0.0... > Preprocessing test suite 'scientific-pl-tests' for scientific-pl-0.1.0.0... > [1 of 3] Compiling SPLData ( SPLData.hs, dist/build/SPLData.o ) > [2 of 3] Compiling SPLEval ( SPLEval.hs, dist/build/SPLEval.o ) > [3 of 3] Compiling Tests ( tests/Tests.hs, dist/build/Tests.o ) > In-place registering scientific-pl-tests-0.1.0.0... > [1 of 1] Compiling Main ( > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/scientific-pl-testsStub.hs, > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/Main.o > ) > Linking dist/build/scientific-pl-testsStub/scientific-pl-testsStub ... > Preprocessing executable 'scientific-pl' for scientific-pl-0.1.0.0... > [1 of 3] Compiling SPLData ( SPLData.hs, > dist/build/scientific-pl/scientific-pl-tmp/SPLData.o ) > [2 of 3] Compiling SPLEval ( SPLEval.hs, > dist/build/scientific-pl/scientific-pl-tmp/SPLEval.o ) > [3 of 3] Compiling Main ( Main.hs, > dist/build/scientific-pl/scientific-pl-tmp/Main.o ) > Linking dist/build/scientific-pl/scientific-pl ... > danking at spock # cabal test > Running 1 test suites... > Test suite scientific-pl-tests: RUNNING... > cabal: Prelude.read: no parse > 1 danking at spock # -- Boris Daix From kevin at ksvanhorn.com Mon May 26 21:12:02 2014 From: kevin at ksvanhorn.com (Kevin Van Horn) Date: Mon, 26 May 2014 15:12:02 -0600 Subject: [Haskell-beginners] Haskell type Array a b Message-ID: <26423227-E638-4DCE-BCDB-30C1A2E9A507@ksvanhorn.com> I'm looking on the Haskell 98 Report, and the definition of (==) for values of type Array a b looks wrong to me. Here is the definition given: a == a' = (assocs a == assocs a') But this fails to account for the array length in each dimension; hence, if x is a 0 x 2 array, y is a 2 x 0, and z is a 0 x 0 array, then x == y x == z, and y == z are all True, even though bounds x != bounds y, bounds x != bounds z, and bounds y != bounds z. This violates the most fundamental property of equality: that if a == b, then you can substitute a for b in an expression without changing the value of the expression. I can't possibly be the first person to have noticed this. So why is equality for Arrays defined this way? -------------- next part -------------- An HTML attachment was scrubbed... URL: From erikprice at gmail.com Mon May 26 21:20:47 2014 From: erikprice at gmail.com (Erik Price) Date: Mon, 26 May 2014 17:20:47 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: <878upobbf4.fsf@gmail.com> References: <878upobbf4.fsf@gmail.com> Message-ID: It's possible that you're calling the read :: Read a => String -> a function somewhere with a parameter that the function doesn't know how to parse. I ran into this just today when I called read "a string" :: String when what I needed to call was read "\"a string\"" :: String e On Monday, May 26, 2014, Boris wrote: > Hello, > > Last time I got such an error, it was because I was using 'read' for a > type I had defined myself, deriving Read, but on something that was not > reelated to the type. For instance: > > data Foo = Foo deriving (Read, Show) > > x :: Foo > x = read "Bar" > > Do you use read somwhere? > > HTH > > Daniel King > writes: > > > Hi all, > > > > I created a cabal project with the attached cabal file. I run: > > > > cabal configure --enable-tests > > cabal build > > cabal test > > > > and then I get the error: > > > > cabal: Prelude.read: no parse > > > > I'm not sure how to debug this any further. If I execute: > > > > ./dist/build/scientific-pl-testsStub/scientific-pl-testsStub > > > > with any input I've tried (numbers, array notation, string notation, > > etc.) and I get that same error. > > > > I can post the whole project if that is helpful. > > > > Thanks for the debugging tips! > > > > The full log is: > > > > danking at spock # cabal configure --enable-tests > > Resolving dependencies... > > Configuring scientific-pl-0.1.0.0... > > danking at spock # cabal build > > Building scientific-pl-0.1.0.0... > > Preprocessing test suite 'scientific-pl-tests' for > scientific-pl-0.1.0.0... > > [1 of 3] Compiling SPLData ( SPLData.hs, dist/build/SPLData.o ) > > [2 of 3] Compiling SPLEval ( SPLEval.hs, dist/build/SPLEval.o ) > > [3 of 3] Compiling Tests ( tests/Tests.hs, dist/build/Tests.o > ) > > In-place registering scientific-pl-tests-0.1.0.0... > > [1 of 1] Compiling Main ( > > > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/scientific-pl-testsStub.hs, > > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/Main.o > > ) > > Linking dist/build/scientific-pl-testsStub/scientific-pl-testsStub ... > > Preprocessing executable 'scientific-pl' for scientific-pl-0.1.0.0... > > [1 of 3] Compiling SPLData ( SPLData.hs, > > dist/build/scientific-pl/scientific-pl-tmp/SPLData.o ) > > [2 of 3] Compiling SPLEval ( SPLEval.hs, > > dist/build/scientific-pl/scientific-pl-tmp/SPLEval.o ) > > [3 of 3] Compiling Main ( Main.hs, > > dist/build/scientific-pl/scientific-pl-tmp/Main.o ) > > Linking dist/build/scientific-pl/scientific-pl ... > > danking at spock # cabal test > > Running 1 test suites... > > Test suite scientific-pl-tests: RUNNING... > > cabal: Prelude.read: no parse > > 1 danking at spock # > > -- > Boris Daix > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Mon May 26 21:37:49 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 26 May 2014 17:37:49 -0400 Subject: [Haskell-beginners] Haskell type Array a b In-Reply-To: <26423227-E638-4DCE-BCDB-30C1A2E9A507@ksvanhorn.com> References: <26423227-E638-4DCE-BCDB-30C1A2E9A507@ksvanhorn.com> Message-ID: On Mon, May 26, 2014 at 5:12 PM, Kevin Van Horn wrote: > I'm looking on the Haskell 98 Report, and the definition of (==) for > values of type Array a b looks wrong to me. Here is the definition given: > > a == a' = (assocs a == assocs a') > > But this fails to account for the array length in each dimension; hence, if > Have you tested this? assocs produces a list whose length depends on the array bounds; (==) on lists accounts for the length. What's left is the possibility of bounds mismatches, but in that case the index elements of the list of pairs produced by assocs will not match and they will compare unequal. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Mon May 26 23:01:31 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Tue, 27 May 2014 01:01:31 +0200 Subject: [Haskell-beginners] Haskell type Array a b In-Reply-To: References: <26423227-E638-4DCE-BCDB-30C1A2E9A507@ksvanhorn.com> Message-ID: The arrays he gave as an example had all At least one dimension equal to 0. So assocs would produce an empty list as far as I remember, so the three arrays would be considered to be equal indeed. It's a pity I can't check that right now, I'm on mobile... Am 26.05.2014 23:37 schrieb "Brandon Allbery" : > On Mon, May 26, 2014 at 5:12 PM, Kevin Van Horn wrote: > >> I'm looking on the Haskell 98 Report, and the definition of (==) for >> values of type Array a b looks wrong to me. Here is the definition given: >> >> a == a' = (assocs a == assocs a') >> >> But this fails to account for the array length in each dimension; hence, >> if >> > > Have you tested this? assocs produces a list whose length depends on the > array bounds; (==) on lists accounts for the length. What's left is the > possibility of bounds mismatches, but in that case the index elements of > the list of pairs produced by assocs will not match and they will compare > unequal. > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Mon May 26 23:17:43 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 26 May 2014 19:17:43 -0400 Subject: [Haskell-beginners] Haskell type Array a b In-Reply-To: References: <26423227-E638-4DCE-BCDB-30C1A2E9A507@ksvanhorn.com> Message-ID: On Mon, May 26, 2014 at 7:01 PM, Norbert Melzer wrote: > The arrays he gave as an example had all At least one dimension equal to > 0. So assocs would produce an empty list as far as I remember, so the three > arrays would be considered to be equal indeed. It's a pity I can't check > that right now, I'm on mobile... > Then I would indeed expect them to be trivially equal in H'98. I think if you're looking for anything more precise, you need to switch to something that can't be represented in H'98 or even H'2010, namely arrays with the bounds in the type. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From kevin at ksvanhorn.com Tue May 27 00:40:01 2014 From: kevin at ksvanhorn.com (Kevin Van Horn) Date: Mon, 26 May 2014 18:40:01 -0600 Subject: [Haskell-beginners] Haskell type Array a b In-Reply-To: References: Message-ID: <47D1DF51-1FF1-48EA-A059-465B8DC5A2D8@ksvanhorn.com> On May 26, Brandon Allbery wrote: > On Mon, May 26, 2014 at 5:12 PM, Kevin Van Horn wrote: > I'm looking on the Haskell 98 Report, and the definition of (==) for values of type Array a b looks wrong to me. Here is the definition given: > > a == a' = (assocs a == assocs a') > > But this fails to account for the array length in each dimension; hence, if > > Have you tested this? Yes. For the arrays I defined I had assocs a = [ ], because the length in one dimension was equal to 0. So I had x == y but bounds x != bounds y. > I think if you're looking for anything more precise, you need to switch to something that can't be represented in H'98 or even H'2010, namely arrays with the bounds in the type. Why would you need that? Array equality could have been defined to be a1 == a2 = (bounds a1 == bounds a2) && (assocs a1 == assocs a2) and I don't understand why this is not the definition. -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.zidan.king at gmail.com Tue May 27 03:12:00 2014 From: daniel.zidan.king at gmail.com (Daniel King) Date: Mon, 26 May 2014 23:12:00 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: References: <878upobbf4.fsf@gmail.com> Message-ID: I don't call the `read' function in my source code anywhere. I do derive `read' instances in my data definitions, but otherwise grep finds no occurrences. I'm fairly certain this is a bug in the testing framework, but I don't know how to debug it further. The cabal compilation process seems to produce a file at "dist/build/MY_PACKAGE_NAME-testsStub/MY_PACKAGE_NAME-testsStub-tmp/MY_PACKAGE_NAME-testsStub.hs". This file contains: module Main ( main ) where import Distribution.Simple.Test ( stubMain ) import Tests ( tests ) main :: IO () main = stubMain tests In the parent directory there is an executable, "dist/build/MY_PACKAGE_NAME-testsStub/MY_PACKAGE_NAME-testsStub" which appears to be the file which cabal executes when I call "cabal test". I'm not sure how this executable is created but I suspect it comes from the file I quoted above. I'm beginning to suspect that the testing framework is not playing well with something. This is my test file: http://lpaste.net/104680 I'm using the detailed-0.9 "type" of test module, because the detailed-1.0 "type" isn't present on my system. Is this the problem? They seem to reference detailed-1.0 in this user guide I found for Cabal: http://www.haskell.org/cabal/release/rc/doc/users-guide/#test-suites . Though now, seeing the URL, it seems that the user guide might be for a release candidate version of cabal? I found that page through googling how to set up a test suite in Cabal. Can someone point me at a different guide or tutorial? I tried this (http://www.haskell.org/cabal/users-guide/) guide as well, but it doesn't give any examples and barely references test suites at all. On Mon, May 26, 2014 at 5:20 PM, Erik Price wrote: > It's possible that you're calling the > > read :: Read a => String -> a > > function somewhere with a parameter that the function doesn't know how to > parse. I ran into this just today when I called > > read "a string" :: String > > when what I needed to call was > > read "\"a string\"" :: String > > e > > > On Monday, May 26, 2014, Boris wrote: >> >> Hello, >> >> Last time I got such an error, it was because I was using 'read' for a >> type I had defined myself, deriving Read, but on something that was not >> reelated to the type. For instance: >> >> data Foo = Foo deriving (Read, Show) >> >> x :: Foo >> x = read "Bar" >> >> Do you use read somwhere? >> >> HTH >> >> Daniel King writes: >> >> > Hi all, >> > >> > I created a cabal project with the attached cabal file. I run: >> > >> > cabal configure --enable-tests >> > cabal build >> > cabal test >> > >> > and then I get the error: >> > >> > cabal: Prelude.read: no parse >> > >> > I'm not sure how to debug this any further. If I execute: >> > >> > ./dist/build/scientific-pl-testsStub/scientific-pl-testsStub >> > >> > with any input I've tried (numbers, array notation, string notation, >> > etc.) and I get that same error. >> > >> > I can post the whole project if that is helpful. >> > >> > Thanks for the debugging tips! >> > >> > The full log is: >> > >> > danking at spock # cabal configure --enable-tests >> > Resolving dependencies... >> > Configuring scientific-pl-0.1.0.0... >> > danking at spock # cabal build >> > Building scientific-pl-0.1.0.0... >> > Preprocessing test suite 'scientific-pl-tests' for >> > scientific-pl-0.1.0.0... >> > [1 of 3] Compiling SPLData ( SPLData.hs, dist/build/SPLData.o ) >> > [2 of 3] Compiling SPLEval ( SPLEval.hs, dist/build/SPLEval.o ) >> > [3 of 3] Compiling Tests ( tests/Tests.hs, dist/build/Tests.o >> > ) >> > In-place registering scientific-pl-tests-0.1.0.0... >> > [1 of 1] Compiling Main ( >> > >> > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/scientific-pl-testsStub.hs, >> > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/Main.o >> > ) >> > Linking dist/build/scientific-pl-testsStub/scientific-pl-testsStub ... >> > Preprocessing executable 'scientific-pl' for scientific-pl-0.1.0.0... >> > [1 of 3] Compiling SPLData ( SPLData.hs, >> > dist/build/scientific-pl/scientific-pl-tmp/SPLData.o ) >> > [2 of 3] Compiling SPLEval ( SPLEval.hs, >> > dist/build/scientific-pl/scientific-pl-tmp/SPLEval.o ) >> > [3 of 3] Compiling Main ( Main.hs, >> > dist/build/scientific-pl/scientific-pl-tmp/Main.o ) >> > Linking dist/build/scientific-pl/scientific-pl ... >> > danking at spock # cabal test >> > Running 1 test suites... >> > Test suite scientific-pl-tests: RUNNING... >> > cabal: Prelude.read: no parse >> > 1 danking at spock # >> >> -- >> Boris Daix >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Dan King From byorgey at seas.upenn.edu Tue May 27 17:45:52 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue, 27 May 2014 13:45:52 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: References: <878upobbf4.fsf@gmail.com> Message-ID: <20140527174552.GA1435@seas.upenn.edu> This seems related, though I don't think it's your problem exactly: https://github.com/haskell/cabal/issues/1366 Apparently a "read: no parse" error can be generated when cabal fails to read a log file for some reason. Can you tell us precisely what versions of cabal and ghc you are using (i.e. the output of ghc --version and cabal --version)? -Brent On Mon, May 26, 2014 at 11:12:00PM -0400, Daniel King wrote: > I don't call the `read' function in my source code anywhere. I do > derive `read' instances in my data definitions, but otherwise grep > finds no occurrences. > > I'm fairly certain this is a bug in the testing framework, but I don't > know how to debug it further. The cabal compilation process seems to > produce a file at > "dist/build/MY_PACKAGE_NAME-testsStub/MY_PACKAGE_NAME-testsStub-tmp/MY_PACKAGE_NAME-testsStub.hs". > This file contains: > > module Main ( main ) where > import Distribution.Simple.Test ( stubMain ) > import Tests ( tests ) > main :: IO () > main = stubMain tests > > In the parent directory there is an executable, > "dist/build/MY_PACKAGE_NAME-testsStub/MY_PACKAGE_NAME-testsStub" which > appears to be the file which cabal executes when I call "cabal test". > I'm not sure how this executable is created but I suspect it comes > from the file I quoted above. > > I'm beginning to suspect that the testing framework is not playing > well with something. > > This is my test file: http://lpaste.net/104680 > > I'm using the detailed-0.9 "type" of test module, because the > detailed-1.0 "type" isn't present on my system. Is this the problem? > They seem to reference detailed-1.0 in this user guide I found for > Cabal: http://www.haskell.org/cabal/release/rc/doc/users-guide/#test-suites > . Though now, seeing the URL, it seems that the user guide might be > for a release candidate version of cabal? I found that page through > googling how to set up a test suite in Cabal. > > Can someone point me at a different guide or tutorial? I tried this > (http://www.haskell.org/cabal/users-guide/) guide as well, but it > doesn't give any examples and barely references test suites at all. > > On Mon, May 26, 2014 at 5:20 PM, Erik Price wrote: > > It's possible that you're calling the > > > > read :: Read a => String -> a > > > > function somewhere with a parameter that the function doesn't know how to > > parse. I ran into this just today when I called > > > > read "a string" :: String > > > > when what I needed to call was > > > > read "\"a string\"" :: String > > > > e > > > > > > On Monday, May 26, 2014, Boris wrote: > >> > >> Hello, > >> > >> Last time I got such an error, it was because I was using 'read' for a > >> type I had defined myself, deriving Read, but on something that was not > >> reelated to the type. For instance: > >> > >> data Foo = Foo deriving (Read, Show) > >> > >> x :: Foo > >> x = read "Bar" > >> > >> Do you use read somwhere? > >> > >> HTH > >> > >> Daniel King writes: > >> > >> > Hi all, > >> > > >> > I created a cabal project with the attached cabal file. I run: > >> > > >> > cabal configure --enable-tests > >> > cabal build > >> > cabal test > >> > > >> > and then I get the error: > >> > > >> > cabal: Prelude.read: no parse > >> > > >> > I'm not sure how to debug this any further. If I execute: > >> > > >> > ./dist/build/scientific-pl-testsStub/scientific-pl-testsStub > >> > > >> > with any input I've tried (numbers, array notation, string notation, > >> > etc.) and I get that same error. > >> > > >> > I can post the whole project if that is helpful. > >> > > >> > Thanks for the debugging tips! > >> > > >> > The full log is: > >> > > >> > danking at spock # cabal configure --enable-tests > >> > Resolving dependencies... > >> > Configuring scientific-pl-0.1.0.0... > >> > danking at spock # cabal build > >> > Building scientific-pl-0.1.0.0... > >> > Preprocessing test suite 'scientific-pl-tests' for > >> > scientific-pl-0.1.0.0... > >> > [1 of 3] Compiling SPLData ( SPLData.hs, dist/build/SPLData.o ) > >> > [2 of 3] Compiling SPLEval ( SPLEval.hs, dist/build/SPLEval.o ) > >> > [3 of 3] Compiling Tests ( tests/Tests.hs, dist/build/Tests.o > >> > ) > >> > In-place registering scientific-pl-tests-0.1.0.0... > >> > [1 of 1] Compiling Main ( > >> > > >> > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/scientific-pl-testsStub.hs, > >> > dist/build/scientific-pl-testsStub/scientific-pl-testsStub-tmp/Main.o > >> > ) > >> > Linking dist/build/scientific-pl-testsStub/scientific-pl-testsStub ... > >> > Preprocessing executable 'scientific-pl' for scientific-pl-0.1.0.0... > >> > [1 of 3] Compiling SPLData ( SPLData.hs, > >> > dist/build/scientific-pl/scientific-pl-tmp/SPLData.o ) > >> > [2 of 3] Compiling SPLEval ( SPLEval.hs, > >> > dist/build/scientific-pl/scientific-pl-tmp/SPLEval.o ) > >> > [3 of 3] Compiling Main ( Main.hs, > >> > dist/build/scientific-pl/scientific-pl-tmp/Main.o ) > >> > Linking dist/build/scientific-pl/scientific-pl ... > >> > danking at spock # cabal test > >> > Running 1 test suites... > >> > Test suite scientific-pl-tests: RUNNING... > >> > cabal: Prelude.read: no parse > >> > 1 danking at spock # > >> > >> -- > >> Boris Daix > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://www.haskell.org/mailman/listinfo/beginners > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > > > -- > Dan King > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From julian.birch at gmail.com Tue May 27 18:39:46 2014 From: julian.birch at gmail.com (Julian Birch) Date: Tue, 27 May 2014 19:39:46 +0100 Subject: [Haskell-beginners] apply a function to all params of another function In-Reply-To: <20140526162527.GB8523@seas.upenn.edu> References: <20140526162527.GB8523@seas.upenn.edu> Message-ID: Just a thought, you might want to use functions like (a,a,a)->a instead. J On Monday, May 26, 2014, Brent Yorgey wrote: > On Mon, May 26, 2014 at 04:46:29PM +0200, Kees Bleijenberg wrote: > > In have a lot of functions in a program with params that are alle Int's. > > f :: Int -> Int -> Int > > g :: Int -> Int -> Int > > h :: Int -> Int -> Int -> Int > > ... > > and convertParam :: Int -> Int -- say (+1) > > I want to call functions like f, g and h, but apply convertParam to all > > params first. > > f2 a b c = f (convertParam a) (convertParam b) (convertParam c) > > > > I tried: > > run1p f conv = \a -> f (conv a) -- for functions > with one > > param > > run2p f conv = \a b -> f (conv a) (conv b) -- for functions with two > > params > > . > > Can you do this in a more generalized way (using currying?) > > Any ideas? > > class Convertible t where > convert :: t -> t > > instance Convertible Int where > ... > > instance Convertible t => Convertible (Int -> t) where > ... > > I'll let you fill in the ... ! =) Note this only works well because > the base case is Int. You could also add some extra base cases for > other concrete return types. However, it is quite difficult to give a > Convertible instance which applies to "all types which are not > functions", so this will only work if you are willing to make one > instance for each concrete result type your functions use. > > -Brent > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.zidan.king at gmail.com Tue May 27 18:47:51 2014 From: daniel.zidan.king at gmail.com (Daniel King) Date: Tue, 27 May 2014 14:47:51 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: <20140527174552.GA1435@seas.upenn.edu> References: <878upobbf4.fsf@gmail.com> <20140527174552.GA1435@seas.upenn.edu> Message-ID: On Tue, May 27, 2014 at 1:45 PM, Brent Yorgey wrote: > This seems related, though I don't think it's your problem exactly: > > https://github.com/haskell/cabal/issues/1366 I saw that, realized I didn't change directory at all and wasn't really sure how to proceed. It did help me narrow done the cause to something directly in the test suite though. > Apparently a "read: no parse" error can be generated when cabal fails > to read a log file for some reason. Can you tell us precisely what > versions of cabal and ghc you are using (i.e. the output of ghc > --version and cabal --version)? Sure thing: 1 danking at spock # cabal --version cabal-install version 1.20.0.2 using version 1.20.0.0 of the Cabal library danking at spock # ghc --version The Glorious Glasgow Haskell Compilation System, version 7.6.3 Upon seeing this version information, I tried changing my build-depends to depend on Cabal 1.20 or greater, but I still get the error. -- Dan King From byorgey at seas.upenn.edu Tue May 27 19:29:15 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue, 27 May 2014 15:29:15 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: References: <878upobbf4.fsf@gmail.com> <20140527174552.GA1435@seas.upenn.edu> Message-ID: <20140527192915.GA6472@seas.upenn.edu> On Tue, May 27, 2014 at 02:47:51PM -0400, Daniel King wrote: > On Tue, May 27, 2014 at 1:45 PM, Brent Yorgey wrote: > > This seems related, though I don't think it's your problem exactly: > > > > https://github.com/haskell/cabal/issues/1366 > > I saw that, realized I didn't change directory at all and wasn't > really sure how to proceed. It did help me narrow done the cause to > something directly in the test suite though. > > > Apparently a "read: no parse" error can be generated when cabal fails > > to read a log file for some reason. Can you tell us precisely what > > versions of cabal and ghc you are using (i.e. the output of ghc > > --version and cabal --version)? > > Sure thing: > > 1 danking at spock # cabal --version > cabal-install version 1.20.0.2 > using version 1.20.0.0 of the Cabal library > > danking at spock # ghc --version > The Glorious Glasgow Haskell Compilation System, version 7.6.3 > > Upon seeing this version information, I tried changing my build-depends to > depend on Cabal 1.20 or greater, but I still get the error. Hmm, I'm not sure then. The people who know the most about the internals of Cabal likely do not read this list; you may have better luck asking on stackoverflow.com and/or in the #hackage channel on Freenode IRC. -Brent From miroslav.karpis at gmail.com Wed May 28 12:48:19 2014 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Wed, 28 May 2014 14:48:19 +0200 Subject: [Haskell-beginners] Network.HTTP.Conduit - body in REST/POST request Message-ID: Hi, I'm having difficulties with sending body parameters/values in a POST request. It seems that the server does not receive in body. ideas/comments very welcome ;-) cheers, m. {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Conduit import Data.Word(Word8) import Data.ByteString.Lazy(pack) import qualified Data.ByteString.Char8 as B x :: B.ByteString x = "?value=10" post = do r <- parseUrl "http://postcatcher.in/catchers/5385d4e0b6887c0200000071" putStrLn $show r let request = r { secure = True , method = "POST" , requestBody = RequestBodyBS s , requestHeaders = (requestHeaders r) ++ [("Content-Type", "application/json")]} putStrLn $ show request response <- withManager $ httpLbs request print $ responseBody response -------------- next part -------------- An HTML attachment was scrubbed... URL: From miroslav.karpis at gmail.com Wed May 28 13:21:18 2014 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Wed, 28 May 2014 15:21:18 +0200 Subject: [Haskell-beginners] Network.HTTP.Conduit - body in REST/POST request In-Reply-To: References: Message-ID: so this did the trick: test :: IO () test = do r <- parseUrl $ "http://localhost:3000/readbody" let r_ = urlEncodedBody [("?nonce:", "2"), ("&method", "getInfo")] r let request = r_ { requestBody = (RequestBodyBS s)} response <- withManager $ httpLbs request putStrLn $ show request print $ responseBody response On Wed, May 28, 2014 at 2:48 PM, Miro Karpis wrote: > Hi, I'm having difficulties with sending body parameters/values in a POST > request. It seems that the server does not receive in body. > > ideas/comments very welcome ;-) > > cheers, > m. > > > {-# LANGUAGE OverloadedStrings #-} > > import Network.HTTP.Conduit > import Data.Word(Word8) > import Data.ByteString.Lazy(pack) > import qualified Data.ByteString.Char8 as B > > x :: B.ByteString > x = "?value=10" > > post = do > r <- parseUrl "http://postcatcher.in/catchers/5385d4e0b6887c0200000071" > putStrLn $show r > let request = r > { secure = True > , method = "POST" > , requestBody = RequestBodyBS s > , requestHeaders = (requestHeaders r) ++ [("Content-Type", > "application/json")]} > putStrLn $ show request > response <- withManager $ httpLbs request > print $ responseBody response > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at snoyman.com Wed May 28 16:18:31 2014 From: michael at snoyman.com (Michael Snoyman) Date: Wed, 28 May 2014 19:18:31 +0300 Subject: [Haskell-beginners] Network.HTTP.Conduit - body in REST/POST request In-Reply-To: References: Message-ID: Just to clarify one point: you don't need to include the ?, :, or & characters in the parameters to urlEncodedBody; urlEncodedBody will automatically add the ? and &, and I'm guessing the colon was just a typo. In other words, the following code is probably closer to what you were looking for: let request = urlEncodedBody [("nonce", "2"), ("method", "getInfo")] r On Wed, May 28, 2014 at 4:21 PM, Miro Karpis wrote: > so this did the trick: > > test :: IO () > test = do > r <- parseUrl $ "http://localhost:3000/readbody" > let r_ = urlEncodedBody [("?nonce:", "2"), ("&method", "getInfo")] r > let request = r_ > { requestBody = (RequestBodyBS s)} > > response <- withManager $ httpLbs request > putStrLn $ show request > print $ responseBody response > > > On Wed, May 28, 2014 at 2:48 PM, Miro Karpis wrote: > >> Hi, I'm having difficulties with sending body parameters/values in a POST >> request. It seems that the server does not receive in body. >> >> ideas/comments very welcome ;-) >> >> cheers, >> m. >> >> >> {-# LANGUAGE OverloadedStrings #-} >> >> import Network.HTTP.Conduit >> import Data.Word(Word8) >> import Data.ByteString.Lazy(pack) >> import qualified Data.ByteString.Char8 as B >> >> x :: B.ByteString >> x = "?value=10" >> >> post = do >> r <- parseUrl "http://postcatcher.in/catchers/5385d4e0b6887c0200000071" >> putStrLn $show r >> let request = r >> { secure = True >> , method = "POST" >> , requestBody = RequestBodyBS s >> , requestHeaders = (requestHeaders r) ++ [("Content-Type", >> "application/json")]} >> putStrLn $ show request >> response <- withManager $ httpLbs request >> print $ responseBody response >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Thu May 29 00:26:18 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 28 May 2014 18:26:18 -0600 Subject: [Haskell-beginners] Type class vs. Record: How do I model an interface in Haskell? In-Reply-To: <20140527192915.GA6472@seas.upenn.edu> References: <878upobbf4.fsf@gmail.com> <20140527174552.GA1435@seas.upenn.edu> <20140527192915.GA6472@seas.upenn.edu> Message-ID: <53867EAA.3040100@ucdavis.edu> Hi All, I have another modeling related question. This time is about how to model an interface in haskell. (full code in https://github.com/defigueiredo/interfaces-in-haskell) I have an interface that I want any Stock Exchange to follow. I can think of 3 different ways to do this in Haskell: - write a type class and then instantiate it for 3 different types - use a record and associate the appropriate function to each field - use a module and define the appropriate function in each file These options seem to go from most restrictive to least restrictive. In the sense that in the last solution I can make two functions calls: print $ NYSE.getPendingOrders print $ NASDAQ.getPendingOrders and the getPendingOrders functions don't even need to have exactly the same types between the two modules. I can see how this could be useful sometimes. Using record syntax is more restrictive as the type signatures for the functions have to match, but I am not sure what I get from having a type class here, rather than just the records. Comparing the model declarations, the type class solution has an extra parameter 'a' clobbering up the signatures: model.hs --------------------------------------------------------------------- module Model where import Data.Time.Clock.POSIX -- Units type Volume = Double type Price = Double type OrderID = Int type Timestamp = POSIXTime -- Orders data OrderType = Buy | Sell deriving Show data Order = Order { order::OrderType, price ::Price, volume ::Volume, confirmation :: Maybe (OrderID, Timestamp) } deriving Show ------ All versions identical until here ------ class Exchange a where sellAt :: a -> Price -> Volume -> Order buyAt :: a -> Price -> Volume -> Order getPendingOrders :: a -> Maybe [Order] -- returns [Order] or Nothing if can't get that information cancelOrder :: a -> OrderID -> Order -> Maybe (Either Order Order) -- returns canceled order (right) or -- what was still pending if already executed (left) -- or Nothing, if has no connectivity (or other error). --------------------------------------------------------------------- whereas the record solution is... ------ All versions identical until here ------ data Exchange = Exchange { sellAt :: Price -> Volume -> Order, buyAt :: Price -> Volume -> Order, getPendingOrders :: Maybe [Order] cancelOrder :: OrderID -> Order -> Maybe (Either Order Order), } --------------------------------------------------------------------- instances look pretty similar: where's the type class solution... --------------------------------------------------------------------- module NYSE where import Model data NYSE = NYSE instance Exchange NYSE where sellAt = error "NYSE doesn't sell." getPendingOrders a = Just [Order Buy 50 100 Nothing] buyAt = undefined cancelOrder = undefined --------------------------------------------------------------------- here's the record solution... --------------------------------------------------------------------- module NYSE where import Model nyse = Exchange { sellAt = error "NYSE doesn't sell.", getPendingOrders = Just [Order Buy 50 100 Nothing], buyAt = undefined, cancelOrder = undefined } --------------------------------------------------------------------- So, my questions are: 1) What do I get from making this a type class rather than using records? 2) Is there an even better solution I have overlooked? (full code for 3 different solutions in https://github.com/defigueiredo/interfaces-in-haskell) Thanks! Dimitri From ovidiudeac at gmail.com Thu May 29 06:01:33 2014 From: ovidiudeac at gmail.com (Ovidiu Deac) Date: Thu, 29 May 2014 09:01:33 +0300 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) Message-ID: I'm trying to install cabal 1.20 like this: I installed ghc and ghc-prof from apt repository so I got the following: ii ghc 7.6.3-5 amd64 The Glasgow Haskell Compilation s ii ghc-prof 7.6.3-5 amd64 Profiling libraries for the Glasg Then I git cloned cabal and checked out Cabal-v1.20.0.0 Then I ran bootstrap.sh in cabal-install dir and I get the following error: Using gcc for C compiler. If this is not what you want, set CC. Using /usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 instead. Checking installed packages for ghc-7.6.3... deepseq is already installed and the version is ok. time is already installed and the version is ok. Cabal is already installed and the version is ok. transformers is already installed and the version is ok. mtl is already installed and the version is ok. text is already installed and the version is ok. parsec is already installed and the version is ok. network is already installed and the version is ok. HTTP is already installed and the version is ok. zlib is already installed and the version is ok. random is already installed and the version is ok. stm is already installed and the version is ok. [...skipping many lines] package cabal-install-1.20.0.0 requires network-2.4.1.2 Building cabal-install-1.20.0.0... Preprocessing executable 'cabal' for cabal-install-1.20.0.0... : cannot satisfy -package-id network-2.4.1.2-fc99093587d92370c7febe034504fb40: network-2.4.1.2-fc99093587d92370c7febe034504fb40 is shadowed by package network-2.4.1.2-040cee5ece44014a8574cb3f87b1eec4 (use -v for more information) Error during cabal-install bootstrap: Building the cabal-install package failed. What am I doing wrong here? Thanks, Ovidiu -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Thu May 29 06:22:49 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 29 May 2014 01:22:49 -0500 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: References: Message-ID: Not to judo past the original question, but would it solve your problem to use an Ubuntu PPA[1]or the cabal binary distribution[2]? Also were you trying to build it in a sandbox? You shouldn't be installing things willy-nilly outside of a sandbox[3]. [1]: https://launchpad.net/~hvr/+archive/ghc [2]: http://www.haskell.org/cabal/download.html [3]https://github.com/bitemyapp/learnhaskell#cabal-guidelines On Thu, May 29, 2014 at 1:01 AM, Ovidiu Deac wrote: > I'm trying to install cabal 1.20 like this: > > I installed ghc and ghc-prof from apt repository so I got the following: > ii ghc 7.6.3-5 amd64 The Glasgow Haskell > Compilation s > ii ghc-prof 7.6.3-5 amd64 Profiling libraries for the > Glasg > > Then I git cloned cabal and checked out Cabal-v1.20.0.0 > > Then I ran bootstrap.sh in cabal-install dir and I get the following error: > > Using gcc for C compiler. If this is not what you want, set CC. > Using /usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 instead. > Checking installed packages for ghc-7.6.3... > deepseq is already installed and the version is ok. > time is already installed and the version is ok. > Cabal is already installed and the version is ok. > transformers is already installed and the version is ok. > mtl is already installed and the version is ok. > text is already installed and the version is ok. > parsec is already installed and the version is ok. > network is already installed and the version is ok. > HTTP is already installed and the version is ok. > zlib is already installed and the version is ok. > random is already installed and the version is ok. > stm is already installed and the version is ok. > > [...skipping many lines] > > package cabal-install-1.20.0.0 requires network-2.4.1.2 > Building cabal-install-1.20.0.0... > Preprocessing executable 'cabal' for cabal-install-1.20.0.0... > : cannot satisfy -package-id > network-2.4.1.2-fc99093587d92370c7febe034504fb40: > network-2.4.1.2-fc99093587d92370c7febe034504fb40 is shadowed by > package network-2.4.1.2-040cee5ece44014a8574cb3f87b1eec4 > (use -v for more information) > > Error during cabal-install bootstrap: > Building the cabal-install package failed. > > What am I doing wrong here? > > Thanks, > Ovidiu > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ovidiudeac at gmail.com Thu May 29 07:29:38 2014 From: ovidiudeac at gmail.com (Ovidiu Deac) Date: Thu, 29 May 2014 10:29:38 +0300 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: References: Message-ID: I'm trying to install a new version of cabal which supports sandboxes (>=1.18). Note that the default version of cabal is 1.16. Thanks for the pointers! I'll try installing from PPA in a few hours and get back with an update On Thu, May 29, 2014 at 9:22 AM, Christopher Allen wrote: > Not to judo past the original question, but would it solve your problem to > use an Ubuntu PPA[1]or the cabal binary distribution[2]? > > Also were you trying to build it in a sandbox? You shouldn't be installing > things willy-nilly outside of a sandbox[3]. > > [1]: https://launchpad.net/~hvr/+archive/ghc > [2]: http://www.haskell.org/cabal/download.html > [3]https://github.com/bitemyapp/learnhaskell#cabal-guidelines > > > > On Thu, May 29, 2014 at 1:01 AM, Ovidiu Deac wrote: > >> I'm trying to install cabal 1.20 like this: >> >> I installed ghc and ghc-prof from apt repository so I got the following: >> ii ghc 7.6.3-5 amd64 The Glasgow Haskell >> Compilation s >> ii ghc-prof 7.6.3-5 amd64 Profiling libraries for the >> Glasg >> >> Then I git cloned cabal and checked out Cabal-v1.20.0.0 >> >> Then I ran bootstrap.sh in cabal-install dir and I get the following >> error: >> >> Using gcc for C compiler. If this is not what you want, set CC. >> Using /usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 instead. >> Checking installed packages for ghc-7.6.3... >> deepseq is already installed and the version is ok. >> time is already installed and the version is ok. >> Cabal is already installed and the version is ok. >> transformers is already installed and the version is ok. >> mtl is already installed and the version is ok. >> text is already installed and the version is ok. >> parsec is already installed and the version is ok. >> network is already installed and the version is ok. >> HTTP is already installed and the version is ok. >> zlib is already installed and the version is ok. >> random is already installed and the version is ok. >> stm is already installed and the version is ok. >> >> [...skipping many lines] >> >> package cabal-install-1.20.0.0 requires network-2.4.1.2 >> Building cabal-install-1.20.0.0... >> Preprocessing executable 'cabal' for cabal-install-1.20.0.0... >> : cannot satisfy -package-id >> network-2.4.1.2-fc99093587d92370c7febe034504fb40: >> network-2.4.1.2-fc99093587d92370c7febe034504fb40 is shadowed by >> package network-2.4.1.2-040cee5ece44014a8574cb3f87b1eec4 >> (use -v for more information) >> >> Error during cabal-install bootstrap: >> Building the cabal-install package failed. >> >> What am I doing wrong here? >> >> Thanks, >> Ovidiu >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlatko.basic at gmail.com Thu May 29 10:55:44 2014 From: vlatko.basic at gmail.com (Vlatko Basic) Date: Thu, 29 May 2014 12:55:44 +0200 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: References: Message-ID: <53871230.3080109@gmail.com> An HTML attachment was scrubbed... URL: From haskell at elisehuard.be Thu May 29 15:31:16 2014 From: haskell at elisehuard.be (Elise Huard) Date: Thu, 29 May 2014 17:31:16 +0200 Subject: [Haskell-beginners] compilation joys Message-ID: Hi, I've been compiling this project https://github.com/mlesniak/game.git It uses Hipmunk (2D collision detection/physics framework) and OpenGL. Now the problem is that Hipmunk uses Data.StateVar, and OpenGL/GLUT use Graphics.Rendering.OpenGL.GL.StateVar Both have pretty much the same code and functionality, but using both together gives problems with conflicting types. My current solution was to locally fork Hipmunk and make it use Graphics.Rendering.OpenGL.GL.StateVar (because the other way round - making OpenGL use Data.StateVar, is a cascading nightmare, lots of libs to patch), and this works. My question: is this the only possible solution? Is there some way that doesn't involve patching one library or the other? The other thing I had to locally patch is to force cabal to use freeglut instead of the local Mac OS X GLUT. I did this by again, locally forking GLUT (long live cabal sandbox add-source!) and adding extra-lib-dirs: /usr/local/Cellar/freeglut/2.8.1/lib which is the local homebrew freeglut dir. Is there a better, more reproducible way to do this? Thank you, Elise From allbery.b at gmail.com Thu May 29 15:41:00 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Thu, 29 May 2014 11:41:00 -0400 Subject: [Haskell-beginners] compilation joys In-Reply-To: References: Message-ID: On Thu, May 29, 2014 at 11:31 AM, Elise Huard wrote: > It uses Hipmunk (2D collision detection/physics framework) and OpenGL. > Now the problem is that Hipmunk uses Data.StateVar, and OpenGL/GLUT > use Graphics.Rendering.OpenGL.GL.StateVar > Both have pretty much the same code and functionality, but using both > together gives problems with conflicting types. > I thought Data.StateVar was the old OpenGL StateVar unbundled? That is, current versions of OpenGL should be using Data.StateVar. Unfortunately, the Haskell Platform ships an older version of OpenGL; you may need to use a sandbox to install a current OpenGL. (Or wait a bit; the next Platform update is in testing.) -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.zidan.king at gmail.com Thu May 29 16:56:02 2014 From: daniel.zidan.king at gmail.com (Daniel King) Date: Thu, 29 May 2014 12:56:02 -0400 Subject: [Haskell-beginners] Using "cabal test" and getting "cabal: Prelude.read: no parse" In-Reply-To: <20140527192915.GA6472@seas.upenn.edu> References: <878upobbf4.fsf@gmail.com> <20140527174552.GA1435@seas.upenn.edu> <20140527192915.GA6472@seas.upenn.edu> Message-ID: Hi all, In correspondence with #hackage, I determined the source of the error. I wasn't using a test framework with the detailed-0.9 "test type", so when my tests threw errors they killed the process and eventually produced that Prelude.read error. On Tue, May 27, 2014 at 3:29 PM, Brent Yorgey wrote: > On Tue, May 27, 2014 at 02:47:51PM -0400, Daniel King wrote: >> On Tue, May 27, 2014 at 1:45 PM, Brent Yorgey wrote: >> > This seems related, though I don't think it's your problem exactly: >> > >> > https://github.com/haskell/cabal/issues/1366 >> >> I saw that, realized I didn't change directory at all and wasn't >> really sure how to proceed. It did help me narrow done the cause to >> something directly in the test suite though. >> >> > Apparently a "read: no parse" error can be generated when cabal fails >> > to read a log file for some reason. Can you tell us precisely what >> > versions of cabal and ghc you are using (i.e. the output of ghc >> > --version and cabal --version)? >> >> Sure thing: >> >> 1 danking at spock # cabal --version >> cabal-install version 1.20.0.2 >> using version 1.20.0.0 of the Cabal library >> >> danking at spock # ghc --version >> The Glorious Glasgow Haskell Compilation System, version 7.6.3 >> >> Upon seeing this version information, I tried changing my build-depends to >> depend on Cabal 1.20 or greater, but I still get the error. > > Hmm, I'm not sure then. The people who know the most about the > internals of Cabal likely do not read this list; you may have better > luck asking on stackoverflow.com and/or in the #hackage channel on > Freenode IRC. > > -Brent > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -- Dan King From ovidiudeac at gmail.com Thu May 29 18:44:46 2014 From: ovidiudeac at gmail.com (Ovidiu Deac) Date: Thu, 29 May 2014 21:44:46 +0300 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: <53871230.3080109@gmail.com> References: <53871230.3080109@gmail.com> Message-ID: If I do cabal update && cabal install cabal Apparently cabal is installed correctly ovidiu at thinkpad ~ $ cabal update && cabal install cabal Downloading the latest package list from hackage.haskell.org Resolving dependencies... Downloading Cabal-1.20.0.1.. [...skipping many lines...] In-place registering Cabal-1.20.0.1... Installing library in /home/ovidiu/.cabal/lib/x86_64-linux-ghc-7.6.3/Cabal-1.20.0.1 Registering Cabal-1.20.0.1... Installed Cabal-1.20.0.1 But I can't find ~/.cabal/bin anywhere ovidiu at thinkpad ~ $ ls ~/.cabal config lib logs packages share world On Thu, May 29, 2014 at 1:55 PM, Vlatko Basic wrote: > Install (any) cabal package in Ubuntu (with synaptic), and than run > > cabal update > cabal install cabal > > > After that new cabal will be in your .cabal/bin directory (which should be > on your path), and available to you only. > > > Cabal is just another package that can be installed. Current version is > 1.20 > > vlatko > > > -------- Original Message -------- > Subject: Re: [Haskell-beginners] installing cabal 1.20 on linux mint > debian edition (LMDE) > From: Ovidiu Deac > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > > Date: 29.05.2014 09:29 > > > I'm trying to install a new version of cabal which supports sandboxes > (>=1.18). Note that the default version of cabal is 1.16. > > Thanks for the pointers! I'll try installing from PPA in a few hours and > get back with an update > > > On Thu, May 29, 2014 at 9:22 AM, Christopher Allen > wrote: > >> Not to judo past the original question, but would it solve your problem >> to use an Ubuntu PPA[1]or the cabal binary distribution[2]? >> >> Also were you trying to build it in a sandbox? You shouldn't be >> installing things willy-nilly outside of a sandbox[3]. >> >> [1]: https://launchpad.net/~hvr/+archive/ghc >> >> [2]: http://www.haskell.org/cabal/download.html >> [3]https://github.com/bitemyapp/learnhaskell#cabal-guidelines >> >> >> >> On Thu, May 29, 2014 at 1:01 AM, Ovidiu Deac >> wrote: >> >>> I'm trying to install cabal 1.20 like this: >>> >>> I installed ghc and ghc-prof from apt repository so I got the following: >>> ii ghc 7.6.3-5 amd64 The Glasgow Haskell >>> Compilation s >>> ii ghc-prof 7.6.3-5 amd64 Profiling libraries for the >>> Glasg >>> >>> Then I git cloned cabal and checked out Cabal-v1.20.0.0 >>> >>> Then I ran bootstrap.sh in cabal-install dir and I get the following >>> error: >>> >>> Using gcc for C compiler. If this is not what you want, set CC. >>> Using /usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 instead. >>> Checking installed packages for ghc-7.6.3... >>> deepseq is already installed and the version is ok. >>> time is already installed and the version is ok. >>> Cabal is already installed and the version is ok. >>> transformers is already installed and the version is ok. >>> mtl is already installed and the version is ok. >>> text is already installed and the version is ok. >>> parsec is already installed and the version is ok. >>> network is already installed and the version is ok. >>> HTTP is already installed and the version is ok. >>> zlib is already installed and the version is ok. >>> random is already installed and the version is ok. >>> stm is already installed and the version is ok. >>> >>> [...skipping many lines] >>> >>> package cabal-install-1.20.0.0 requires network-2.4.1.2 >>> Building cabal-install-1.20.0.0... >>> Preprocessing executable 'cabal' for cabal-install-1.20.0.0... >>> : cannot satisfy -package-id >>> network-2.4.1.2-fc99093587d92370c7febe034504fb40: >>> network-2.4.1.2-fc99093587d92370c7febe034504fb40 is shadowed by >>> package network-2.4.1.2-040cee5ece44014a8574cb3f87b1eec4 >>> (use -v for more information) >>> >>> Error during cabal-install bootstrap: >>> Building the cabal-install package failed. >>> >>> What am I doing wrong here? >>> >>> Thanks, >>> Ovidiu >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mlists at pmade.com Thu May 29 18:49:11 2014 From: mlists at pmade.com (Peter Jones) Date: Thu, 29 May 2014 12:49:11 -0600 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) References: <53871230.3080109@gmail.com> Message-ID: <874n08l8t4.fsf@pmade.com> Ovidiu Deac writes: > ovidiu at thinkpad ~ $ cabal update && cabal install cabal That should be: $ cabal install cabal-install Cabal is a library, and cabal-install is the executable. -- Peter Jones, Founder, Devalot.com Defending the honor of good code From ovidiudeac at gmail.com Thu May 29 18:58:28 2014 From: ovidiudeac at gmail.com (Ovidiu Deac) Date: Thu, 29 May 2014 21:58:28 +0300 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: <874n08l8t4.fsf@pmade.com> References: <53871230.3080109@gmail.com> <874n08l8t4.fsf@pmade.com> Message-ID: ...and I'm back to the error regarding the shadowing of network pakage ovidiu at thinkpad ~ $ cabal install cabal-install Resolving dependencies... Downloading cabal-install-1.20.0.2... Configuring cabal-install-1.20.0.2... Warning: This package indirectly depends on multiple versions of the same package. This is highly likely to cause a compile failure. package HTTP-4000.2.8 requires network-2.4.1.2 package cabal-install-1.20.0.2 requires network-2.4.1.2 Building cabal-install-1.20.0.2... Preprocessing executable 'cabal' for cabal-install-1.20.0.2... : cannot satisfy -package-id network-2.4.1.2-fc99093587d92370c7febe034504fb40: network-2.4.1.2-fc99093587d92370c7febe034504fb40 is shadowed by package network-2.4.1.2-040cee5ece44014a8574cb3f87b1eec4 (use -v for more information) Failed to install cabal-install-1.20.0.2 cabal: Error: some packages failed to install: cabal-install-1.20.0.2 failed during the building phase. The exception was: ExitFailure 1 On Thu, May 29, 2014 at 9:49 PM, Peter Jones wrote: > Ovidiu Deac writes: > > ovidiu at thinkpad ~ $ cabal update && cabal install cabal > > That should be: > > $ cabal install cabal-install > > Cabal is a library, and cabal-install is the executable. > > -- > Peter Jones, Founder, Devalot.com > Defending the honor of good code > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dserban01 at gmail.com Thu May 29 20:06:11 2014 From: dserban01 at gmail.com (Dan Serban) Date: Thu, 29 May 2014 23:06:11 +0300 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: References: <53871230.3080109@gmail.com> <874n08l8t4.fsf@pmade.com> Message-ID: What is the output of: ghc-pkg list | grep network ? From mark.m.fredrickson at gmail.com Fri May 30 22:50:21 2014 From: mark.m.fredrickson at gmail.com (Mark Fredrickson) Date: Fri, 30 May 2014 17:50:21 -0500 Subject: [Haskell-beginners] Static records (CSV tables as modules) Message-ID: Hello, I writing a program that operates on some static data, currently saved in CSV files. Right now I parse the files at run time and then generate hashmap tables to connect the different data. Since I'm only ever operating on static data, I'm wondering if I can generate module files that capture the records as a sum type. To access the fields of the records, I could then imagine functions that exhaustively map the constructors to the data. Do any tools to generate .hs files from CSV or other formats exist? Insofar as this question has been asked before, the recommendation is "use Template Haskell", which makes sense, but is a less complete solution than I'm hoping for. Here's example of what what would be generated in literal Haskell source: -- input.csv name, age, someValue "abc", 1, 3.0 "xyz3", 99, -5.9 -- Input.hs module Input where data Row = R1 | R2 name :: Row -> String name R1 = "abc" name R2 = "xyz3" age :: Row -> Integer age R1 = 1 age R2 = 99 -- likewise for the function someValue Thanks, -M -------------- next part -------------- An HTML attachment was scrubbed... URL: From maydwell at gmail.com Fri May 30 23:07:00 2014 From: maydwell at gmail.com (Lyndon Maydwell) Date: Sat, 31 May 2014 09:07:00 +1000 Subject: [Haskell-beginners] Static records (CSV tables as modules) In-Reply-To: References: Message-ID: I've never heard of a csv code-generation tool that works like this, but it doesn't look like it would be too hard to hand-roll one, or do you want to bypass code-generation? - Lyndon On Sat, May 31, 2014 at 8:50 AM, Mark Fredrickson wrote: > Hello, > > I writing a program that operates on some static data, currently saved in > CSV files. Right now I parse the files at run time and then generate hashmap > tables to connect the different data. > > Since I'm only ever operating on static data, I'm wondering if I can > generate module files that capture the records as a sum type. To access the > fields of the records, I could then imagine functions that exhaustively map > the constructors to the data. > > Do any tools to generate .hs files from CSV or other formats exist? Insofar > as this question has been asked before, the recommendation is "use Template > Haskell", which makes sense, but is a less complete solution than I'm hoping > for. > > Here's example of what what would be generated in literal Haskell source: > > -- input.csv > name, age, someValue > "abc", 1, 3.0 > "xyz3", 99, -5.9 > > -- Input.hs > module Input where > > data Row = R1 | R2 > > name :: Row -> String > name R1 = "abc" > name R2 = "xyz3" > > age :: Row -> Integer > age R1 = 1 > age R2 = 99 > > -- likewise for the function someValue > > Thanks, > -M > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From bgamari.foss at gmail.com Fri May 30 23:40:53 2014 From: bgamari.foss at gmail.com (Ben Gamari) Date: Fri, 30 May 2014 19:40:53 -0400 Subject: [Haskell-beginners] Static records (CSV tables as modules) In-Reply-To: References: Message-ID: <87bnueded6.fsf@gmail.com> Mark Fredrickson writes: > Hello, > > I writing a program that operates on some static data, currently saved in > CSV files. Right now I parse the files at run time and then generate > hashmap tables to connect the different data. > > Since I'm only ever operating on static data, I'm wondering if I can > generate module files that capture the records as a sum type. To access the > fields of the records, I could then imagine functions that exhaustively map > the constructors to the data. > > Do any tools to generate .hs files from CSV or other formats exist? Insofar > as this question has been asked before, the recommendation is "use Template > Haskell", which makes sense, but is a less complete solution than I'm > hoping for. > How does the TH hack below look? See this Gist for this code and a test-case. Unfortunately there are a few gotchas here, 1. The record type needs a `Lift` instance[2]. There are a pain to write but can be derived[3] 2. The type of your data can't be defined in the same module as the TH splice due to GHC's stage restriction Cheers, - Ben [1] https://gist.github.com/bgamari/efad8560ab7dd38e9407 [2] http://hackage.haskell.org/package/template-haskell-2.9.0.0/docs/Language-Haskell-TH-Syntax.html#t:Lift [3] http://hackage.haskell.org/package/th-lift {-# LANGUAGE TemplateHaskell, FlexibleContexts #-} module StaticCSV (staticCSV) where import Control.Applicative import Data.Csv as Csv hiding (Name) import Data.Proxy import Data.Data import Language.Haskell.TH import Language.Haskell.TH.Syntax (Lift, lift) import qualified Data.ByteString.Lazy as BSL import qualified Data.Vector as V staticCSV :: (FromRecord a, Lift (V.Vector a)) => FilePath -> Proxy a -> ExpQ staticCSV fileName ty = do contents <- runIO $ BSL.readFile fileName csv <- case decode NoHeader contents of Right a -> return $ fmap (flip asProxyTypeOf ty) a Left err -> fail err [| csv |] instance Lift a => Lift (V.Vector a) where lift v = do list <- ListE <$> mapM lift (V.toList v) return $ AppE (VarE 'V.fromList) list -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 472 bytes Desc: not available URL: From chespinoza at gmail.com Fri May 30 23:59:22 2014 From: chespinoza at gmail.com (Christian Espinoza L.) Date: Fri, 30 May 2014 19:59:22 -0400 Subject: [Haskell-beginners] Best way to have a fresh&updated Haskell installation Message-ID: Hi guys, like subject says , how can I get it? I'm using Debian Linux, and I need a guide to have a updated haskell platform, easy to update when will necessary. Haskell Platform is totally out of date, I know is required compile all, but I need a guide in order to get a standard way. Thanks in advance. Christian. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Sat May 31 00:06:11 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Fri, 30 May 2014 19:06:11 -0500 Subject: [Haskell-beginners] Best way to have a fresh&updated Haskell installation In-Reply-To: References: Message-ID: My recommendation for Linux users is to use the Ubuntu PPA[1] if it's compatible with your distro, otherwise binary distribution[2]. [1]: https://launchpad.net/~hvr/+archive/ghc [2]: https://github.com/bitemyapp/learnhaskell#windows-and-other-linux-users On Fri, May 30, 2014 at 6:59 PM, Christian Espinoza L. wrote: > Hi guys, like subject says , how can I get it? > I'm using Debian Linux, and I need a guide to have a updated haskell > platform, easy to update when will necessary. > > Haskell Platform is totally out of date, I know is required compile all, > but I need a guide in order to get a standard way. > > Thanks in advance. > Christian. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ovidiudeac at gmail.com Sat May 31 08:49:34 2014 From: ovidiudeac at gmail.com (Ovidiu Deac) Date: Sat, 31 May 2014 11:49:34 +0300 Subject: [Haskell-beginners] installing cabal 1.20 on linux mint debian edition (LMDE) In-Reply-To: References: <53871230.3080109@gmail.com> <874n08l8t4.fsf@pmade.com> Message-ID: Eventually I fixed the problem by using the Trusty apt repository and then let cabal update itself. Thanks! On Thu, May 29, 2014 at 11:06 PM, Dan Serban wrote: > What is the output of: > ghc-pkg list | grep network > ? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: