From mike_k_houghton at yahoo.co.uk Thu Nov 25 17:10:44 2021 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Thu, 25 Nov 2021 17:10:44 +0000 Subject: [Haskell-beginners] Monad question In-Reply-To: <20210912105738.egomltlkquv5le5l@gmail.com> References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: Hi, This isn’t homework! I’ve been staring at this for several hours - and that usually works. I also tried typed holes to no avail. I thinks it is quite simple really but I’ve gone past seeing it! I have data Expr a = Var a | Add (Expr a) (Expr a) and would like to write convert :: Expr (Maybe a) -> Maybe (Expr a) which returns Nothing if there is an occurrence of Nothing inside the input expression e, otherwise it returns Just e', where e' is a new expression where the internal values of type a are not wrapped in Just. You should use the functionality of the Maybe monad to implement the convert function. Thanks Mike From yehoshuapw at gmail.com Thu Nov 25 17:46:05 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Thu, 25 Nov 2021 19:46:05 +0200 Subject: [Haskell-beginners] Monad question In-Reply-To: References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: I can't actually cheok right now, but ``` convert (Var mv) = do v <- mv return $ Var v ``` is the "do notation" which should work. (and similarly for the other expression) On Thu, Nov 25, 2021, 19:13 mike h wrote: > Hi, > This isn’t homework! I’ve been staring at this for several hours - and > that usually works. > I also tried typed holes to no avail. I thinks it is quite simple really > but I’ve gone past seeing it! > > I have > data Expr a = Var a | Add (Expr a) (Expr a) > > and would like to write > > convert :: Expr (Maybe a) -> Maybe (Expr a) > > which returns Nothing if there is an occurrence of Nothing inside the > input expression e, otherwise it returns Just e', where e' > is a new expression where the internal values of type a are not wrapped in > Just. > You should use the functionality of the Maybe monad to implement > the convert function. > > > 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 yehoshuapw at gmail.com Thu Nov 25 17:53:41 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Thu, 25 Nov 2021 19:53:41 +0200 Subject: [Haskell-beginners] Monad question In-Reply-To: References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: convert (Var mx) = mx >>= (return .Var x) Should also be the non-do-notation. (again, typing from a phone, and not tested) On Thu, Nov 25, 2021, 19:46 יהושע ולך wrote: > I can't actually cheok right now, > but > ``` > convert (Var mv) = do > v <- mv > return $ Var v > ``` > > is the "do notation" which should work. > (and similarly for the other expression) > > On Thu, Nov 25, 2021, 19:13 mike h wrote: > >> Hi, >> This isn’t homework! I’ve been staring at this for several hours - and >> that usually works. >> I also tried typed holes to no avail. I thinks it is quite simple >> really but I’ve gone past seeing it! >> >> I have >> data Expr a = Var a | Add (Expr a) (Expr a) >> >> and would like to write >> >> convert :: Expr (Maybe a) -> Maybe (Expr a) >> >> which returns Nothing if there is an occurrence of Nothing inside the >> input expression e, otherwise it returns Just e', where e' >> is a new expression where the internal values of type a are not wrapped >> in Just. >> You should use the functionality of the Maybe monad to implement >> the convert function. >> >> >> 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 yehoshuapw at gmail.com Thu Nov 25 17:54:21 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Thu, 25 Nov 2021 19:54:21 +0200 Subject: [Haskell-beginners] Monad question In-Reply-To: References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: convert (Var mx) = mx >>= (return .Var) Typo. On Thu, Nov 25, 2021, 19:53 יהושע ולך wrote: > convert (Var mx) = mx >>= (return .Var x) > > Should also be the non-do-notation. > (again, typing from a phone, and not tested) > > On Thu, Nov 25, 2021, 19:46 יהושע ולך wrote: > >> I can't actually cheok right now, >> but >> ``` >> convert (Var mv) = do >> v <- mv >> return $ Var v >> ``` >> >> is the "do notation" which should work. >> (and similarly for the other expression) >> >> On Thu, Nov 25, 2021, 19:13 mike h wrote: >> >>> Hi, >>> This isn’t homework! I’ve been staring at this for several hours - and >>> that usually works. >>> I also tried typed holes to no avail. I thinks it is quite simple >>> really but I’ve gone past seeing it! >>> >>> I have >>> data Expr a = Var a | Add (Expr a) (Expr a) >>> >>> and would like to write >>> >>> convert :: Expr (Maybe a) -> Maybe (Expr a) >>> >>> which returns Nothing if there is an occurrence of Nothing inside the >>> input expression e, otherwise it returns Just e', where e' >>> is a new expression where the internal values of type a are not wrapped >>> in Just. >>> You should use the functionality of the Maybe monad to implement >>> the convert function. >>> >>> >>> 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 imantc at gmail.com Thu Nov 25 18:36:02 2021 From: imantc at gmail.com (Imants Cekusins) Date: Thu, 25 Nov 2021 20:36:02 +0200 Subject: [Haskell-beginners] Monad question In-Reply-To: References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: Or you could derive Traversable for Expr Then sequenceA can be used: https://hoogle.haskell.org/?hoogle=f%20(m%20a)%20-%3E%20m%20(f%20a) On Thu 25 Nov 2021, 19:54 יהושע ולך, wrote: > convert (Var mx) = mx >>= (return .Var x) > > Should also be the non-do-notation. > (again, typing from a phone, and not tested) > > On Thu, Nov 25, 2021, 19:46 יהושע ולך wrote: > >> I can't actually cheok right now, >> but >> ``` >> convert (Var mv) = do >> v <- mv >> return $ Var v >> ``` >> >> is the "do notation" which should work. >> (and similarly for the other expression) >> >> On Thu, Nov 25, 2021, 19:13 mike h wrote: >> >>> Hi, >>> This isn’t homework! I’ve been staring at this for several hours - and >>> that usually works. >>> I also tried typed holes to no avail. I thinks it is quite simple >>> really but I’ve gone past seeing it! >>> >>> I have >>> data Expr a = Var a | Add (Expr a) (Expr a) >>> >>> and would like to write >>> >>> convert :: Expr (Maybe a) -> Maybe (Expr a) >>> >>> which returns Nothing if there is an occurrence of Nothing inside the >>> input expression e, otherwise it returns Just e', where e' >>> is a new expression where the internal values of type a are not wrapped >>> in Just. >>> You should use the functionality of the Maybe monad to implement >>> the convert function. >>> >>> >>> 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 ky3 at atamo.com Thu Nov 25 18:55:42 2021 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Fri, 26 Nov 2021 01:55:42 +0700 Subject: [Haskell-beginners] Monad question In-Reply-To: References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: Hi Mike, On Fri, Nov 26, 2021 at 12:12 AM mike h wrote: > > I have > data Expr a = Var a | Add (Expr a) (Expr a) > > and would like to write > > convert :: Expr (Maybe a) -> Maybe (Expr a) With the requisite setup, convert has a one word definition. But more on that later. Yehoshua has already given you a nudge in the right direction. Others will surely chime in if you get stuck grinding out something the compiler will type check. What we could discuss is higher level. Where would use this convert function? What utility could you obtain from it? > > which returns Nothing if there is an occurrence of Nothing inside the > input expression e, otherwise it returns Just e', where e' > is a new expression where the internal values of type a are not wrapped in > Just. > You should use the functionality of the Maybe monad to implement > the convert function. > > > Thanks > Mike > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: