From 1625143974 at qq.com Wed Apr 4 06:37:13 2018 From: 1625143974 at qq.com (=?utf-8?B?5riF5769?=) Date: Wed, 4 Apr 2018 14:37:13 +0800 Subject: [Haskell-beginners] I have a question in designing a 'show' function Message-ID: here is the question: Write a function showAreaOfCircle which, given the radius of a circle, calculates the area of the circle, `showAreaOfCircle 12.3`  ⇒ `"The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2"` my solution is: showAreaOfCircle :: Show a => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2" but there is an error • Could not deduce (Num a) arising from a use of ‘*’ from the context: Show a bound by the type signature for: showAreaOfCircle :: forall a. Show a => a -> String at FirstStep.hs:20:1-41 Possible fix: add (Num a) to the context of the type signature for: showAreaOfCircle :: forall a. Show a => a -> String • In the first argument of ‘show’, namely ‘(pi * x * x)’ In the second argument of ‘(+)’, namely ‘show (pi * x * x)’ In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’ I had tried to change the type declaration to Show a => Num a => a -> String i guess there must be a problem with pi. Can someone help me with the error message? Great thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From mukeshtiwari.iiitm at gmail.com Wed Apr 4 06:51:43 2018 From: mukeshtiwari.iiitm at gmail.com (mukesh tiwari) Date: Wed, 4 Apr 2018 16:51:43 +1000 Subject: [Haskell-beginners] I have a question in designing a 'show' function In-Reply-To: References: Message-ID: Add one more constraint that a is member of Floating type class and change your + to ++ " " + show(pi * x * x showAreaOfCircle :: (Floating a, Show a) => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " ++ show (pi * x * x) ++ "cm^2" *Tmp> showAreaOfCircle 10 "The area of a circle with radius10.0is about 314.1592653589793cm^2" *Tmp> :t showAreaOfCircle 10 showAreaOfCircle 10 :: String *Tmp> :t showAreaOfCircle showAreaOfCircle :: (Show a, Floating a) => a -> String *Tmp> :t showAreaOfCircle 10.12 showAreaOfCircle 10.12 :: String *Tmp> showAreaOfCircle 10.12 "The area of a circle with radius10.12is about 321.74432666180644cm^2" *Tmp> Best, Mukesh On Wed, Apr 4, 2018 at 4:37 PM, 清羽 <1625143974 at qq.com> wrote: > > here is the question: > > Write a function showAreaOfCircle which, given the radius of a circle, > calculates the area of the circle, > > `showAreaOfCircle 12.3`  ⇒ `"The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2"` > > my solution is: > > showAreaOfCircle :: Show a => a -> String > showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2" > > but there is an error > > • Could not deduce (Num a) arising from a use of ‘*’ > from the context: Show a > bound by the type signature for: > showAreaOfCircle :: forall a. Show a => a -> String > at FirstStep.hs:20:1-41 > Possible fix: > add (Num a) to the context of > the type signature for: > showAreaOfCircle :: forall a. Show a => a -> String > • In the first argument of ‘show’, namely ‘(pi * x * x)’ > In the second argument of ‘(+)’, namely ‘show (pi * x * x)’ > In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’ > > I had tried to change the type declaration to > > Show a => Num a => a -> String > > i guess there must be a problem with pi. > > Can someone help me with the error message? Great thanks > > _______________________________________________ > 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 joel.neely at gmail.com Wed Apr 4 11:19:11 2018 From: joel.neely at gmail.com (Joel Neely) Date: Wed, 4 Apr 2018 06:19:11 -0500 Subject: [Haskell-beginners] I have a question in designing a 'show' function In-Reply-To: References: Message-ID: You only have a single plus sign before the second invocation of show: ++ " " + show(pi * x * x) ++ "cm^2" Don't you mean concatenation? ++ " " ++ show(pi * x * x) ++ "cm^2" Good luck, -jn- On Wed, Apr 4, 2018 at 1:37 AM, 清羽 <1625143974 at qq.com> wrote: > > here is the question: > > Write a function showAreaOfCircle which, given the radius of a circle, > calculates the area of the circle, > > `showAreaOfCircle 12.3`  ⇒ `"The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2"` > > my solution is: > > showAreaOfCircle :: Show a => a -> String > showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2" > > but there is an error > > • Could not deduce (Num a) arising from a use of ‘*’ > from the context: Show a > bound by the type signature for: > showAreaOfCircle :: forall a. Show a => a -> String > at FirstStep.hs:20:1-41 > Possible fix: > add (Num a) to the context of > the type signature for: > showAreaOfCircle :: forall a. Show a => a -> String > • In the first argument of ‘show’, namely ‘(pi * x * x)’ > In the second argument of ‘(+)’, namely ‘show (pi * x * x)’ > In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’ > > I had tried to change the type declaration to > > Show a => Num a => a -> String > > i guess there must be a problem with pi. > > Can someone help me with the error message? Great thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -- Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato -------------- next part -------------- An HTML attachment was scrubbed... URL: From 1625143974 at qq.com Wed Apr 4 12:14:57 2018 From: 1625143974 at qq.com (=?utf-8?B?5riF5769?=) Date: Wed, 4 Apr 2018 20:14:57 +0800 Subject: [Haskell-beginners] =?utf-8?b?5Zue5aSN77yaICBJIGhhdmUgYSBxdWVz?= =?utf-8?q?tion_in_designing_a_=27show=27function?= In-Reply-To: References: Message-ID: Yes.And I finally make the function error-free.Thanks ------------------ 原始邮件 ------------------ 发件人: "Joel Neely"; 发送时间: 2018年4月4日(星期三) 晚上7:19 收件人: "The Haskell-Beginners Mailing List - Discussion of primarilybeginner-level topics related to Haskell"; 主题: Re: [Haskell-beginners] I have a question in designing a 'show'function You only have a single plus sign before the second invocation of show: ++ " " + show(pi * x * x) ++ "cm^2"Don't you mean concatenation? ++ " " ++ show(pi * x * x) ++ "cm^2" Good luck, -jn- On Wed, Apr 4, 2018 at 1:37 AM, 清羽 <1625143974 at qq.com> wrote: here is the question: Write a function showAreaOfCircle which, given the radius of a circle, calculates the area of the circle, `showAreaOfCircle 12.3`  ⇒ `"The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2"` my solution is: showAreaOfCircle :: Show a => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2" but there is an error • Could not deduce (Num a) arising from a use of ‘*’ from the context: Show a bound by the type signature for: showAreaOfCircle :: forall a. Show a => a -> String at FirstStep.hs:20:1-41 Possible fix: add (Num a) to the context of the type signature for: showAreaOfCircle :: forall a. Show a => a -> String • In the first argument of ‘show’, namely ‘(pi * x * x)’ In the second argument of ‘(+)’, namely ‘show (pi * x * x)’ In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’ I had tried to change the type declaration to Show a => Num a => a -> String i guess there must be a problem with pi. Can someone help me with the error message? Great thanks _______________________________________________ Beginners mailing list Beginners at haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -- Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato -------------- next part -------------- An HTML attachment was scrubbed... URL: From 1625143974 at qq.com Wed Apr 4 14:08:04 2018 From: 1625143974 at qq.com (=?utf-8?B?5riF5769?=) Date: Wed, 4 Apr 2018 22:08:04 +0800 Subject: [Haskell-beginners] A question regarding the syntax behind the parenthesis and type class. Message-ID: this is the original question: Write a function showAreaOfCircle which, given the radius of a circle, calculates the area of the circle, showAreaOfCircle 12.3  ⇒ "The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2" Use the show function, as well as the predefined value pi :: Floating a => a to write showAreaOfCircle. the type signature works for this function is: showAreaOfCircle :: (Show a, Floating a) => a -> a -> String my question is : I don't quite understand the syntax behind the parenthesis and the type class Floating a. Since pi is not a function, and hence the entire function only takes one argument which belongs to the type class Show, why should i put a Floating a in the type signature in the first place?(i come up with this type signature because of the error message and the suggestion stated above) Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed Apr 4 14:20:40 2018 From: toad3k at gmail.com (David McBride) Date: Wed, 4 Apr 2018 10:20:40 -0400 Subject: [Haskell-beginners] A question regarding the syntax behind the parenthesis and type class. In-Reply-To: References: Message-ID: Because you are not showing x or pi, you are showing (pi * x * x). And the type of the (*) operator is (*) :: Num a => a -> a -> a. So if pi is Floating => a, then x must also be the same type as pi, and thus must be have the same Floating constraint. Keep in mind that Floating is a Fractional, and Fractional is a Num, so you know that if you have a Floating it is already an instance of Num, so you don't have to worry about that constraint. On Wed, Apr 4, 2018 at 10:08 AM, 清羽 <1625143974 at qq.com> wrote: > this is the original question: > > 1. > > Write a function showAreaOfCircle which, given the radius of a circle, > calculates the area of the circle, > > showAreaOfCircle 12.3 ⇒ "The area of a circle with radius 12.3cm is > about 475.2915525615999 cm^2" > > Use the show function, as well as the predefined value pi :: Floating > a => a to write showAreaOfCircle. > > the type signature works for this function is: showAreaOfCircle :: (Show > a, Floating a) => a -> a -> String > > my question is : I don't quite understand the syntax behind the > parenthesis and the type class Floating a. Since pi is not a function, and > hence the entire function only takes one argument which belongs to the type > class Show, why should i put a Floating a in the type signature in the > first place?(i come up with this type signature because of the error > message and the suggestion stated above) Thanks > > > _______________________________________________ > 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 1625143974 at qq.com Tue Apr 10 12:47:17 2018 From: 1625143974 at qq.com (=?utf-8?B?5riF5769?=) Date: Tue, 10 Apr 2018 20:47:17 +0800 Subject: [Haskell-beginners] May I ask what is the meaning of ch <= 'z' and 'a' <= ch. Message-ID: Hello everyone, I have a simple question here Define a function isLower :: Char -> Bool which returns True if a given character is a lower case letter. You can use the fact that characters are ordered, and for all lower case letters ch we have ′a′ ≤ ch and ch ≤ ′z′. Alternatively, you can use the fact that ['a'..'z'] evaluates to a list containing all lower case letters. And May I ask what is the meaning of ch <= 'z' and 'a' <= ch? I copied this as a condition and solved the problem.here is my work. isLower :: Char -> Bool isLower x = if x ch <= 'z' && 'a' <= ch then True else False. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Tue Apr 10 13:38:58 2018 From: fa-ml at ariis.it (Francesco Ariis) Date: Tue, 10 Apr 2018 15:38:58 +0200 Subject: [Haskell-beginners] May I ask what is the meaning of ch <= 'z' and 'a' <= ch. In-Reply-To: References: Message-ID: <20180410133858.cl7yp2pepvvc4k7b@x60s.casa> On Tue, Apr 10, 2018 at 08:47:17PM +0800, 清羽 wrote: > And May I ask what is the meaning of ch <= 'z' and 'a' <= ch? I copied this as a condition and solved the problem.here is my work. > isLower :: Char -> Bool > isLower x = if x ch <= 'z' && 'a' <= ch then True else False. > Thanks in advance. Hello 清羽, Char is an instance of Ord, so you can use <, compare, etc. with it. <= and >= stand for "less or equal than" and "greater or equal than". As with many enumeration types, the order is a bit arbitrary λ> minBound :: Char '\NUL' λ> maxBound :: Char '\1114111' λ> ['\$'..'Z'] "\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" -- so * will be < than, say, A λ> '*' < 'A' True Does that answer your question? From karim.salah2048 at gmail.com Tue Apr 10 15:35:56 2018 From: karim.salah2048 at gmail.com (Karim Salah) Date: Tue, 10 Apr 2018 17:35:56 +0200 Subject: [Haskell-beginners] 2018 report Message-ID: hi everyone, I am wondering how can i submit a contribution in the Haskell report. It is stated up here: http://mail.haskell.org/pipermail/haskell-cafe/2018-March/128829.html that i shall send my contribution hcar at haskell.org which i have no clue how to this i will appreciate any help Thanks in advance -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Tue Apr 10 15:56:45 2018 From: fa-ml at ariis.it (Francesco Ariis) Date: Tue, 10 Apr 2018 17:56:45 +0200 Subject: [Haskell-beginners] 2018 report In-Reply-To: References: Message-ID: <20180410155645.wqlml4w4sfczdpmm@x60s.casa> On Tue, Apr 10, 2018 at 05:35:56PM +0200, Karim Salah wrote: > hi everyone, > I am wondering how can i submit a contribution in the Haskell report. > It is stated up here: > http://mail.haskell.org/pipermail/haskell-cafe/2018-March/128829.html > that i shall send my contribution hcar at haskell.org which i have no clue > how to this > i will appreciate any help > Thanks in advance Hey Karim, it is "please send your contributions to *hcar* at haskell.org" in the same (antispam) fashion as "karim.salah2048 at gmail.com" -F From karim.salah2048 at gmail.com Tue Apr 10 17:06:21 2018 From: karim.salah2048 at gmail.com (Karim Salah) Date: Tue, 10 Apr 2018 19:06:21 +0200 Subject: [Haskell-beginners] 2018 report In-Reply-To: <20180410155645.wqlml4w4sfczdpmm@x60s.casa> References: <20180410155645.wqlml4w4sfczdpmm@x60s.casa> Message-ID: Now i get it thanks a lot On Tue, Apr 10, 2018 at 5:56 PM, Francesco Ariis wrote: > On Tue, Apr 10, 2018 at 05:35:56PM +0200, Karim Salah wrote: > > hi everyone, > > I am wondering how can i submit a contribution in the Haskell report. > > It is stated up here: > > http://mail.haskell.org/pipermail/haskell-cafe/2018-March/128829.html > > that i shall send my contribution hcar at haskell.org which i have no > clue > > how to this > > i will appreciate any help > > Thanks in advance > > Hey Karim, > it is "please send your contributions to *hcar* at haskell.org" > in the same (antispam) fashion as "karim.salah2048 at gmail.com" > -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 tanuki at gmail.com Tue Apr 10 17:51:17 2018 From: tanuki at gmail.com (Theodore Lief Gannon) Date: Tue, 10 Apr 2018 17:51:17 +0000 Subject: [Haskell-beginners] May I ask what is the meaning of ch <= 'z' and 'a' <= ch. In-Reply-To: References: Message-ID: 'ch' here is a variable, standing for 'character.' It takes the place of 'x'. Your solution is almost correct, you just need to pick one name or the other for your variable. On Tue, Apr 10, 2018, 5:51 AM 清羽 <1625143974 at qq.com> wrote: > Hello everyone, I have a simple question here > Define a function isLower :: Char -> Bool which returns True if a > given character is a lower case letter. You can use the fact that > characters are ordered, and for all lower case letters *ch* we have ′*a*′ > ≤ *c**h* and *c**h* ≤ ′*z*′. Alternatively, you can use the fact that ['a' > ..'z'] evaluates to a list containing all lower case letters. > And May I ask what is the meaning of ch <= 'z' and 'a' <= ch? I > copied this as a condition and solved the problem.here is my work. > isLower :: Char -> Bool > isLower x = if x ch <= 'z' && 'a' <= ch then True else False. > Thanks in advance. > > _______________________________________________ > 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 mike_k_houghton at yahoo.co.uk Thu Apr 12 10:25:57 2018 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Thu, 12 Apr 2018 11:25:57 +0100 Subject: [Haskell-beginners] EitherT Message-ID: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> Hi, I’m trying to write EitherT from first principles and I’m stuck at the first hurdle - Functor. I’m looking for a hint rather than a complete answer :) This is what I have newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} instance Monad m => Functor (EitherT m a) where ---- fmap :: (a -> b) -> f a -> f b fmap f m = EitherT \$ do mv <- runEitherT m case mv of Left _ -> return mv Right rv -> return \$ Right (f rv) and here is the compilers view Phrase.hs:31:25: error: • Couldn't match type ‘b’ with ‘a1’ ‘b’ is a rigid type variable bound by the type signature for: fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b at Phrase.hs:27:5-8 ‘a1’ is a rigid type variable bound by the type signature for: fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b at Phrase.hs:27:5-8 Expected type: m (Either a a1) Actual type: m (Either a b) • In the expression: return \$ Right (f rv) In a case alternative: Right rv -> return \$ Right (f rv) In a stmt of a 'do' block: case mv of Left _ -> return mv Right rv -> return \$ Right (f rv) • Relevant bindings include rv :: a1 (bound at Phrase.hs:31:19) mv :: Either a a1 (bound at Phrase.hs:28:9) m :: EitherT m a a1 (bound at Phrase.hs:27:12) f :: a1 -> b (bound at Phrase.hs:27:10) fmap :: (a1 -> b) -> EitherT m a a1 -> EitherT m a b (bound at Phrase.hs:27:5) what I think I need to do is fmap over the right value after pulling if out of the monad m by doing mv <- runEitherT m these lines from the compiler are particularly confusing Expected type: m (Either a a1) Actual type: m (Either a b) as I believe f is f:: a1->b So just hints please and I expect I’ll have another duh moment. Thanks Mike From fa-ml at ariis.it Thu Apr 12 15:59:32 2018 From: fa-ml at ariis.it (Francesco Ariis) Date: Thu, 12 Apr 2018 17:59:32 +0200 Subject: [Haskell-beginners] EitherT In-Reply-To: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> References: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> Message-ID: <20180412155932.r5fbeh5wn52k42sp@x60s.casa> On Thu, Apr 12, 2018 at 11:25:57AM +0100, mike h wrote: > This is what I have > > newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} > instance Monad m => Functor (EitherT m a) where > ---- fmap :: (a -> b) -> f a -> f b > fmap f m = EitherT \$ do > mv <- runEitherT m > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) Tricky error! The signature for this fmap is: fmap :: (b -> c) -> EitherT m a b -> EitherT m a c The offending line is: Left _ -> return mv You *think* you are returning `Either a c`, but are you really? Type HINTS for more :P -F From toad3k at gmail.com Thu Apr 12 16:27:30 2018 From: toad3k at gmail.com (David McBride) Date: Thu, 12 Apr 2018 12:27:30 -0400 Subject: [Haskell-beginners] EitherT In-Reply-To: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> References: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> Message-ID: You will feel like this was obvious in hindsight. fmap :: (a -> b) -> f a -> f b fmap (a -> b) -> EitherT m c a -> EitherT m c b Now follow the types in your code. m :: EitherT m c a mv :: Either c a return mv :: EitherT m c a -- <- you are back to the wrong type. How can you instead return EitherT m c b? On Thu, Apr 12, 2018 at 6:25 AM, mike h wrote: > Hi, > I’m trying to write EitherT from first principles and I’m stuck at the > first hurdle - Functor. I’m looking for a hint rather than a complete > answer :) > > > This is what I have > > newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} > instance Monad m => Functor (EitherT m a) where > ---- fmap :: (a -> b) -> f a -> f b > fmap f m = EitherT \$ do > mv <- runEitherT m > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) > > > > and here is the compilers view > Phrase.hs:31:25: error: > • Couldn't match type ‘b’ with ‘a1’ > ‘b’ is a rigid type variable bound by > the type signature for: > fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b > at Phrase.hs:27:5-8 > ‘a1’ is a rigid type variable bound by > the type signature for: > fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b > at Phrase.hs:27:5-8 > Expected type: m (Either a a1) > Actual type: m (Either a b) > • In the expression: return \$ Right (f rv) > In a case alternative: Right rv -> return \$ Right (f rv) > In a stmt of a 'do' block: > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) > • Relevant bindings include > rv :: a1 (bound at Phrase.hs:31:19) > mv :: Either a a1 (bound at Phrase.hs:28:9) > m :: EitherT m a a1 (bound at Phrase.hs:27:12) > f :: a1 -> b (bound at Phrase.hs:27:10) > fmap :: (a1 -> b) -> EitherT m a a1 -> EitherT m a b > (bound at Phrase.hs:27:5) > > > what I think I need to do is fmap over the right value after pulling if > out of the monad m by doing mv <- runEitherT m > > these lines from the compiler are particularly confusing > Expected type: m (Either a a1) > Actual type: m (Either a b) > > as I believe f is f:: a1->b > > So just hints please and I expect I’ll have another duh moment. > > Thanks > > Mike > > > > _______________________________________________ > 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 traqueofziche at gmail.com Thu Apr 12 16:40:53 2018 From: traqueofziche at gmail.com (=?UTF-8?B?6bKN5Yev5paH?=) Date: Thu, 12 Apr 2018 16:40:53 +0000 Subject: [Haskell-beginners] EitherT In-Reply-To: References: Message-ID: Hi, Try to unify the types returned by each of the case alternatives, starting from what the compiler has inferred so far. I’ve made this mistake many times lol. Best, toz On Thu, Apr 12, 2018 at 5:38 AM wrote: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. EitherT (mike h) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 12 Apr 2018 11:25:57 +0100 > From: mike h > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: [Haskell-beginners] EitherT > Message-ID: <57AFC9F5-3601-4D18-B860-41352D3F2204 at yahoo.co.uk> > Content-Type: text/plain; charset=utf-8 > > Hi, > I’m trying to write EitherT from first principles and I’m stuck at the > first hurdle - Functor. I’m looking for a hint rather than a complete > answer :) > > > This is what I have > > newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} > instance Monad m => Functor (EitherT m a) where > ---- fmap :: (a -> b) -> f a -> f b > fmap f m = EitherT \$ do > mv <- runEitherT m > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) > > > > and here is the compilers view > Phrase.hs:31:25: error: > • Couldn't match type ‘b’ with ‘a1’ > ‘b’ is a rigid type variable bound by > the type signature for: > fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b > at Phrase.hs:27:5-8 > ‘a1’ is a rigid type variable bound by > the type signature for: > fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b > at Phrase.hs:27:5-8 > Expected type: m (Either a a1) > Actual type: m (Either a b) > • In the expression: return \$ Right (f rv) > In a case alternative: Right rv -> return \$ Right (f rv) > In a stmt of a 'do' block: > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) > • Relevant bindings include > rv :: a1 (bound at Phrase.hs:31:19) > mv :: Either a a1 (bound at Phrase.hs:28:9) > m :: EitherT m a a1 (bound at Phrase.hs:27:12) > f :: a1 -> b (bound at Phrase.hs:27:10) > fmap :: (a1 -> b) -> EitherT m a a1 -> EitherT m a b > (bound at Phrase.hs:27:5) > > > what I think I need to do is fmap over the right value after pulling if > out of the monad m by doing mv <- runEitherT m > > these lines from the compiler are particularly confusing > Expected type: m (Either a a1) > Actual type: m (Either a b) > > as I believe f is f:: a1->b > > So just hints please and I expect I’ll have another duh moment. > > Thanks > > Mike > > > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 118, Issue 6 > ***************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike_k_houghton at yahoo.co.uk Thu Apr 12 20:32:05 2018 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Thu, 12 Apr 2018 21:32:05 +0100 Subject: [Haskell-beginners] EitherT In-Reply-To: References: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> Message-ID: <7EDD529B-500B-4138-A4FD-FBDD14D5D474@yahoo.co.uk> instance Monad m => Functor (EitherT m a) where fmap f m = EitherT \$ do mv <- runEitherT m case mv of Left lv -> return \$ Left lv Right rv -> return \$ Right (f rv) Thanks all :) I think its correct. The compiler does! Mike > On 12 Apr 2018, at 17:27, David McBride wrote: > > You will feel like this was obvious in hindsight. > > fmap :: (a -> b) -> f a -> f b > fmap (a -> b) -> EitherT m c a -> EitherT m c b > > Now follow the types in your code. > m :: EitherT m c a > mv :: Either c a > return mv :: EitherT m c a -- <- you are back to the wrong type. > > How can you instead return EitherT m c b? > > > On Thu, Apr 12, 2018 at 6:25 AM, mike h > wrote: > Hi, > I’m trying to write EitherT from first principles and I’m stuck at the first hurdle - Functor. I’m looking for a hint rather than a complete answer :) > > > This is what I have > > newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} > instance Monad m => Functor (EitherT m a) where > ---- fmap :: (a -> b) -> f a -> f b > fmap f m = EitherT \$ do > mv <- runEitherT m > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) > > > > and here is the compilers view > Phrase.hs:31:25: error: > • Couldn't match type ‘b’ with ‘a1’ > ‘b’ is a rigid type variable bound by > the type signature for: > fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b > at Phrase.hs:27:5-8 > ‘a1’ is a rigid type variable bound by > the type signature for: > fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b > at Phrase.hs:27:5-8 > Expected type: m (Either a a1) > Actual type: m (Either a b) > • In the expression: return \$ Right (f rv) > In a case alternative: Right rv -> return \$ Right (f rv) > In a stmt of a 'do' block: > case mv of > Left _ -> return mv > Right rv -> return \$ Right (f rv) > • Relevant bindings include > rv :: a1 (bound at Phrase.hs:31:19) > mv :: Either a a1 (bound at Phrase.hs:28:9) > m :: EitherT m a a1 (bound at Phrase.hs:27:12) > f :: a1 -> b (bound at Phrase.hs:27:10) > fmap :: (a1 -> b) -> EitherT m a a1 -> EitherT m a b > (bound at Phrase.hs:27:5) > > > what I think I need to do is fmap over the right value after pulling if out of the monad m by doing mv <- runEitherT m > > these lines from the compiler are particularly confusing > Expected type: m (Either a a1) > Actual type: m (Either a b) > > as I believe f is f:: a1->b > > So just hints please and I expect I’ll have another duh moment. > > Thanks > > Mike > > > > _______________________________________________ > 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike_k_houghton at yahoo.co.uk Fri Apr 13 08:35:01 2018 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Fri, 13 Apr 2018 09:35:01 +0100 Subject: [Haskell-beginners] EitherT In-Reply-To: <7EDD529B-500B-4138-A4FD-FBDD14D5D474@yahoo.co.uk> References: <57AFC9F5-3601-4D18-B860-41352D3F2204@yahoo.co.uk> <7EDD529B-500B-4138-A4FD-FBDD14D5D474@yahoo.co.uk> Message-ID: <0709C844-FD6E-4130-B959-59EA459F798F@yahoo.co.uk> Even ‘better’ instance Monad m => Functor (EitherT m a) where fmap f m = EitherT \$ runEitherT m >>= \mv -> return \$ fmap f mv :) > On 12 Apr 2018, at 21:32, mike h wrote: > > instance Monad m => Functor (EitherT m a) where > fmap f m = EitherT \$ do > mv <- runEitherT m > case mv of > Left lv -> return \$ Left lv > Right rv -> return \$ Right (f rv) > > > Thanks all :) I think its correct. The compiler does! > Mike > > >> On 12 Apr 2018, at 17:27, David McBride > wrote: >> >> You will feel like this was obvious in hindsight. >> >> fmap :: (a -> b) -> f a -> f b >> fmap (a -> b) -> EitherT m c a -> EitherT m c b >> >> Now follow the types in your code. >> m :: EitherT m c a >> mv :: Either c a >> return mv :: EitherT m c a -- <- you are back to the wrong type. >> >> How can you instead return EitherT m c b? >> >> >> On Thu, Apr 12, 2018 at 6:25 AM, mike h > wrote: >> Hi, >> I’m trying to write EitherT from first principles and I’m stuck at the first hurdle - Functor. I’m looking for a hint rather than a complete answer :) >> >> >> This is what I have >> >> newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} >> instance Monad m => Functor (EitherT m a) where >> ---- fmap :: (a -> b) -> f a -> f b >> fmap f m = EitherT \$ do >> mv <- runEitherT m >> case mv of >> Left _ -> return mv >> Right rv -> return \$ Right (f rv) >> >> >> >> and here is the compilers view >> Phrase.hs:31:25: error: >> • Couldn't match type ‘b’ with ‘a1’ >> ‘b’ is a rigid type variable bound by >> the type signature for: >> fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b >> at Phrase.hs:27:5-8 >> ‘a1’ is a rigid type variable bound by >> the type signature for: >> fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b >> at Phrase.hs:27:5-8 >> Expected type: m (Either a a1) >> Actual type: m (Either a b) >> • In the expression: return \$ Right (f rv) >> In a case alternative: Right rv -> return \$ Right (f rv) >> In a stmt of a 'do' block: >> case mv of >> Left _ -> return mv >> Right rv -> return \$ Right (f rv) >> • Relevant bindings include >> rv :: a1 (bound at Phrase.hs:31:19) >> mv :: Either a a1 (bound at Phrase.hs:28:9) >> m :: EitherT m a a1 (bound at Phrase.hs:27:12) >> f :: a1 -> b (bound at Phrase.hs:27:10) >> fmap :: (a1 -> b) -> EitherT m a a1 -> EitherT m a b >> (bound at Phrase.hs:27:5) >> >> >> what I think I need to do is fmap over the right value after pulling if out of the monad m by doing mv <- runEitherT m >> >> these lines from the compiler are particularly confusing >> Expected type: m (Either a a1) >> Actual type: m (Either a b) >> >> as I believe f is f:: a1->b >> >> So just hints please and I expect I’ll have another duh moment. >> >> Thanks >> >> Mike >> >> >> >> _______________________________________________ >> 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc at lamarciana.com Fri Apr 13 09:42:20 2018 From: marc at lamarciana.com (=?ISO-8859-15?Q?Marc_Busqu=E9?=) Date: Fri, 13 Apr 2018 11:42:20 +0200 (CEST) Subject: [Haskell-beginners] Lazyness and forM_ Message-ID: Hi there! I'm using [selda](https://github.com/valderman/selda) package to work with databases. I'm trying to write a function to generate several tables at once. In the following examples, `categories` and `expenses` are two selda `Table`. The other functions in use and their respective imports should be self-evident: If I do: ``` migrate :: IO () migrate = do dir <- dBDir createDirectoryIfMissing True dir forM_ (categories, expenses) \$ withDB . createTable ``` tables are not actually created. However, if I do: ``` migrate :: IO () migrate = do dir <- dBDir createDirectoryIfMissing True dir withDB . createTable \$ categories withDB . createTable \$ expenses ``` Both tables are actually created. So it seems like the thugs created with `formM_` are actually never executed. Is it so? I'm new with Haskell and it seems strange for me. If it is so, doesn't make it useless `forM_` for IO? Furthermore, I can't reproduce it in ghci. Doing this: ``` ["a", "b"] `forM_` print ``` Actually prints both `"a"` and `"b"`. Thanks for any enlightment. Marc Busqué http://waiting-for-dev.github.io/about/ From ky3 at atamo.com Fri Apr 13 10:53:28 2018 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Fri, 13 Apr 2018 17:53:28 +0700 Subject: [Haskell-beginners] Lazyness and forM_ In-Reply-To: References: Message-ID: Hi Marc, and welcome to the haskell beginners list. ``` > migrate :: IO () > migrate = do > dir <- dBDir > createDirectoryIfMissing True dir > forM_ (categories, expenses) > \$ withDB . createTable > ``` > > tables are not actually created. > > Well, forM_ (categories, expenses) \$ withDB . createTable is equivalent to withDB . createTable \$ expenses. So exactly one table is created. > ``` > ["a", "b"] `forM_` print > ``` > > Actually prints both `"a"` and `"b"`. > > Here the code uses square brackets--and so we have honest-to-goodness lists--whereas the previous used parentheses. See what happens with: ("a","b") `forM_` print. Marc: Feel free to write to the haskell-cafe mailing list for questions such as this. Fortuitously in this case, it turns out that your query needed knowledge only about the forM* combinators and--ever since their ilk was generalized--the known instances declared for the Traversable constraint. As you explore the domain-specific package "selda" further, you will find more people acquainted with the package over at the cafe than here in beginners. Suffice to say, everyone here in this list is also in cafe, which is also open to beginners questions. p.s. Veterans would recognize this as ye olde controversy on the Foldable instance for pairs introduced in GHC 7.10.x. The controversy still simmers apparently because there isn't an instance for triples and higher tuples in the latest and greatest GHC 8.4.1. We are at the mercy of this potentially toe-stubbing absence of uniformity. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc at lamarciana.com Fri Apr 13 13:32:27 2018 From: marc at lamarciana.com (=?ISO-8859-15?Q?Marc_Busqu=E9?=) Date: Fri, 13 Apr 2018 15:32:27 +0200 (CEST) Subject: [Haskell-beginners] Lazyness and forM_ In-Reply-To: References: Message-ID: On Fri, 13 Apr 2018, Kim-Ee Yeoh wrote: > Well,  > > forM_ (categories, expenses) \$ withDB . createTable > > is equivalent to  > > withDB . createTable \$ expenses. > > So exactly one table is created. Oops, you are right. > ``` > ["a", "b"] `forM_` print > ``` > > Actually prints both `"a"` and `"b"`. > > > Here the code uses square brackets--and so we have honest-to-goodness lists--whereas the previous used parentheses. > > See what happens with: ("a","b") `forM_` print. Now I feel embarrassed I overlooked that :) I used a tuple instead of a list because in selda `Table` are type-safe. So `categories` and `expenses` are different types and they can't go in the same list. So I guess, once the `forM_` mistery is gone, I can boil down my problem to something very different. As it has nothing to do with the current subject, I'll submit another question. > Marc: Feel free to write to the haskell-cafe mailing list for questions such as this. Fortuitously in this case, it turns out that your query > needed knowledge only about the forM* combinators and--ever since their ilk was generalized--the known instances declared for the Traversable > constraint. As you explore the domain-specific package "selda" further, you will find more people acquainted with the package over at the cafe > than here in beginners. Suffice to say, everyone here in this list is also in cafe, which is also open to beginners questions. And following your recommendation I'll submit that question in the haskell-cafe mailing list :) Thank you very much for your help :) Marc Busqué http://waiting-for-dev.github.io/about/ From quentin.liu.0415 at gmail.com Sat Apr 14 20:28:55 2018 From: quentin.liu.0415 at gmail.com (Quentin Liu) Date: Sat, 14 Apr 2018 16:28:55 -0400 Subject: [Haskell-beginners] Efficient Binary Multiplication Message-ID: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> Hi all, Suppose I want to multiply two binary numbers whose representation uses lists (e.g. 14 is represented as [1, 1, 1, 0]). Is there any efficient way to do binary multiplication? The way I could come up with involves a lot of intermediate lists that will be discarded eventually and is extremely inefficient. I know one fast algorithm that uses Array but would it be possible to do the multiplication with only lists? Regards, Qingbo Liu -------------- next part -------------- An HTML attachment was scrubbed... URL: From trupill at gmail.com Mon Apr 16 13:26:20 2018 From: trupill at gmail.com (Alejandro Serrano Mena) Date: Mon, 16 Apr 2018 15:26:20 +0200 Subject: [Haskell-beginners] Call for Participation: Summer School on Advanced Functional Programming Message-ID: # Call for Participation SUMMER SCHOOL ON ADVANCED FUNCTIONAL PROGRAMMING Utrecht, the Netherlands, 27-31 August 2018 http://www.afp.school ## ABOUT The Advanced Functional Programming summer school has been running for more than ten years. We aim to educate aspiring Haskell programmers beyond the basic material covered by many textbooks. The lectures will cover several more advanced topics regarding the theory and practice of Haskell programming, including topics such as: * lambda calculus; * monads and monad transformers; * lazy evaluation; * generalized algebraic data types; * type families and type-level programming; * concurrency and parallelism. The summer school consists of a mix of lectures, labs, and a busy social program. ## LECTURERS Utrecht staff: * Johan Jeuring * Alejandro Serrano Mena * Doaitse Swierstra * Wouter Swierstra Guest lectures: * Manuel Chakravarty * Koen Claessen * Gabriele Keller ## PREREQUISITES We expect students to have a basic familiarity with Haskell already. You should be able to write recursive functions over algebraic data types, such as lists and trees. There is a great deal of material readily available that covers this material. If you’ve already started learning Haskell and are looking to take your functional programming skills to the next level, this is the course for you. ## DATES Registration deadline: 1 August, 2017 School: 27-31 August ## COSTS €1700 - Housing and registration €1500 - Registration only We offer a €1000 discount for students and staff members affiliated with a university. ## SCHOLARSHIPS If you’re struggling to finance your trip to Utrecht, please let us know. We have a limited number of scholarships or discounts available for students that would not be able to attend otherwise, especially for women and under represented minorities. ## FURTHER INFORMATION Further information, including instructions on how to register, is available on our website: http://www.afp.school -------------- next part -------------- An HTML attachment was scrubbed... URL: From leiva.steven at gmail.com Tue Apr 17 00:48:40 2018 From: leiva.steven at gmail.com (Steven Leiva) Date: Mon, 16 Apr 2018 20:48:40 -0400 Subject: [Haskell-beginners] Efficient Binary Multiplication In-Reply-To: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> References: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> Message-ID: Interesting question. So there is a concept in computer science called *fusion*. My understanding (I don't have a CS degree) is that when data is subject to fusion, we don't create the intermediary lists as you are talking about. This is why, in Haskell, we have the *ByteString* and *Text* datatypes. Unlike *String*, they (ByteString / Text) are subject to fusion. *Text* is useful when we are dealing with unicode data, while *ByteString* is the correct tool when we are dealing with binary data. All of this is to say that you should look at the *ByteString* data type. (Take a look at this description, for example: https://hackage.haskell.org/package/bytestring-0.10.8.2/docs/Data-ByteString.html#t:ByteString). The only potential problem I see is that *ByteString* internally uses *Word8*, and since *Word8* is *unsigned*, negatives are problematic. (Now that I think about it, how the heck do you represent negative numbers in binary?). I hope this is useful. On Sat, Apr 14, 2018 at 4:28 PM, Quentin Liu wrote: > Hi all, > > Suppose I want to multiply two binary numbers whose representation uses > lists (e.g. 14 is represented as [1, 1, 1, 0]). Is there any efficient way > to do binary multiplication? The way I could come up with involves a lot of > intermediate lists that will be discarded eventually and is extremely > inefficient. I know one fast algorithm that uses Array but would it be > possible to do the multiplication with only lists? > > Regards, > Qingbo Liu > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -- Steven Leiva 305.528.6038 leiva.steven at gmail.com http://www.linkedin.com/in/stevenleiva -------------- next part -------------- An HTML attachment was scrubbed... URL: From frederic-emmanuel.picca at synchrotron-soleil.fr Wed Apr 18 14:39:58 2018 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Wed, 18 Apr 2018 14:39:58 +0000 Subject: [Haskell-beginners] phantom type In-Reply-To: References: <20180331084011.5ixszt3pboaovr4i@x60s.casa>, Message-ID: > Directly relevant blog: https://www.benjamin.pizza/posts/2017-12-15-functor-functors.html thanks a lot very interesting post :) From frederic-emmanuel.picca at synchrotron-soleil.fr Wed Apr 18 14:41:39 2018 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Wed, 18 Apr 2018 14:41:39 +0000 Subject: [Haskell-beginners] phantom type In-Reply-To: <20180331084011.5ixszt3pboaovr4i@x60s.casa> References: , <20180331084011.5ixszt3pboaovr4i@x60s.casa> Message-ID: Hello > I am in a rush so I cant write a minimal example, but wouldn't > a typeclass + make Collect and Cara instances of that typeclass do? Yes I can do this in my case and it is not thaht difficult :). thanks for your help. cheers Frederic From stephen.tetley at gmail.com Wed Apr 18 17:33:12 2018 From: stephen.tetley at gmail.com (Stephen Tetley) Date: Wed, 18 Apr 2018 18:33:12 +0100 Subject: [Haskell-beginners] Efficient Binary Multiplication In-Reply-To: References: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> Message-ID: List fusion has existed for a long time (based on a change to the internal representation of lists to make them streams). IIRC the actual implementation was not considered a satisfactory replacement for GHCs existing list implementation as performance improvements were not uniform, the stream representation is good for some things worse for others. I don't know whether this situation has now changed. Byte String and Text exist because implementing strings or byte sequences as a cons list of Char or Byte is very inefficient both for storage / data locality and for many operations. Fusion for Byte String and Text is a bonus rather than a motivation. [*] Stream Fusion: From Lists to Streams to Nothing at All Duncan Coutts , Roman Leshchinskiy , Don Stewart http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.104.7401 On 17 April 2018 at 01:48, Steven Leiva wrote: > Interesting question. > > So there is a concept in computer science called fusion. > > My understanding (I don't have a CS degree) is that when data is subject to > fusion, we don't create the intermediary lists as you are talking about. > This is why, in Haskell, we have the ByteString and Text datatypes. Unlike > String, they (ByteString / Text) are subject to fusion. Text is useful when > we are dealing with unicode data, while ByteString is the correct tool when > we are dealing with binary data. > > All of this is to say that you should look at the ByteString data type. > (Take a look at this description, for example: > https://hackage.haskell.org/package/bytestring-0.10.8.2/docs/Data-ByteString.html#t:ByteString). > > The only potential problem I see is that ByteString internally uses Word8, > and since Word8 is unsigned, negatives are problematic. (Now that I think > about it, how the heck do you represent negative numbers in binary?). > > I hope this is useful. > > On Sat, Apr 14, 2018 at 4:28 PM, Quentin Liu > wrote: >> >> Hi all, >> >> Suppose I want to multiply two binary numbers whose representation uses >> lists (e.g. 14 is represented as [1, 1, 1, 0]). Is there any efficient way >> to do binary multiplication? The way I could come up with involves a lot of >> intermediate lists that will be discarded eventually and is extremely >> inefficient. I know one fast algorithm that uses Array but would it be >> possible to do the multiplication with only lists? >> >> Regards, >> Qingbo Liu >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > > > > -- > Steven Leiva > 305.528.6038 > leiva.steven at gmail.com > http://www.linkedin.com/in/stevenleiva > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > From kc1956 at gmail.com Wed Apr 18 19:07:06 2018 From: kc1956 at gmail.com (KC) Date: Wed, 18 Apr 2018 19:07:06 +0000 Subject: [Haskell-beginners] Efficient Binary Multiplication In-Reply-To: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> References: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> Message-ID: Hi Must you use only lists? Must each node only contain one digit? -- Sent from an expensive device which will be obsolete in a few months Casey On Sat, Apr 14, 2018, 1:30 PM Quentin Liu, wrote: > Hi all, > > Suppose I want to multiply two binary numbers whose representation uses > lists (e.g. 14 is represented as [1, 1, 1, 0]). Is there any efficient way > to do binary multiplication? The way I could come up with involves a lot of > intermediate lists that will be discarded eventually and is extremely > inefficient. I know one fast algorithm that uses Array but would it be > possible to do the multiplication with only lists? > > Regards, > Qingbo Liu > _______________________________________________ > 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 leiva.steven at gmail.com Sat Apr 28 16:08:22 2018 From: leiva.steven at gmail.com (Steven Leiva) Date: Sat, 28 Apr 2018 11:08:22 -0500 Subject: [Haskell-beginners] Efficient Binary Multiplication In-Reply-To: References: <2cd382a0-b5be-4eed-a0c0-e25d2045031d@Spark> Message-ID: Thank you Stephen for the clarification! On Wed, Apr 18, 2018 at 12:33 PM, Stephen Tetley wrote: > List fusion has existed for a long time (based on a change to the > internal representation of lists to make them streams). > > IIRC the actual implementation was not considered a satisfactory > replacement for GHCs existing list implementation as performance > improvements were not uniform, the stream representation is good for > some things worse for others. I don't know whether this situation has > now changed. > > Byte String and Text exist because implementing strings or byte > sequences as a cons list of Char or Byte is very inefficient both for > storage / data locality and for many operations. Fusion for Byte > String and Text is a bonus rather than a motivation. > > > [*] Stream Fusion: From Lists to Streams to Nothing at All > Duncan Coutts , Roman Leshchinskiy , Don Stewart > http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.104.7401 > > On 17 April 2018 at 01:48, Steven Leiva wrote: > > Interesting question. > > > > So there is a concept in computer science called fusion. > > > > My understanding (I don't have a CS degree) is that when data is subject > to > > fusion, we don't create the intermediary lists as you are talking about. > > This is why, in Haskell, we have the ByteString and Text datatypes. > Unlike > > String, they (ByteString / Text) are subject to fusion. Text is useful > when > > we are dealing with unicode data, while ByteString is the correct tool > when > > we are dealing with binary data. > > > > All of this is to say that you should look at the ByteString data type. > > (Take a look at this description, for example: > > https://hackage.haskell.org/package/bytestring-0.10.8.2/ > docs/Data-ByteString.html#t:ByteString). > > > > The only potential problem I see is that ByteString internally uses > Word8, > > and since Word8 is unsigned, negatives are problematic. (Now that I think > > about it, how the heck do you represent negative numbers in binary?). > > > > I hope this is useful. > > > > On Sat, Apr 14, 2018 at 4:28 PM, Quentin Liu > > > wrote: > >> > >> Hi all, > >> > >> Suppose I want to multiply two binary numbers whose representation uses > >> lists (e.g. 14 is represented as [1, 1, 1, 0]). Is there any efficient > way > >> to do binary multiplication? The way I could come up with involves a > lot of > >> intermediate lists that will be discarded eventually and is extremely > >> inefficient. I know one fast algorithm that uses Array but would it be > >> possible to do the multiplication with only lists? > >> > >> Regards, > >> Qingbo Liu > >> > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > >> > > > > > > > > -- > > Steven Leiva > > 305.528.6038 > > leiva.steven at gmail.com > > http://www.linkedin.com/in/stevenleiva > > > > _______________________________________________ > > 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 > -- Steven Leiva 305.528.6038 leiva.steven at gmail.com http://www.linkedin.com/in/stevenleiva -------------- next part -------------- An HTML attachment was scrubbed... URL: