[Haskell-cafe] why does a foldr not have a space leak effect?
Hi, I wonder why a foldr does not have a space leak effect? Any hints? Thanks. Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] why does a foldr not have a space leak effect?
Foldl has the space leak effect, and that's why foldl' has been recommended. On Monday, July 23, 2012 9:44:59 PM UTC-5, Ivan Lazar Miljenovic wrote: (Trying again using the real mailing list address rather than google groups) On 24 July 2012 12:37, Qi Qi qiqi...@gmail.com wrote: Hi, I wonder why a foldr does not have a space leak effect? Any hints? Thanks. Why should it? Does it not depend on the function you're folding, the type of data you're folding over, how you use it, etc.? Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com http://IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] why does a foldr not have a space leak effect?
This question actually was risen when I read a relevant part in the RWH book. Now it's much clearer to me. Thanks Ivan! On Monday, July 23, 2012 10:04:00 PM UTC-5, Ivan Lazar Miljenovic wrote: On 24 July 2012 12:52, Qi Qi qiqi...@gmail.com wrote: Foldl has the space leak effect, and that's why foldl' has been recommended. foldl can have a space leak due to accumulation of thunks: foldl f a [b,c,d] = f (f (f a b) c) d ^^ Building up a chain of functions. As such, these thunks are kept around until the result is actually needed. foldl' forces each previous thunk first. foldr f a [b,c,d] = f b (f c (f d a)) ^^ This also builds up a chain of functions... but foldr is typically used in cases where f is lazy in the second argument. For example, and = foldr (); so as soon as it hits one False value, it doesn't need to go on with the rest of the list. Thus, it's not that foldr doesn't necessarily have a space-leak effect; it's just that foldr is used in cases where you're either a) using something that should stop traversing the list when it reaches a certain type of value, or b) you want to preserve the list order (e.g. using foldr to implement map). foldl is used in cases where the entire list _does_ need to be consumed, so the possibility of a space leak becomes more of an issue. Note also the recommendation of foldl' is a bit of a premature optimisation: it isn't _always_ needed (short lists, values are evaluated soon after the fold, etc.), but it's easier to always prefer foldl' over foldl rather than having to go through your code base and selectively replace foldl with foldl'. On Monday, July 23, 2012 9:44:59 PM UTC-5, Ivan Lazar Miljenovic wrote: (Trying again using the real mailing list address rather than google groups) On 24 July 2012 12:37, Qi Qi qiqi...@gmail.com wrote: Hi, I wonder why a foldr does not have a space leak effect? Any hints? Thanks. Why should it? Does it not depend on the function you're folding, the type of data you're folding over, how you use it, etc.? Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com http://IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com http://IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] wondering about a MonadIO instance for a heap data type
Hi, I was wondering about creating an instance of MonadIO for a heap data. Any hints? data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show) The reason is that I want to use liftIO during a heapsort to print out intermediate results. Thanks. Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] wondering about a MonadIO instance for a heap data type
List [] is a monad, why not for heap data. Heap data could be an instance of Monad too. I have the heapsort function, and just wanted to rewrite a verbose version of it by using liftIO. But I would look into Debug.Trace. Thanks for your hint. Qi On Wednesday, July 11, 2012 5:28:17 PM UTC-5, Eugene Kirpichov wrote: Use Debug.Trace. It does not make sense to declare that heap is a monad, as a monad is an abstraction of sequencing computations, and a heap is not an abstraction of sequencing computations at all. You don't make your String class implement the rendering engine interface just because you want to use it in a computer game program, equally you dont pretend that a heap is a way of sequencing computations just because you want to sequence computations related to heaps. The actual computation in your case is the heapsort function, not the heap. If you absolutely must use IO, add IO to the functions type. 11.07.2012, в 15:19, Qi Qi qiqi...@gmail.com написал(а): Hi, I was wondering about creating an instance of MonadIO for a heap data. Any hints? data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show) The reason is that I want to use liftIO during a heapsort to print out intermediate results. Thanks. Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] haskell.org website ?
Hi, Haskell.org website seems dropping offline at this moment. Anyone know what's happening to it? Thanks, Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] how to print out intermediate results in a recursive function?
Brilliant! That's what I was looking for. Thanks for all the replies. Sometimes, I suspect that Haskell not only makes easier of the hard things for imperative programming languages but also makes harder of some easy things. Roman Cheplyaka r...@ro-che.info writes: * Steve Horne sh006d3...@blueyonder.co.uk [2012-02-04 11:54:44+] On 04/02/2012 08:46, MigMit wrote: Well, if you want that in production, not for debugging purposes, you should change the type signature of mergesort so that it uses some monad. Printing requires IO monad; however, I would advise to collect all intermediate results using Writer monad, and print them afterwards: mergesort [] = return [] mergesort [x] = return [x] mergesort xs = do tell [xs] -- that's right, [xs], not xs let (as, bs) = splitAt (length xs `quot` 2) xs liftM2 merge (mergesort as) (mergesort bs) Also, don't forget that IO actions are values too. IOW, your list of intermediate results can be a list of IO actions, effectively deferring their execution until later. No need to keep them in a list -- you can compose them as you go, while still deferring the execution. import Data.Monoid import Control.Monad.Writer newtype O = O (IO ()) instance Monoid O where mempty = O $ return () mappend (O a) (O b) = O $ a b mergesort [] = return [] mergesort [x] = return [x] mergesort xs = do trace xs let (as, bs) = splitAt (length xs `quot` 2) xs liftM2 merge (mergesort as) (mergesort bs) where trace = tell . O . print main = let (_, O trace) = runWriter $ mergesort [3,1,4,2,10,8,4,7,6] in trace -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] how to print out intermediate results in a recursive function?
Hello, I have a question;how can I print out the intermediate number lists in a mergesort recursive function like the following one. merge [] ys = ys merge xs [] = xs merge (x:xs) (y:ys) = if x = y then x : merge xs (y:ys) else y : merge (x:xs) ys mergesort [] = [] mergesort [x] = [x] mergesort xs = let (as, bs) = splitAt (length xs `quot` 2) xs in merge (mergesort as) (mergesort bs) main = do print $ mergesort [5,4,3,2,1] In the main function, it only prints out the final number list. But I'd like to print out the number lists in every recursive level. How can I do that? Thanks. -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] how long could it wait for an account on hackageDB?
Hello all, I wanted to have an account on hackageDB to upload a package. I followed http://hackage.haskell.org/packages/accounts.html, and sent an email to r...@soi.city.ac.uk for asking an account. It's been a couple days that I haven't heard back. Is this normal, or I have to do something else? Thanks, -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] have anyone tried GHC 7.0.2
ghc-pkg list returns these: /usr/local/lib/ghc-7.0.2/package.conf.d: Cabal-1.10.1.0 array-0.3.0.2 base-4.3.1.0 bin-package-db-0.0.0.0 bytestring-0.9.1.10 containers-0.4.0.0 directory-1.1.0.0 extensible-exceptions-0.1.1.2 ffi-1.0 filepath-1.2.0.0 (ghc-7.0.2) (ghc-binary-0.5.0.2) ghc-prim-0.2.0.0 (haskell2010-1.0.0.0) haskell98-1.1.0.1 hpc-0.5.0.6 integer-gmp-0.2.0.3 old-locale-1.0.0.2 old-time-1.0.0.6 pretty-1.0.1.2 process-1.0.1.5 random-1.0.0.3 rts-1.0 template-haskell-2.5.0.0 time-1.2.0.3 unix-2.4.2.0 /home/qi/.ghc/i386-linux-7.0.2/package.conf.d: HTTP-4000.1.1 QuickCheck-2.4.0.1 X11-1.5.0.0 X11-xft-0.3 binary-0.5.0.2 containers-0.3.0.0 cpphs-1.11 directory-1.1.0.0 filepath-1.1.0.4 haskell98-1.1.0.1 hinotify-0.3.1 hscolour-1.17 language-c-0.3.2.1 mtl-1.1.1.1 mtl-2.0.1.0 {network-2.2.1.10} network-2.3.0.2 parsec-2.1.0.1 parsec-3.1.1 primitive-0.3.1 process-1.0.1.5 stm-2.2.0.1 storable-complex-0.2.1 syb-0.3 transformers-0.2.2.0 uniplate-1.6 utf8-string-0.3.6 vector-0.7.0.1 xmonad-0.9.2 xmonad-contrib-0.9.2 zlib-0.5.3.1 ghc-pkg check returns this: There are problems in package network-2.2.1.10: dependency parsec-3.1.1-583092e3fd1a17b79365b197467a185d doesn't exist The following packages are broken, either because they have a problem listed above, or because they depend on a broken package. network-2.2.1.10 I installed parsec and network via cabal. Do you know how can I remove network-2.2.1.10 that is not needed because a higher version exists. Should I delete anything under ~/.ghc and ~/.cabal, and reinstall ghc and cabal-install, and those pacakges installed before using cabal? Thanks for your link of the Haskell-platform. I will wait for the official release and hope it will be soon. Christian Maeder christian.mae...@dfki.de writes: What does ghc-pkg list and ghc-pkg check say? Cabal-1.10.1.0, directory-1.1.0.0 and process-1.0.1.5 should be there after installation of GHC 7.0.2. (I've actually installed cabal-install-0.10.0 using an older cabal, but that does not work on macs and may not be the recommended way.) I think I've used http://www.galois.com/~dons/code/2011.2.0.0/haskell-platform-2011.2.0.0.tar.gz from http://www.galois.com/~dons/code/2011.2.0.0/, but they are not official yet. C. Am 08.03.2011 19:20, schrieb Qi Qi: Hi, After I installed GHC 7.0.2 and cabal-install 0.10.0 using Cabal library 1.10.1, I found that some package failed to install. For example like Happy, I got the following error: cabal install happy Resolving dependencies... command line: cannot satisfy -package Cabal-1.10.1.0: Cabal-1.10.1.0-64e8f05722ea5bbdf07df2e5a3491105 is unusable due to missing or recursive dependencies: directory-1.1.0.0-85d1d0f9d96dffdacf64f3cc6fba6f2f process-1.0.1.5-4cab1bf0666275ac101dd48c7565b64c (use -v for more information) cabal: Error: some packages failed to install: happy-1.18.6 failed during the configure step. The exception was: ExitFailure 1 Besides happy package, some other packages like ghc-mod, hmatrix also failed to install. Does anyone else have the same issue after installed ghc 7.0.2? By the way, does anyone hear anything about Haskell-platform 2011.2 ? Thanks! -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] some problem with Cabal
Hi, When I tried the haskell platform 2011.2 source, from here http://code.galois.com/darcs/haskell-platform/download-website/linux.html . Configuration passed successfully. But when making, it gives the following error: scripts/build.sh ** Scanning system for any installed Haskell Platform components... Found:None. New packages to install: None! All done. ** Building /usr/local/bin/ghc --make Setup -o Setup -package Cabal-1.10.1.0 command line: cannot satisfy -package Cabal-1.10.1.0: Cabal-1.10.1.0-64e8f05722ea5bbdf07df2e5a3491105 is unusable due to missing or recursive dependencies: directory-1.1.0.0-85d1d0f9d96dffdacf64f3cc6fba6f2f process-1.0.1.5-4cab1bf0666275ac101dd48c7565b64c (use -v for more information) Error: Compiling the Setup script failed make: *** [build.stamp] Error 2 I think this is the same problem as I got during installing some other packages via cabal such as hmatrix, ghc-mod, happy and etc. Does anyone have any idea about how to solve it? Thanks! -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] have anyone tried GHC 7.0.2
Hi, After I installed GHC 7.0.2 and cabal-install 0.10.0 using Cabal library 1.10.1, I found that some package failed to install. For example like Happy, I got the following error: cabal install happy Resolving dependencies... command line: cannot satisfy -package Cabal-1.10.1.0: Cabal-1.10.1.0-64e8f05722ea5bbdf07df2e5a3491105 is unusable due to missing or recursive dependencies: directory-1.1.0.0-85d1d0f9d96dffdacf64f3cc6fba6f2f process-1.0.1.5-4cab1bf0666275ac101dd48c7565b64c (use -v for more information) cabal: Error: some packages failed to install: happy-1.18.6 failed during the configure step. The exception was: ExitFailure 1 Besides happy package, some other packages like ghc-mod, hmatrix also failed to install. Does anyone else have the same issue after installed ghc 7.0.2? By the way, does anyone hear anything about Haskell-platform 2011.2 ? Thanks! -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] can Haskell do everyting as we want?
As more I learn haskell, I am more interested in this function programming language. I am intended to more focus on haskell than other languages like python, Java, or C++. But I am still wonder whether haskell can do everyting as other languages do, such as python, perl, Java and C++. Is there anyone happen to come into any tasks that haskell is not able to achieve? Thanks. -- Qi Qi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe