Re: [Haskell-cafe] View Thunk Evaluation
That is so cool! Thank you. To anyone who's interested: Try it. It's enlightening. Tom On 6/26/11, Don Stewart don...@gmail.com wrote: Yes, via the -hpc tracing mechanism. When executed HPC generates a highlighted log of your source, and expressions that aren't evaluated will be marked up in a special color. On Sun, Jun 26, 2011 at 9:22 PM, Tom Murphy amin...@gmail.com wrote: Hi All, Is there a way to determine whether a thunk was evaluated during code's execution? Thanks, Tom ___ 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] Data.Time
Tony Morris wrote: I recently set out to write a library that required a decent time library. Having only had a flirt with Data.Time previously, I assumed it would be robust like many other haskell libraries. I don't know about consensus, but I have been massively let down. I won't go in to the details... Data.Time is in some ways like Haskell itself. Though at first glance it seems a little daunting, in reality its semantic correctness makes it easier to use, not harder, than any of its alternatives in any language that I know (and I am proficient in quite a few). Once you have caught on, it is difficult to settle for anything less. The biggest shortcoming, in my opinion, is that the documentation assumes that the reader is very familiar with the Haskell type system, and with viewing type signatures and instance lists as an integral and central part of the documentation. In particular, Haskell's standard numeric type classes and the conversion functions between them play a central role in the API of Data.Time. But you wouldn't realize that unless you have read the type signatures and instance lists in the Haddocks very carefully, and have thought about it for a while. Another problem, as Malcolm pointed out, is that because of the sheer size of the library, a quick-start guide for the common cases would be extremely helpful for newcomers. What other issues have you noticed? is there a reasonable alternative to Data.Time If I am right, and there is no alternative, I see no option but to take an excursion into writing my own. No. There are several alternatives, but I have never seen anything that has come close to Data.Time. Before you undertake writing a whole new time library, why not try writing some improved documentation for Data.Time first? I think that would give the most immediate benefit to the community, and I'm sure it would improve the quality of whatever library of your own you end up writing. Regards, Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Period of a sequence
On 06/26/2011 04:16 PM, michael rice wrote: MathWorks has the function seqperiod(x) to return the period of sequence x. Is there an equivalent function in Haskell? Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. If sequences are represented by the terms that define them (or this information is at least accessible), chances might be better, but I would still be interested how such a function works. The problem seems undecidable to me in general. On finite lists (which may be produced from infinite ones via 'take'), a naive implementation could be this: import Data.List (inits, cycle, isPrefixOf) import Debug.Trace -- Given a finite list, calculate its period. -- The first parameter controls what is accepted as a generator. See below. -- Set it to False when looking at chunks from an infinite sequence. listPeriod :: (Eq a) = Bool - [a] - Int listPeriod precisely xs = case filter (generates precisely xs) (inits xs) of -- as (last $ init xs) == xs, this will always suffice. (g:_) - length g -- length of the *shortest* generator -- @generates prec xs g@ iff @g@ generates @xs@ by repitition. If @prec@, the -- lengths have to match, too. Consider -- -- generates True [1,2,3,1,2,1,2] [1,2,3,1,2] -- False -- -- generates False [1,2,3,1,2,1,2] [1,2,3,1,2] -- True generates :: (Eq a) = Bool - [a] - [a] - Bool generates precisely xs g = if null g then null xs else (not precisely || length xs `mod` length g == 0) xs `isPrefixOf` cycle g -- Steffen ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Enumerator.List.concatMap is to Data.Iteratee.?
From: David Place d...@vidplace.com Hi: I've been studying iteratee IO. Is there a function in the iteratee package that is analogous to Data.Enumerator.List.concatMap? Iteratee's 'Data.Iteratee.Iteratee.convStream', or the more general 'Data.Iteratee.Iteratee.unfoldConvStream', would be the rough equivalents. The difference is that DEL.concatMap operates on each chunk, whereas DII.convStream operates on the stream. This makes it possible to specify operations which work on multiple chunks. If you want a function more like DEL.concatMap, it would be concatMap f = convStream (fmap f getChunk) Note that stream type 's' in enumerator should generally be translated to '[s]' in iteratee. John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Fwd: Re: Period of a sequence
Forwarding to -cafe Original Message Subject:Re: [Haskell-cafe] Period of a sequence Date: Mon, 27 Jun 2011 04:46:10 -0700 (PDT) From: michael rice nowg...@yahoo.com To: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Hi Steffen, Repeating decimals. 5/7 == 0.714285 714285 7142857 ... Period = 6 It does seem like a difficult problem. This one is eventually repeating, with Period = 3 3227/555 = 5.8144 144 144… Michael --- On *Mon, 6/27/11, Steffen Schuldenzucker /sschuldenzuc...@uni-bonn.de/*wrote: From: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Subject: Re: [Haskell-cafe] Period of a sequence To: michael rice nowg...@yahoo.com Cc: haskell-cafe@haskell.org Date: Monday, June 27, 2011, 4:32 AM On 06/26/2011 04:16 PM, michael rice wrote: MathWorks has the function seqperiod(x) to return the period of sequence x. Is there an equivalent function in Haskell? Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. If sequences are represented by the terms that define them (or this information is at least accessible), chances might be better, but I would still be interested how such a function works. The problem seems undecidable to me in general. On finite lists (which may be produced from infinite ones via 'take'), a naive implementation could be this: import Data.List (inits, cycle, isPrefixOf) import Debug.Trace -- Given a finite list, calculate its period. -- The first parameter controls what is accepted as a generator. See below. -- Set it to False when looking at chunks from an infinite sequence. listPeriod :: (Eq a) = Bool - [a] - Int listPeriod precisely xs = case filter (generates precisely xs) (inits xs) of -- as (last $ init xs) == xs, this will always suffice. (g:_) - length g -- length of the *shortest* generator -- @generates prec xs g@ iff @g@ generates @xs@ by repitition. If @prec@, the -- lengths have to match, too. Consider -- -- generates True [1,2,3,1,2,1,2] [1,2,3,1,2] -- False -- -- generates False [1,2,3,1,2,1,2] [1,2,3,1,2] -- True generates :: (Eq a) = Bool - [a] - [a] - Bool generates precisely xs g = if null g then null xs else (not precisely || length xs `mod` length g == 0) xs `isPrefixOf` cycle g -- Steffen ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fwd: Re: Period of a sequence
Michael, On 06/27/2011 01:51 PM, Steffen Schuldenzucker wrote: Forwarding to -cafe Original Message Subject: Re: [Haskell-cafe] Period of a sequence Date: Mon, 27 Jun 2011 04:46:10 -0700 (PDT) From: michael rice nowg...@yahoo.com To: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Hi Steffen, Repeating decimals. 5/7 == 0.714285 714285 7142857 ... Period = 6 It does seem like a difficult problem. This one is eventually repeating, with Period = 3 3227/555 = 5.8144 144 144… why not use the well-known division algorithm: (I hope this is readable) 3227 / 555 = 3227 `div` 555 + 3227 `mod` 555 / 555 = 5 + 452 / 555 = 5 + 0.1 * 4520 / 555 = 5 + 0.1 * (4520 `div` 555 + (4520 `mod` 555) / 555) = 5 + 0.1 * (8 + 80 / 555) = 5 + 0.1 * (8 + 0.1 * (800 / 555)) = 5 + 0.1 * (8 + 0.1 * (800 `div` 555 + (800 `mod` 555) / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 245 / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * 2450 / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 230 / 555))) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * 2300 / 555))) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * (4 + 80 / 555 *whoops*, saw 80 already, namely in line 6. Would go on like that forever if I continued like this, so the final result has to be: vvv Part before the place where I saw the '80' first 5.8 144 144 144 ... ^^^ Part after I saw the '80' So you could write a recursive function that takes as an accumulating parameter containing the list of numbers already seen: -- periodOf n m gives the periodic part of n/m as a decimal fraction. -- (or an empty list if that number has finitely many decimal places) periodOf :: (Integral a) = a - a - [a] periodOf = periodOfWorker [] where periodOfWorker seen n m | n `mod` m == 0 = ... | (n `mod` m) `elem` seen = ... | otherwise = ... --- On *Mon, 6/27/11, Steffen Schuldenzucker /sschuldenzuc...@uni-bonn.de/*wrote: From: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Subject: Re: [Haskell-cafe] Period of a sequence To: michael rice nowg...@yahoo.com Cc: haskell-cafe@haskell.org Date: Monday, June 27, 2011, 4:32 AM On 06/26/2011 04:16 PM, michael rice wrote: MathWorks has the function seqperiod(x) to return the period of sequence x. Is there an equivalent function in Haskell? Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. If sequences are represented by the terms that define them (or this information is at least accessible), chances might be better, but I would still be interested how such a function works. The problem seems undecidable to me in general. On finite lists (which may be produced from infinite ones via 'take'), a naive implementation could be this: import Data.List (inits, cycle, isPrefixOf) import Debug.Trace -- Given a finite list, calculate its period. -- The first parameter controls what is accepted as a generator. See below. -- Set it to False when looking at chunks from an infinite sequence. listPeriod :: (Eq a) = Bool - [a] - Int listPeriod precisely xs = case filter (generates precisely xs) (inits xs) of -- as (last $ init xs) == xs, this will always suffice. (g:_) - length g -- length of the *shortest* generator -- @generates prec xs g@ iff @g@ generates @xs@ by repitition. If @prec@, the -- lengths have to match, too. Consider -- -- generates True [1,2,3,1,2,1,2] [1,2,3,1,2] -- False -- -- generates False [1,2,3,1,2,1,2] [1,2,3,1,2] -- True generates :: (Eq a) = Bool - [a] - [a] - Bool generates precisely xs g = if null g then null xs else (not precisely || length xs `mod` length g == 0) xs `isPrefixOf` cycle g -- Steffen ___ 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] Data.Time
On 2011-June-27 Monday 10:15:28 Yitzchak Gale wrote: The biggest shortcoming, in my opinion, is that the documentation assumes that the reader is very familiar with the Haskell type system, and with viewing type signatures and instance lists as an integral and central part of the documentation. In particular, Haskell's standard numeric type classes and the conversion functions between them play a central role in the API of Data.Time. Making use of Haddock's recently added(?) support for comments on instance declaration would help a lot here, I think (even if it was just to draw attention to the Num/Integral/Real/Fractional instances). Before you undertake writing a whole new time library, why not try writing some improved documentation for Data.Time first? I got the impression that Tony's issue with Data.Time was the lack of some feature, not usability. Seems like the details that were omitted for the sake of constructiveness are quite relevant :) Cheers, Daniel ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fwd: Re: Period of a sequence
I've attached some code I wrote a while ago for playing with repeating decimal expansions, perhaps you'll find some of it useful. -Brent On Mon, Jun 27, 2011 at 02:21:55PM +0200, Steffen Schuldenzucker wrote: Michael, On 06/27/2011 01:51 PM, Steffen Schuldenzucker wrote: Forwarding to -cafe Original Message Subject: Re: [Haskell-cafe] Period of a sequence Date: Mon, 27 Jun 2011 04:46:10 -0700 (PDT) From: michael rice nowg...@yahoo.com To: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Hi Steffen, Repeating decimals. 5/7 == 0.714285 714285 7142857 ... Period = 6 It does seem like a difficult problem. This one is eventually repeating, with Period = 3 3227/555 = 5.8144 144 144… why not use the well-known division algorithm: (I hope this is readable) 3227 / 555 = 3227 `div` 555 + 3227 `mod` 555 / 555 = 5 + 452 / 555 = 5 + 0.1 * 4520 / 555 = 5 + 0.1 * (4520 `div` 555 + (4520 `mod` 555) / 555) = 5 + 0.1 * (8 + 80 / 555) = 5 + 0.1 * (8 + 0.1 * (800 / 555)) = 5 + 0.1 * (8 + 0.1 * (800 `div` 555 + (800 `mod` 555) / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 245 / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * 2450 / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 230 / 555))) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * 2300 / 555))) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * (4 + 80 / 555 *whoops*, saw 80 already, namely in line 6. Would go on like that forever if I continued like this, so the final result has to be: vvv Part before the place where I saw the '80' first 5.8 144 144 144 ... ^^^ Part after I saw the '80' So you could write a recursive function that takes as an accumulating parameter containing the list of numbers already seen: -- periodOf n m gives the periodic part of n/m as a decimal fraction. -- (or an empty list if that number has finitely many decimal places) periodOf :: (Integral a) = a - a - [a] periodOf = periodOfWorker [] where periodOfWorker seen n m | n `mod` m == 0 = ... | (n `mod` m) `elem` seen = ... | otherwise = ... --- On *Mon, 6/27/11, Steffen Schuldenzucker /sschuldenzuc...@uni-bonn.de/*wrote: From: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Subject: Re: [Haskell-cafe] Period of a sequence To: michael rice nowg...@yahoo.com Cc: haskell-cafe@haskell.org Date: Monday, June 27, 2011, 4:32 AM On 06/26/2011 04:16 PM, michael rice wrote: MathWorks has the function seqperiod(x) to return the period of sequence x. Is there an equivalent function in Haskell? Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. If sequences are represented by the terms that define them (or this information is at least accessible), chances might be better, but I would still be interested how such a function works. The problem seems undecidable to me in general. On finite lists (which may be produced from infinite ones via 'take'), a naive implementation could be this: import Data.List (inits, cycle, isPrefixOf) import Debug.Trace -- Given a finite list, calculate its period. -- The first parameter controls what is accepted as a generator. See below. -- Set it to False when looking at chunks from an infinite sequence. listPeriod :: (Eq a) = Bool - [a] - Int listPeriod precisely xs = case filter (generates precisely xs) (inits xs) of -- as (last $ init xs) == xs, this will always suffice. (g:_) - length g -- length of the *shortest* generator -- @generates prec xs g@ iff @g@ generates @xs@ by repitition. If @prec@, the -- lengths have to match, too. Consider -- -- generates True [1,2,3,1,2,1,2] [1,2,3,1,2] -- False -- -- generates False [1,2,3,1,2,1,2] [1,2,3,1,2] -- True generates :: (Eq a) = Bool - [a] - [a] - Bool generates precisely xs g = if null g then null xs else (not precisely || length xs `mod` length g == 0) xs `isPrefixOf` cycle g -- Steffen ___ 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 import qualified Data.Map as M import Data.Maybe import Data.Ratio import Data.List import Data.Char import Control.Arrow import Test.QuickCheck f n (d,r) = ((10*r) `divMod` n) -- Given a list and a way to extract a tag for each element, find the -- indices of the list giving the first and second occurrence of the -- first element to repeat, or Nothing if there are no repeats. findRep :: Ord b = (a - b) - [a] - Maybe (Int,Int) findRep = findRep' M.empty 0 findRep' :: Ord b = M.Map b Int - Int - (a - b) - [a] - Maybe (Int,Int) findRep' _
Re: [Haskell-cafe] Data.Time
On Mon, 27 Jun 2011 11:15:28 +0300 Yitzchak Gale g...@sefer.org wrote: The biggest shortcoming, in my opinion, is that the documentation assumes that the reader is very familiar with the Haskell type system, and with viewing type signatures and instance lists as an integral and central part of the documentation. In particular, Haskell's standard numeric type classes and the conversion functions between them play a central role in the API of Data.Time. But you wouldn't realize that unless you have read the type signatures and instance lists in the Haddocks very carefully, and have thought about it for a while. This is exactly right. Another problem, as Malcolm pointed out, is that because of the sheer size of the library, a quick-start guide for the common cases would be extremely helpful for newcomers. That would be very, very helpful. I had a few working examples things were much better. Finding a starting place, any starting place, proved to be quite elusive. Also the fact that asking for the current time traps you in IO hell, doesn't help, although it's clear that it should be that way. Brian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fwd: Re: Period of a sequence
Thanks, all. I have an evaluation copy of Mathematica and have been looking for problems to feed it. Michael --- On Mon, 6/27/11, Brent Yorgey byor...@seas.upenn.edu wrote: From: Brent Yorgey byor...@seas.upenn.edu Subject: Re: [Haskell-cafe] Fwd: Re: Period of a sequence To: haskell-cafe@haskell.org Date: Monday, June 27, 2011, 9:56 AM I've attached some code I wrote a while ago for playing with repeating decimal expansions, perhaps you'll find some of it useful. -Brent On Mon, Jun 27, 2011 at 02:21:55PM +0200, Steffen Schuldenzucker wrote: Michael, On 06/27/2011 01:51 PM, Steffen Schuldenzucker wrote: Forwarding to -cafe Original Message Subject: Re: [Haskell-cafe] Period of a sequence Date: Mon, 27 Jun 2011 04:46:10 -0700 (PDT) From: michael rice nowg...@yahoo.com To: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Hi Steffen, Repeating decimals. 5/7 == 0.714285 714285 7142857 ... Period = 6 It does seem like a difficult problem. This one is eventually repeating, with Period = 3 3227/555 = 5.8144 144 144… why not use the well-known division algorithm: (I hope this is readable) 3227 / 555 = 3227 `div` 555 + 3227 `mod` 555 / 555 = 5 + 452 / 555 = 5 + 0.1 * 4520 / 555 = 5 + 0.1 * (4520 `div` 555 + (4520 `mod` 555) / 555) = 5 + 0.1 * (8 + 80 / 555) = 5 + 0.1 * (8 + 0.1 * (800 / 555)) = 5 + 0.1 * (8 + 0.1 * (800 `div` 555 + (800 `mod` 555) / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 245 / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * 2450 / 555)) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 230 / 555))) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * 2300 / 555))) = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * (4 + 80 / 555 *whoops*, saw 80 already, namely in line 6. Would go on like that forever if I continued like this, so the final result has to be: vvv Part before the place where I saw the '80' first 5.8 144 144 144 ... ^^^ Part after I saw the '80' So you could write a recursive function that takes as an accumulating parameter containing the list of numbers already seen: -- periodOf n m gives the periodic part of n/m as a decimal fraction. -- (or an empty list if that number has finitely many decimal places) periodOf :: (Integral a) = a - a - [a] periodOf = periodOfWorker [] where periodOfWorker seen n m | n `mod` m == 0 = ... | (n `mod` m) `elem` seen = ... | otherwise = ... --- On *Mon, 6/27/11, Steffen Schuldenzucker /sschuldenzuc...@uni-bonn.de/*wrote: From: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Subject: Re: [Haskell-cafe] Period of a sequence To: michael rice nowg...@yahoo.com Cc: haskell-cafe@haskell.org Date: Monday, June 27, 2011, 4:32 AM On 06/26/2011 04:16 PM, michael rice wrote: MathWorks has the function seqperiod(x) to return the period of sequence x. Is there an equivalent function in Haskell? Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. If sequences are represented by the terms that define them (or this information is at least accessible), chances might be better, but I would still be interested how such a function works. The problem seems undecidable to me in general. On finite lists (which may be produced from infinite ones via 'take'), a naive implementation could be this: import Data.List (inits, cycle, isPrefixOf) import Debug.Trace -- Given a finite list, calculate its period. -- The first parameter controls what is accepted as a generator. See below. -- Set it to False when looking at chunks from an infinite sequence. listPeriod :: (Eq a) = Bool - [a] - Int listPeriod precisely xs = case filter (generates precisely xs) (inits xs) of -- as (last $ init xs) == xs, this will always suffice. (g:_) - length g -- length of the *shortest* generator -- @generates prec xs g@ iff @g@ generates @xs@ by repitition. If @prec@, the -- lengths have to match, too. Consider -- -- generates True [1,2,3,1,2,1,2] [1,2,3,1,2] -- False -- -- generates False [1,2,3,1,2,1,2] [1,2,3,1,2] -- True generates :: (Eq a) = Bool - [a] - [a] - Bool generates precisely xs g = if null g then null xs else (not precisely || length xs `mod` length g == 0) xs `isPrefixOf` cycle g -- Steffen ___ 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 -Inline Attachment Follows- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Help
Yes, how can i miss that... It's working now, but still it works only for the first element of the list. It prints the result only for the first string. Now when it's operational, i have just to modify it to be working for all of the elements of the list. If i run the program right now, the results are: Main p [2*3,4/2] 6 I'm not so sure, if i have to modify the caller function only, or i have to modify both the caller function and the exact function that makes the parsing? 2011/6/25 Jack Henahan jhena...@uvm.edu: The error in ghci is Couldn't match expected type `Int' with actual type `[a0]' In the expression: [] In an equation for `p': p [] = [] You've defined p as [String] - Int, but then your base case is p [] = []. [] is not an Int. I changed it to 0 and it'll compile, at least, but I'm not sure if that's the effect you're after. http://hpaste.org/48324 Edited code (really just indentation changes and the change from p [] = [] to p [] = 0) On Jun 25, 2011, at 3:19 PM, Stoyan Peev wrote: First I am using WinHugs. that's the code i made so far but it's still not working: http://hpaste.org/48318 Error: ERROR file:.\kursovazadacha.hs:36 - Type error in explicitly typed binding *** Term : p *** Type : [String] - [a] *** Does not match : [String] - Int I'm still breaking down somewhere ... 2011/6/25 Daniel Patterson lists.hask...@dbp.mm.st: what haskell compiler are you using? And what does the include line do? That does not look like a GHC error message (the only compiler I'm familiar with), but it seems like it is saying that you should not have the extra newlines between the function type signature and declaration. - that's only my guess, based on the assumption that the whitespace is being converted to a syntax with explicit semilcolon line terminations. now, looking at the actual code, the type of the parse function is [a] - [a]. This means that you can parse a list of anything into a list of anything, which doesnt make much sense. This should probably be [String] - [String] (you are parsing a list of strings to a list of strings, yes?). Now the base case of parse (the first case) makes sense, but look at the second case. parse is being given a list of elements (which you have used pattern matching to decompose, but the whole argument, (x:xs), is a list of elements). You are then passing that, unchanged, to eval. This means that eval must take the same type. Does it? how would you apply eval to each element in that list, instead of just applying it to the whole list? On Jun 24, 2011, at 4:31 PM, Stoyan Peev wrote: I found the library myself, and i already put the code in that site: http://hpaste.org/48277 That's what i have tried to do for making the task by calling the one string function by another one: include kursovazadacha parse :: [a] - [a] parse [] = [] parse (x:xs) = eval (x:xs) The error from the compiler: ERROR file:.\list.hs:3 - Syntax error in declaration (unexpected `;', possibly due to bad layout) On Fri, Jun 24, 2011 at 11:20 PM, Daniel Patterson lists.hask...@dbp.mm.st wrote: What have you tried to do in order to make it work for the list, and what error results? What is confusing about the error message? More generally, how could you transform an operation on a single string into one that does the same thing to a list of strings? You've probably talked about higher order functions in your class - would any of the common ones (filter, map, foldr) be helpful here? Would any encapsulate what you are trying to do? If you include these kinds of things, I think you'll find this community to be very helpful; without that (showing what your thought process is, why it isn't working, what seems confusing about what the haskell compiler is telling you, etc), you are not going to get help here. People here are very friendly and willing to help people learn; this is not a place to come to get an assignment finished :) Also, could you put the library you are using (I'm assuming that this is provided by your university) and the code on somewhere like hpaste.org, so that the formatting is not messed up by email, and it is syntax highlighted? On Jun 24, 2011, at 3:57 PM, Stoyan Peev wrote: Hello all, I am experiencing some issues to do my course task in university. I have to write a calculator- function in Haskell. The function argument is a list of strings and also form such list, as each string of the argument made definite action: - If the string has the form of an arithmetic _expression_ - calculate this _expression_. The string result becomes part of the list-result. If the _expression_ contains a variable which is not assigned value, the result is displayed undefined. - If the string has the form- Name = value calculated from the last _expression_ is assigned to the variable with the corresponding name in the list,
Re: [Haskell-cafe] Help
so think about the high level design for a second, and let that guide the types. then the types should guide the code. p, which I assume is the top level evaluation, is supposed to take a list of strings, and produce a list of integers (the result of evaluating the expression), right? So it should have type p :: [String] - [Int]. Now the base case is obvious - if you are given an empty list of strings, then you should give back an empty list of results. The recursive case is a little more complicated - the idea with simple recursion is that the recursive case should eventually land you at the base case, which will stop the recursion. With lists, this usually means that each application of the recursive case should call itself on the rest of the list, and somehow process the first element of the list (it doesnt have to be this way, but often is). So in your case, the recursive case should be: evaluate the first element of the list, and then call the whole function on the rest of the list. You can check this mentally by using a couple examples. In the case of a one element list, this means that it will evaluate the first (and only) element of the list, and then call itself on the rest of the list, which is an empty list, which is the bottom case and therefore ends the recursion. On a two element list, this can be checked as well. Now the types should be your best friend. You know that you want: p :: [String] - [Int] That is from the problem statement. So you write the base case first: p [] = [] Now for the recursive case, you know you want to eval the first element, and then call the function on the rest of the list. But since you need to return a list eventually, then you need to return a list in this function. You know that calling the function recursively will result in a list (the type guarantees that), so if you've evaluated the first element of a list, resulting in an Int, and you have a list of Int's that is the rest of the list, how do you combine those? Well, put the element at the beginning of the list! p (x:xs) = (eval p) : (p xs) Now this is a really really common pattern - do the same thing to every element of a list. The first thing in haskell to do if you think that what you are doing might already exist in some generalized form is to try to describe what the function is that you want. So in our case, you want a function that takes another function and applies it to every element in the list. This function would have type: (a - b) - [a] - [b] In your case the (a - b) is String - Int (the function eval), the [a] is [String], [b] is [Int]. Now if you take this and search on Hoogle (a haskell search engine: http://haskell.org/hoogle/?hoogle=%28a+-%3E+b%29+-%3E+%5Ba%5D+-%3E+%5Bb%5D ), the first result is a function called map, which does exactly what you want. So you can actually write your whole p function as: p :: [String] - [Int] p xs = map eval xs Or, if you are okay with partial application, this is equivalent to: p :: [String] - [Int] p = map eval On Jun 25, 2011, at 3:56 PM, Jack Henahan wrote: The error in ghci is Couldn't match expected type `Int' with actual type `[a0]' In the expression: [] In an equation for `p': p [] = [] You've defined p as [String] - Int, but then your base case is p [] = []. [] is not an Int. I changed it to 0 and it'll compile, at least, but I'm not sure if that's the effect you're after. http://hpaste.org/48324 Edited code (really just indentation changes and the change from p [] = [] to p [] = 0) On Jun 25, 2011, at 3:19 PM, Stoyan Peev wrote: First I am using WinHugs. that's the code i made so far but it's still not working: http://hpaste.org/48318 Error: ERROR file:.\kursovazadacha.hs:36 - Type error in explicitly typed binding *** Term : p *** Type : [String] - [a] *** Does not match : [String] - Int I'm still breaking down somewhere ... 2011/6/25 Daniel Patterson lists.hask...@dbp.mm.st: what haskell compiler are you using? And what does the include line do? That does not look like a GHC error message (the only compiler I'm familiar with), but it seems like it is saying that you should not have the extra newlines between the function type signature and declaration. - that's only my guess, based on the assumption that the whitespace is being converted to a syntax with explicit semilcolon line terminations. now, looking at the actual code, the type of the parse function is [a] - [a]. This means that you can parse a list of anything into a list of anything, which doesnt make much sense. This should probably be [String] - [String] (you are parsing a list of strings to a list of strings, yes?). Now the base case of parse (the first case) makes sense, but look at the second case. parse is being given a list of elements (which you have used pattern matching to decompose, but the whole argument, (x:xs), is a
Re: [Haskell-cafe] Help
Yeah, that really helped me :)) Finally i got the results i wanted : Main p [2*34/3,2+3,2*(6/2)] [22,5,6] There is only one more question i have about this. I have already written 2 error captures, but they don't really apply to the task i have. Here are my error captures: [(_,out)] - error (неопределено) []- 0 and here is the result Parsing p [2*34/3,2+3,2*(6 / 2),] [22,5,6,0] It's ok for me, but i have to make it, not showing anything if there are blanks. I suggest i had to make some checking in the caller function before calling the eval funcition. Also somehow i have to check the syntax of the string and if it is like =x, the result should be x= previous string Probably i have to user where for the caller function or some if cases :? 2011/6/27 Daniel Patterson lists.hask...@dbp.mm.st: so think about the high level design for a second, and let that guide the types. then the types should guide the code. p, which I assume is the top level evaluation, is supposed to take a list of strings, and produce a list of integers (the result of evaluating the expression), right? So it should have type p :: [String] - [Int]. Now the base case is obvious - if you are given an empty list of strings, then you should give back an empty list of results. The recursive case is a little more complicated - the idea with simple recursion is that the recursive case should eventually land you at the base case, which will stop the recursion. With lists, this usually means that each application of the recursive case should call itself on the rest of the list, and somehow process the first element of the list (it doesnt have to be this way, but often is). So in your case, the recursive case should be: evaluate the first element of the list, and then call the whole function on the rest of the list. You can check this mentally by using a couple examples. In the case of a one element list, this means that it will evaluate the first (and only) element of the list, and then call itself on the rest of the list, which is an empty list, which is the bottom case and therefore ends the recursion. On a two element list, this can be checked as well. Now the types should be your best friend. You know that you want: p :: [String] - [Int] That is from the problem statement. So you write the base case first: p [] = [] Now for the recursive case, you know you want to eval the first element, and then call the function on the rest of the list. But since you need to return a list eventually, then you need to return a list in this function. You know that calling the function recursively will result in a list (the type guarantees that), so if you've evaluated the first element of a list, resulting in an Int, and you have a list of Int's that is the rest of the list, how do you combine those? Well, put the element at the beginning of the list! p (x:xs) = (eval p) : (p xs) Now this is a really really common pattern - do the same thing to every element of a list. The first thing in haskell to do if you think that what you are doing might already exist in some generalized form is to try to describe what the function is that you want. So in our case, you want a function that takes another function and applies it to every element in the list. This function would have type: (a - b) - [a] - [b] In your case the (a - b) is String - Int (the function eval), the [a] is [String], [b] is [Int]. Now if you take this and search on Hoogle (a haskell search engine: http://haskell.org/hoogle/?hoogle=%28a+-%3E+b%29+-%3E+%5Ba%5D+-%3E+%5Bb%5D ), the first result is a function called map, which does exactly what you want. So you can actually write your whole p function as: p :: [String] - [Int] p xs = map eval xs Or, if you are okay with partial application, this is equivalent to: p :: [String] - [Int] p = map eval On Jun 25, 2011, at 3:56 PM, Jack Henahan wrote: The error in ghci is Couldn't match expected type `Int' with actual type `[a0]' In the expression: [] In an equation for `p': p [] = [] You've defined p as [String] - Int, but then your base case is p [] = []. [] is not an Int. I changed it to 0 and it'll compile, at least, but I'm not sure if that's the effect you're after. http://hpaste.org/48324 Edited code (really just indentation changes and the change from p [] = [] to p [] = 0) On Jun 25, 2011, at 3:19 PM, Stoyan Peev wrote: First I am using WinHugs. that's the code i made so far but it's still not working: http://hpaste.org/48318 Error: ERROR file:.\kursovazadacha.hs:36 - Type error in explicitly typed binding *** Term : p *** Type : [String] - [a] *** Does not match : [String] - Int I'm still breaking down somewhere ... 2011/6/25 Daniel Patterson lists.hask...@dbp.mm.st: what haskell compiler are
[Haskell-cafe] Fwd: Data.Time
sent from wrong account - message follows: I've found most of the time library to be quite useful, but the parsing to be worthless (I've tried to get someone to prove me wrong already, and would be happy if someone could on this thread!). Specifically, the formatTime function, if it ever strips out padding (by zeros or spaces), results in a time that is unparseable. The fact that formatTime and parseTime are not capable of being inverses of each other seems like a major flaw, when you think that this is not a parseable date: 2011/1/30 (because the month must be padded by zeros). Even though it is very easy to print, and occurs commonly in the world. Because of this, I use formatTime to write my times, and then have a custom parser to parse them back out. Which makes me think that this is a broken library On Jun 27, 2011, at 10:37 AM, bri...@aracnet.com bri...@aracnet.com wrote: On Mon, 27 Jun 2011 11:15:28 +0300 Yitzchak Gale g...@sefer.org wrote: The biggest shortcoming, in my opinion, is that the documentation assumes that the reader is very familiar with the Haskell type system, and with viewing type signatures and instance lists as an integral and central part of the documentation. In particular, Haskell's standard numeric type classes and the conversion functions between them play a central role in the API of Data.Time. But you wouldn't realize that unless you have read the type signatures and instance lists in the Haddocks very carefully, and have thought about it for a while. This is exactly right. Another problem, as Malcolm pointed out, is that because of the sheer size of the library, a quick-start guide for the common cases would be extremely helpful for newcomers. That would be very, very helpful. I had a few working examples things were much better. Finding a starting place, any starting place, proved to be quite elusive. Also the fact that asking for the current time traps you in IO hell, doesn't help, although it's clear that it should be that way. Brian ___ 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] Help
Well, if you are at all familiar with (or wanted to learn about) the Maybe type, I would suggest you use that. A brief synopsis: data Maybe a = Nothing | Just a Which means that a Maybe Int is either Nothing or or Just an int. If you were to got this path, then your p function should have type [String] - [Maybe Int] - which means that either the calculator has a response (Just 22 or Just 6 or Just whatever) or something went wrong (invalid input) and it gave Nothing. So in your case, you'd get: Parsing p [2*34/3,2+3,2*(6 / 2),] [Just 22,Just 5,Just 6,Nothing] Which really clearly demonstrates what you are trying to communicate (and importantly doesn't cause the whole program to stop running when it sees a bad input). Then in the eval function, the error cases would result in Nothing, and the good cases would result in Just n. On Jun 27, 2011, at 12:09 PM, Stoyan Peev wrote: Yeah, that really helped me :)) Finally i got the results i wanted : Main p [2*34/3,2+3,2*(6/2)] [22,5,6] There is only one more question i have about this. I have already written 2 error captures, but they don't really apply to the task i have. Here are my error captures: [(_,out)] - error (неопределено) []- 0 and here is the result Parsing p [2*34/3,2+3,2*(6 / 2),] [22,5,6,0] It's ok for me, but i have to make it, not showing anything if there are blanks. I suggest i had to make some checking in the caller function before calling the eval funcition. Also somehow i have to check the syntax of the string and if it is like =x, the result should be x= previous string Probably i have to user where for the caller function or some if cases :? 2011/6/27 Daniel Patterson lists.hask...@dbp.mm.st: so think about the high level design for a second, and let that guide the types. then the types should guide the code. p, which I assume is the top level evaluation, is supposed to take a list of strings, and produce a list of integers (the result of evaluating the expression), right? So it should have type p :: [String] - [Int]. Now the base case is obvious - if you are given an empty list of strings, then you should give back an empty list of results. The recursive case is a little more complicated - the idea with simple recursion is that the recursive case should eventually land you at the base case, which will stop the recursion. With lists, this usually means that each application of the recursive case should call itself on the rest of the list, and somehow process the first element of the list (it doesnt have to be this way, but often is). So in your case, the recursive case should be: evaluate the first element of the list, and then call the whole function on the rest of the list. You can check this mentally by using a couple examples. In the case of a one element list, this means that it will evaluate the first (and only) element of the list, and then call itself on the rest of the list, which is an empty list, which is the bottom case and therefore ends the recursion. On a two element list, this can be checked as well. Now the types should be your best friend. You know that you want: p :: [String] - [Int] That is from the problem statement. So you write the base case first: p [] = [] Now for the recursive case, you know you want to eval the first element, and then call the function on the rest of the list. But since you need to return a list eventually, then you need to return a list in this function. You know that calling the function recursively will result in a list (the type guarantees that), so if you've evaluated the first element of a list, resulting in an Int, and you have a list of Int's that is the rest of the list, how do you combine those? Well, put the element at the beginning of the list! p (x:xs) = (eval p) : (p xs) Now this is a really really common pattern - do the same thing to every element of a list. The first thing in haskell to do if you think that what you are doing might already exist in some generalized form is to try to describe what the function is that you want. So in our case, you want a function that takes another function and applies it to every element in the list. This function would have type: (a - b) - [a] - [b] In your case the (a - b) is String - Int (the function eval), the [a] is [String], [b] is [Int]. Now if you take this and search on Hoogle (a haskell search engine: http://haskell.org/hoogle/?hoogle=%28a+-%3E+b%29+-%3E+%5Ba%5D+-%3E+%5Bb%5D ), the first result is a function called map, which does exactly what you want. So you can actually write your whole p function as: p :: [String] - [Int] p xs = map eval xs Or, if you are okay with partial application, this is equivalent to: p :: [String] - [Int] p = map eval On Jun 25, 2011, at 3:56
[Haskell-cafe] Installation Failed: Haskell Platform 2011.2.0.1-i386 for OS X
I'm running OS X 10.6.7, XCode 3.2.5. When I try to install The Haskell Platform 2011.2.0.1 for Mac OS X 10.6 (Snow Leopard) it goes all the way through to running package scripts, then says installation failed I did two separate downloads, and tried the first installation two time, the second download one time. Same result all three times. Is this a problem with the package, or ? Any suggestions? Thanks, John Velman ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] toSql and fromSql, for Algebraic Data Type
Tom Murphy amin...@gmail.com wrote: The title is self-explanatory. I'd like to store information from an algebraic data type in an SQL database, but the type signature of toSql (toSql :: Data.Convertible.Base.Convertible a SqlValue = a - SqlValue) doesn't make sense to me. How is this done (how do I make an instance of a typeclass like that?) My answer is: This is tiring, redundant work. Honestly, don't do it, if you can avoid it. Rather find a type with a ready-made Convertible instance, which can represent the values of your type and convert to and from that one instead. By the way, if your type is just an enumeration of nullary constructors, note that most database systems support efficient 'ENUM' types. For example in PostgreSQL you can do this: CREATE TYPE server_status AS ENUM( 'online', 'offline', 'not available'); According to the documentation columns of type server_status will use a compact, efficient 32 bit integer representation, but on the interface you can work with regular strings, so you can just convert your algebraic type to and from strings. Even better, if you don't mind being tied to the particular database system, you can abstract away such types by using stored procedures. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife = sex) http://coder.mx/ http://ertes.de/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Hoopl: Combining CheckingFuelMonad with State?
I asked this question on StackOverflow and someone suggested using StateT. Unfortunately I don't think I can really carry around the state I'd like to: http://stackoverflow.com/questions/6495320/hoopl-how-can-i-combine-the-checkingfuelmonad-with-a-state-monad On Fri, Jun 24, 2011 at 5:56 PM, Antoine Latter aslat...@gmail.com wrote: Hi Justin, this message might be better on the haskell-cafe list (or the excellent beginers list!). When you tried to write the get/put implementations, what problems were you running into? Antoine On Sat, Jun 25, 2011 at 7:50 AM, Justin Bailey jgbai...@gmail.com wrote: I'd like to carry around some state when rewriting. It seems like CheckingFuelMonad, etc. are set up to use with other monads but I can't get the types to agree. Using MTL I've managed to come up with these types: newtype RewriteOnce a = R (State Bool a) deriving (Monad) instance MonadState s (CheckingFuelMonad RewriteOnce) where get = undefined put = undefined But I cannot write the definitions for get and put. Is this possible or am I misundersanding CheckingFuelMonad? Is there a better approach altogether? Thanks in advance for any help! Justin ___ Glasgow-haskell-users mailing list glasgow-haskell-us...@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fwd: Re: Period of a sequence
On 2011-06-27 13:51, Steffen Schuldenzucker wrote: Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. What about sequences that can be specified in terms of 'iterate': import Control.Arrow (first) -- Return the non-repeating part of a sequence followed by the repeating part. -- -- iterate f x0 == in a ++ cycle b -- where (a,b) = findCycle f x0 -- -- see http://en.wikipedia.org/wiki/Cycle_detection findCycle :: Eq a = (a - a) - a - ([a],[a]) findCycle f x0 = go1 (f x0) (f (f x0)) where go1 x y | x == y= go2 x0 x | otherwise = go1 (f x) (f (f y)) go2 x y | x == y= ([], x : go3 x (f x)) | otherwise = first (x:) (go2 (f x) (f y)) go3 x y | x == y= [] | otherwise = y : go3 x (f y) -- diverges if not periodic seqPeriod :: Eq a = (a - a) - a - Integer seqPeriod f x0 = length . snd $ findCycle f x0 Twan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Period of a sequence
On 27/06/2011, at 8:32 PM, Steffen Schuldenzucker wrote: On 06/26/2011 04:16 PM, michael rice wrote: MathWorks has the function seqperiod(x) to return the period of sequence x. Is there an equivalent function in Haskell? Could you specify what exactly the function is supposed to do? Google turns up the documentation pretty quickly. The period p is computed as the minimum length of a subsequence x(1:p) of x that repeats itself continuously every p samples in x. http://www.mathworks.com/help/toolbox/signal/seqperiod.html (The misuse of the word continuously is theirs, not mine.) Incomplete repetitions at the end are allowed, so by that definition every finite sequence _has_ a period which can be found in quadratic time. The times I've wanted something like this, the data have been noisy enough that the implied algorithm would have been guaranteed to be useless. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Enumerators, Enumeratees and Iterators
Hey Café. I've been playing with Enumerators, Iteratees and Enumeratees today after having spent a few days writing a server application using lazy IO, then reading slides from Oleg's DEFUN8 talk notes, and I quote: Lazy IO in serious, server-side programming is unprofessional, I can talk a lot how disturbingly, distressingly wrong lazy IO is theoretically, how it breaks all equational reasoning after which my OCD self has been eating me up to take a proper look at this, so here I am. I'm already beginning to see how Iteratees, Enumerators and Enumeratees can be nifty, but I have a couple of questions about a couple of ideas and whether, and if so how they can be implemented. The first question is, I think, to be solved with enumeratees but I can't really grok how. Let's say I have an iteratee that consumes all input. Is it possible to implement an enumeratee or something else to stick between the enumerator and the iteratee to basically modify the input to the iteratee to only be a part of the input? Something like this: enumFile someFile printFileLines -- prints file with prepended line numbers enumFile someFile ?? onlyGet10Lines printFileLines -- print only 10 lines The second question could actually have an application in the server I'm writing. I was wondering if it was possible to write iteratees/enumerators that would only generate/consume when a certain something was the next chunk to be processed? It's a bit hard to explain, but let me try to give you an example. Let's say my application takes input from the network which contains commands that the server reacts to. Let's say I have a list of command handlers that I want to run this command through, but the handlers will only execute when they are passed the command that they are responsible for handling. Can this list of command handlers be enumerators/iteratees/enumeratees? E.g. is it possible to 'concat' them, making them each peek at the next chunk to see if it's theirs to handle and put it back if it isn't? I know this is possible to do differently and I have an idea for how I would do this, but I'm wondering if this is possible. If I can get answers to either or both of the questions it would help me a bit on my way to completely realize the roles that enumerators vs. iteratees vs. enumeratees have, as the lines seem a bit blurred -- sometimes, anyway. Thanks in advance. :) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fwd: Re: Period of a sequence
On Mon, Jun 27, 2011 at 4:25 PM, Twan van Laarhoven twa...@gmail.comwrote: On 2011-06-27 13:51, Steffen Schuldenzucker wrote: Could you specify what exactly the function is supposed to do? I am pretty sure that a function like seqPeriod :: (Eq a) = [a] - Maybe Integer -- Nothing iff non-periodic cannot be written. What about sequences that can be specified in terms of 'iterate': This is beginning to be reminiscent of the recent paper by Max Bolingbroke, termination combinators forever (great paper). http://www.cl.cam.ac.uk/~mb566/papers/termination-combinators-hs11.pdf import Control.Arrow (first) -- Return the non-repeating part of a sequence followed by the repeating part. -- -- iterate f x0 == in a ++ cycle b -- where (a,b) = findCycle f x0 -- -- see http://en.wikipedia.org/wiki/**Cycle_detectionhttp://en.wikipedia.org/wiki/Cycle_detection findCycle :: Eq a = (a - a) - a - ([a],[a]) findCycle f x0 = go1 (f x0) (f (f x0)) where go1 x y | x == y= go2 x0 x | otherwise = go1 (f x) (f (f y)) go2 x y | x == y= ([], x : go3 x (f x)) | otherwise = first (x:) (go2 (f x) (f y)) go3 x y | x == y= [] | otherwise = y : go3 x (f y) -- diverges if not periodic seqPeriod :: Eq a = (a - a) - a - Integer seqPeriod f x0 = length . snd $ findCycle f x0 Twan __**_ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe