From KSergej at seznam.cz Thu Jun 6 19:06:00 2019 From: KSergej at seznam.cz (Sergej KAREL) Date: Thu, 06 Jun 2019 21:06:00 +0200 (CEST) Subject: [Haskell-beginners] How can I view a digit on the N-th position of 9^(9^9) in Haskell? Message-ID: <55T.EThB.72{hdq8EsVi.1S{MGO@seznam.cz> Hello, Im looking for setting up a solver for the determination of digit on N-th position of the high value number Best regards Sergej -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Thu Jun 6 20:12:56 2019 From: fa-ml at ariis.it (Francesco Ariis) Date: Thu, 6 Jun 2019 22:12:56 +0200 Subject: [Haskell-beginners] How can I view a digit on the N-th position of 9^(9^9) in Haskell? In-Reply-To: <55T.EThB.72{hdq8EsVi.1S{MGO@seznam.cz> References: <55T.EThB.72{hdq8EsVi.1S{MGO@seznam.cz> Message-ID: <20190606201256.os2ezycnh4qtm64c@x60s.casa> Hello Sergej, On Thu, Jun 06, 2019 at 09:06:00PM +0200, Sergej KAREL wrote: > Hello, > Im looking for setting up a solver for the determination of digit on N-th > position of the high value number The trick is to only take the appropriate reminder instead of the whole multiplication result. Something like: λ> myMult x y = rem (x * y) 100 λ> :t +d foldr1 foldr1 :: (a -> a -> a) -> [a] -> a λ> myPower x y = foldr1 myMult (replicate y x) λ> myPower 9 9 89 λ> 9^9 387420489 λ> myPower 9 (myPower 9 9) 89 Does this help? -F From KSergej at seznam.cz Fri Jun 7 04:06:27 2019 From: KSergej at seznam.cz (Sergej KAREL) Date: Fri, 07 Jun 2019 06:06:27 +0200 (CEST) Subject: [Haskell-beginners] =?utf-8?q?How_can_I_view_a_digit_on_the_N-th_?= =?utf-8?q?position_of_9=5E=289=5E9=29_in_Haskell=3F?= References: <55T.EThB.72{hdq8EsVi.1S{MGO@seznam.cz> <20190606201256.os2ezycnh4qtm64c@x60s.casa> Message-ID: Hello Francesco, Im total beginner. I read some books and online pages. I do not know, how to apply your rows if Im looking eg. digit on position  177486336 of the number string Sorry for asking so straightforward Sergej ---------- Původní e-mail ---------- Od: Francesco Ariis Komu: beginners at haskell.org Datum: 6. 6. 2019 22:13:36 Předmět: Re: [Haskell-beginners] How can I view a digit on the N-th position of 9^(9^9) in Haskell? "Hello Sergej, On Thu, Jun 06, 2019 at 09:06:00PM +0200, Sergej KAREL wrote: > Hello, > Im looking for setting up a solver for the determination of digit on N-th > position of the high value number The trick is to only take the appropriate reminder instead of the whole multiplication result. Something like: λ> myMult x y = rem (x * y) 100 λ> :t +d foldr1 foldr1 :: (a -> a -> a) -> [a] -> a λ> myPower x y = foldr1 myMult (replicate y x) λ> myPower 9 9 89 λ> 9^9 387420489 λ> myPower 9 (myPower 9 9) 89 Does this help? -F _______________________________________________ Beginners mailing list Beginners at haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners " -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at orlitzky.com Fri Jun 7 14:52:50 2019 From: michael at orlitzky.com (Michael Orlitzky) Date: Fri, 7 Jun 2019 10:52:50 -0400 Subject: [Haskell-beginners] How can I view a digit on the N-th position of 9^(9^9) in Haskell? In-Reply-To: References: <55T.EThB.72{hdq8EsVi.1S{MGO@seznam.cz> <20190606201256.os2ezycnh4qtm64c@x60s.casa> Message-ID: <074b7349-665a-bd0d-2fdd-054226060221@orlitzky.com> On 6/7/19 12:06 AM, Sergej KAREL wrote: > Hello Francesco, > Im total beginner. I read some books and online pages. > I do not know, how to apply your rows if Im looking eg. digit on > position *177486336 *of the number string > Sorry for asking so straightforward > Sergej This is a math problem. Read the first few chapters of a book on number theory -- you'll usually find something like this in the exercises. For example, exercise 2.30 in the freely-available "Elementary Number Theory: Primes, Congruences, and Secrets" by William Stein: https://wstein.org/ent/ From utprimum at gmail.com Fri Jun 7 16:21:39 2019 From: utprimum at gmail.com (Ut Primum) Date: Fri, 7 Jun 2019 18:21:39 +0200 Subject: [Haskell-beginners] How can I view a digit on the N-th position of 9^(9^9) in Haskell? In-Reply-To: <074b7349-665a-bd0d-2fdd-054226060221@orlitzky.com> References: <55T.EThB.72{hdq8EsVi.1S{MGO@seznam.cz> <20190606201256.os2ezycnh4qtm64c@x60s.casa> <074b7349-665a-bd0d-2fdd-054226060221@orlitzky.com> Message-ID: Hello Sergej, if you need to know the Nth digit of a number (i.e. 10987654321), assuming that we say that the rightmost digit is the digit 1, (so in my example 1st digit is 1), you can use: > x=10987654321 > div (mod x (10^N)) (10^(N-1)) So, *if you don't need a fast program* and you want to compute the digit in position 177486336 of 9^(9^9): > x=9^(9^9) > div (mod x (10^177486336)) (10^177486335) (it takes about 1 minute to compute it) or in general if you want to know the N-th digit : > x=9^(9^9) > div (mod x (10^N)) (10^(N-1)) Mail priva di virus. www.avg.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> Il giorno ven 7 giu 2019 alle ore 16:53 Michael Orlitzky < michael at orlitzky.com> ha scritto: > On 6/7/19 12:06 AM, Sergej KAREL wrote: > > Hello Francesco, > > Im total beginner. I read some books and online pages. > > I do not know, how to apply your rows if Im looking eg. digit on > > position *177486336 *of the number string > > Sorry for asking so straightforward > > Sergej > > This is a math problem. Read the first few chapters of a book on number > theory -- you'll usually find something like this in the exercises. > > For example, exercise 2.30 in the freely-available "Elementary Number > Theory: Primes, Congruences, and Secrets" by William Stein: > > https://wstein.org/ent/ > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hilco.wijbenga at gmail.com Tue Jun 11 03:44:30 2019 From: hilco.wijbenga at gmail.com (Hilco Wijbenga) Date: Mon, 10 Jun 2019 20:44:30 -0700 Subject: [Haskell-beginners] How to test "error"? Message-ID: Hi all, I have a function that may call "error" if the input represents a bug in my program. (Think something like PositiveInt -1; this should never happen so Maybe or Either are not applicable.) f :: Whatever f = error "This should never happen." spec :: Spec spec = describe "f" $ it "error" $ return f `shouldThrow` anyErrorCall The above does not work, I get "did not get expected exception: ErrorCall". Using "anyException" doesn't work either. How can I write a test for this? And (assuming I can get the test to work), how do I check that the error message is indeed what I expect? Cheers, Hilco From michele.alzetta at gmail.com Tue Jun 11 07:43:53 2019 From: michele.alzetta at gmail.com (Michele Alzetta) Date: Tue, 11 Jun 2019 09:43:53 +0200 Subject: [Haskell-beginners] How to test "error"? In-Reply-To: References: Message-ID: As beginner to beginner, so take this with a kg of salt ... isn't what you're looking for a construction of the Either sort? That way you get Right and Left and you can check for Left. Il giorno mar 11 giu 2019 alle ore 05:45 Hilco Wijbenga < hilco.wijbenga at gmail.com> ha scritto: > Hi all, > > I have a function that may call "error" if the input represents a bug > in my program. (Think something like PositiveInt -1; this should never > happen so Maybe or Either are not applicable.) > > f :: Whatever > f = error "This should never happen." > > spec :: Spec > spec = > describe "f" $ > it "error" $ > return f `shouldThrow` anyErrorCall > > The above does not work, I get "did not get expected exception: > ErrorCall". Using "anyException" doesn't work either. > > How can I write a test for this? And (assuming I can get the test to > work), how do I check that the error message is indeed what I expect? > > Cheers, > Hilco > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Tue Jun 11 09:39:09 2019 From: fa-ml at ariis.it (Francesco Ariis) Date: Tue, 11 Jun 2019 11:39:09 +0200 Subject: [Haskell-beginners] How to test "error"? In-Reply-To: References: Message-ID: <20190611093909.zbbu6ynnjkoxclbt@x60s.casa> Hello Hilco, On Mon, Jun 10, 2019 at 08:44:30PM -0700, Hilco Wijbenga wrote: > f :: Whatever > f = error "This should never happen." > > spec :: Spec > spec = > describe "f" $ > it "error" $ > return f `shouldThrow` anyErrorCall import qualified Control.Exception as E and E.evaluate tt' f `shouldThrow` anyErrorCall should work. Does this work? -F From hilco.wijbenga at gmail.com Wed Jun 12 05:24:09 2019 From: hilco.wijbenga at gmail.com (Hilco Wijbenga) Date: Tue, 11 Jun 2019 22:24:09 -0700 Subject: [Haskell-beginners] How to test "error"? In-Reply-To: References: Message-ID: No, I don't think you should use Either/Maybe to anticipate bugs. If your code could _legitimately_ fail (it's just something that follows from your domain) then Maybe/Either are appropriate. Otherwise, some sort of exception or "error" is more appropriate. E.g., if your domain knowledge tells you that zeroes are impossible then you would not want every division to return a Maybe just because division by zero is not defined. _Because that should never happen._ On Tue, Jun 11, 2019 at 12:44 AM Michele Alzetta wrote: > > As beginner to beginner, so take this with a kg of salt ... isn't what you're looking for a construction of the Either sort? > > That way you get Right and Left and you can check for Left. > > > > > > Il giorno mar 11 giu 2019 alle ore 05:45 Hilco Wijbenga ha scritto: >> >> Hi all, >> >> I have a function that may call "error" if the input represents a bug >> in my program. (Think something like PositiveInt -1; this should never >> happen so Maybe or Either are not applicable.) >> >> f :: Whatever >> f = error "This should never happen." >> >> spec :: Spec >> spec = >> describe "f" $ >> it "error" $ >> return f `shouldThrow` anyErrorCall >> >> The above does not work, I get "did not get expected exception: >> ErrorCall". Using "anyException" doesn't work either. >> >> How can I write a test for this? And (assuming I can get the test to >> work), how do I check that the error message is indeed what I expect? >> >> Cheers, >> Hilco >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From hilco.wijbenga at gmail.com Wed Jun 12 05:26:03 2019 From: hilco.wijbenga at gmail.com (Hilco Wijbenga) Date: Tue, 11 Jun 2019 22:26:03 -0700 Subject: [Haskell-beginners] How to test "error"? In-Reply-To: <20190611093909.zbbu6ynnjkoxclbt@x60s.casa> References: <20190611093909.zbbu6ynnjkoxclbt@x60s.casa> Message-ID: Yes, "evaluate" does the trick. Thank you. On Tue, Jun 11, 2019 at 2:39 AM Francesco Ariis wrote: > > Hello Hilco, > > On Mon, Jun 10, 2019 at 08:44:30PM -0700, Hilco Wijbenga wrote: > > f :: Whatever > > f = error "This should never happen." > > > > spec :: Spec > > spec = > > describe "f" $ > > it "error" $ > > return f `shouldThrow` anyErrorCall > > import qualified Control.Exception as E > > and > > E.evaluate tt' f `shouldThrow` anyErrorCall > > should work. Does this work? > -F > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From frederic-emmanuel.picca at synchrotron-soleil.fr Fri Jun 21 08:05:21 2019 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Fri, 21 Jun 2019 08:05:21 +0000 Subject: [Haskell-beginners] Parser using the show method Message-ID: Hello, I have a data type like this data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 deriving (Show) has you can see it is quite long. I want to write a method in order to parser a string using the show method. myparser :: Parser SpaceGroup myparser = do ... I do not want to write for each constructor something like do t <- takeText if t == "P21" then P21 else ... thanks for your help Frederic From to_br at uni-bremen.de Fri Jun 21 12:02:02 2019 From: to_br at uni-bremen.de (Tobias Brandt) Date: Fri, 21 Jun 2019 14:02:02 +0200 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: References: Message-ID: <7e877a3b-aa8f-5259-4819-f829824dc929@uni-bremen.de> Hello, you may derive an Enum instance for your SpaceGroup type and then generate all enumerations: data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132     deriving (Show, Enum) matches t =  t `elem` map show [P1 .. I4132] But of course this should not be as efficient as writing every constructor case by hand. Tobias On 6/21/19 10:05 AM, PICCA Frederic-Emmanuel wrote: > Hello, > > I have a data type like this > > data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 > deriving (Show) > > has you can see it is quite long. > > I want to write a method in order to parser a string using the show method. > > myparser :: Parser SpaceGroup > myparser = do > ... > > I do not want to write for each constructor something like > > do > t <- takeText > if t == "P21" then P21 else ... > > thanks for your help > > Frederic > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From fa-ml at ariis.it Fri Jun 21 12:09:36 2019 From: fa-ml at ariis.it (Francesco Ariis) Date: Fri, 21 Jun 2019 14:09:36 +0200 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: References: Message-ID: <20190621120936.lmmpldlwupb3xbkm@x60s.casa> Hello Frederic, On Fri, Jun 21, 2019 at 08:05:21AM +0000, PICCA Frederic-Emmanuel wrote: > Hello, > > I have a data type like this > > data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 > deriving (Show) > > has you can see it is quite long. > > I want to write a method in order to parser a string using the show method. If you can use `read` data SpaceGroup = P1 | P2 | P21 | C2 | P222 deriving (Show, Read) p = read <$> takeText and if you cannot import Text.Parsec import Text.Parsec.String import Text.Parsec.Char data SpaceGroup = P1 | P2 | P21 | C2 | P222 deriving (Enum, Show) pa :: Parser SpaceGroup pa = let ps = map (\s -> s <$ string (show s)) [P1 ..] in choice ps -- this need to be tweaked Is this what you were looking for? -F From frederic-emmanuel.picca at synchrotron-soleil.fr Fri Jun 21 12:37:37 2019 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Fri, 21 Jun 2019 12:37:37 +0000 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: <20190621120936.lmmpldlwupb3xbkm@x60s.casa> References: , <20190621120936.lmmpldlwupb3xbkm@x60s.casa> Message-ID: Hello > data SpaceGroup = P1 | P2 | P21 | C2 | P222 > deriving (Show, Read) > > p = read <$> takeText this is excalty what I wanted. In my case I use readMaybe. Do you know if a version existe of readMaybe with Text instead of String From frederic-emmanuel.picca at synchrotron-soleil.fr Fri Jun 21 13:02:26 2019 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Fri, 21 Jun 2019 13:02:26 +0000 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: References: , <20190621120936.lmmpldlwupb3xbkm@x60s.casa>, Message-ID: Re-Hello, is there a way to apply a function to all constructors of this this type ? Instead of doing this ? do sg P1 sg P2 sg P21 sg C2 sg P222 sg P2221 sg P21212 sg P212121 sg C222 sg C2221 sg F222 sg I222 sg I212121 sg P4 sg P41 sg P42 sg P43 sg P422 sg P4212 sg P4122 sg P41212 sg P4222 sg P42212 sg P4322 sg P43212 sg I4 sg I41 sg I422 sg I4122 sg P3 sg P31 sg P32 sg P312 sg P321 sg P3112 sg P3121 sg P3212 sg P3221 sg P6 sg P61 sg P62 sg P63 sg P64 sg P65 sg P622 sg P6122 sg P6522 sg P6222 sg P6422 sg P6322 sg R3 sg R32 sg P23 sg P213 sg P432 sg P4232 sg P4332 sg P4132 sg F23 sg F432 sg F4132 sg I23 sg I213 sg I432 sg I4132 From fa-ml at ariis.it Fri Jun 21 13:05:27 2019 From: fa-ml at ariis.it (Francesco Ariis) Date: Fri, 21 Jun 2019 15:05:27 +0200 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: References: <20190621120936.lmmpldlwupb3xbkm@x60s.casa> Message-ID: <20190621130527.o3jozmilr3lwnz3q@x60s.casa> On Fri, Jun 21, 2019 at 12:37:37PM +0000, PICCA Frederic-Emmanuel wrote: > Do you know if a version existe of readMaybe with Text instead of String Voilà https://hackage.haskell.org/package/readable-0.3.1/docs/Data-Readable.html Maybe is an instance of MonadPlus, so it should fit your bill From fa-ml at ariis.it Fri Jun 21 13:08:54 2019 From: fa-ml at ariis.it (Francesco Ariis) Date: Fri, 21 Jun 2019 15:08:54 +0200 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: References: <20190621120936.lmmpldlwupb3xbkm@x60s.casa> Message-ID: <20190621130854.mo5eaqbrhdwkvajl@x60s.casa> On Fri, Jun 21, 2019 at 01:02:26PM +0000, PICCA Frederic-Emmanuel wrote: > Re-Hello, > > is there a way to apply a function to all constructors of this this type ? > > Instead of doing this ? > > do sg P1 > sg P2 > sg P21 > [...] Use the same `Enum` trick Tobias Brandt described. λ> :t sequence_ $ map print [FirstConstructor ..] From frederic-emmanuel.picca at synchrotron-soleil.fr Fri Jun 21 13:14:17 2019 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Fri, 21 Jun 2019 13:14:17 +0000 Subject: [Haskell-beginners] Parser using the show method In-Reply-To: <20190621130854.mo5eaqbrhdwkvajl@x60s.casa> References: <20190621120936.lmmpldlwupb3xbkm@x60s.casa> , <20190621130854.mo5eaqbrhdwkvajl@x60s.casa> Message-ID: > Use the same `Enum` trick Tobias Brandt described. > λ> :t sequence_ $ map print [FirstConstructor ..] Yes, I just wanted to avoid typing all the list by hand. In order to avoid mistake when adding new constructors later.