Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Monad question ([email protected])
----------------------------------------------------------------------
Message: 1
Date: Tue, 28 Dec 2021 18:34:53 +0000
From: [email protected]
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Monad question
Message-ID:
<WOCL_AmKin5SrdUyVgKxdYv2CK0i5sSGYvK4_Nb5h8SG45jL08-m68ON45h3REW_HsvK9F_ZuxCl9wvDrEL3rsSIA2L6RynauSbPUZeqNuo=@coot.me>
Content-Type: text/plain; charset="utf-8"
Hi Mike,
An applicative instance of `Maybe` is enough to write the convert function
you're looking for. Applicative is very useful when there are no dependencies
between different branches, like when converting `Add`. But anyway, converting
between applicative and monadic operations is a good exercise, which I'll leave
for you. It's always possible to convert from applicative to monadic form, but
the more useful conversion is from monadic to applicative (which is not always
possible):
```
convert :: Expr (Maybe a) -> Maybe (Expr a)
convert (Var a) = Var <$> a
convert (Add a b) = Add <$> convert a
<*> convert b
```
Best regards,
Marcin
Sent with ProtonMail Secure Email.
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Thursday, November 25th, 2021 at 18:10, mike h <[email protected]>
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
>
> [email protected]
>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 509 bytes
Desc: OpenPGP digital signature
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20211228/703d7f09/attachment-0001.sig>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 161, Issue 4
*****************************************