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: Maybe and Just (Frerich Raabe)
2. Re: Type Parameters and Type Variables
(Sumit Sahrawat, Maths & Computing, IIT (BHU))
3. Re: Empty list (Joel Neely)
----------------------------------------------------------------------
Message: 1
Date: Thu, 26 Mar 2015 11:34:58 +0100
From: Frerich Raabe <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe and Just
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed
Hi,
On 2015-03-26 11:06, Shishir Srivastava wrote:
> After reading and re-reading the haskell tutorials I don't happen to see a
> very convincing or appealing reason for having these data types.
First of all, only one of them is a type (or a 'type constructor'): "Maybe".
"Just" on the other hand is a "data constructor", i.e. "Just 'x'" will yield
a value of type "Maybe Char".
> Can anyone please explain where Maybe and Just provide the sort of
> functionality that cannot be achieved in other languages which don't have
> these kind.
They don't provide anything which other languages cannot achieve. 'Maybe'
values merely indicate that you *maybe* get a value. This is useful for
functions which might not be able to yield a meaningful result. Consider a
'safeHead' function which works like 'head', returning the first element of a
given list. What should it do for empty lists?
Something like 'Maybe' is useful because it
1. Makes potential errors clearly visible in the type system: If a function
returns a 'Maybe Char', you know that it possible won't yield a Char (but
rather 'Nothing'). Raising an exception is totally opaque to the caller.
2. Clearly separates the values which might represent failures from those
which don't. Contrast this with e.g. Java, where every object of every type
can be null. If every value is potentially 'null', you either have to check
using if() everywhere or use assertions. With 'Maybe', the null'ness can be
asserted by the compiler.
3. Relieves other types from having an explicit 'Invalid' value. Consider:
data Color = Red | Green | Blue | Invalid
-- Parses a color string; yields 'Invalid' if the string is malformed
parseColor :: String -> Color
parseColor = undefind
With a setup like this, *every* code dealing with 'Color' values will have to
be able to handle 'Invalid' colors. It ripples through the system. OTOH,
something like
data Color = Red | Green | Blue
-- Parses a color string; yields 'Nothing' if the string is malformed
parseColor :: String -> Maybe Color
parseColor = undefined
Clearly separates the valid colors, such that only the caller of 'parseColor'
only has to check for 'Just x' values - and if it got one, it can pass the
'x' value to other code.
For what it's worth, other languages have similiar concepts. For instance,
C++ programmers can use 'boost::optional' (which, alas, didn't make it into
C++14).
--
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing
------------------------------
Message: 2
Date: Thu, 26 Mar 2015 16:18:52 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type Parameters and Type Variables
Message-ID:
<cajbew8mj-2pddjs_cbvu9ntxnf8gasegxkkddbwyl94sfgz...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
When type variables are passed to a Type Constructor, they become type
parameters.
Just like when variables are passed to functions, they become parameters.
Operationally, both are equivalent.
On 26 March 2015 at 15:51, Shishir Srivastava <[email protected]>
wrote:
> What's the difference between the two.. ?
>
> Shishir Srivastava
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150326/e164a6fa/attachment-0001.html>
------------------------------
Message: 3
Date: Thu, 26 Mar 2015 06:54:29 -0500
From: Joel Neely <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Empty list
Message-ID:
<caeezxaiebc3usduqi7ojja+xtvm5nftyu6pbqbokocyfwdc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Given the distinction between lists and sets, you might also consider
Prelude Data.Set Data.List> [] `isInfixOf` [1,2,3]
True
Prelude Data.Set Data.List> (fromList []) `isSubsetOf` (fromList [1,2,3])
True
Of course `isInfixOf` is in general more restrictive than `isSubsetOf`, in
the sense that the infix relationship depends on ordering, which the subset
relationship does not.
Hope that helps,
-jn-
On Wed, Mar 25, 2015 at 8:55 AM, Shishir Srivastava <
[email protected]> wrote:
> Hi,
>
> Can someone please explain why this results in error -
>
> [] `elem` [1,2,3]
>
> Shouldn't the empty set by definition be the element of all sets including
> a non-empty set ?
>
> I am assuming 'Lists' are different from 'Sets' in Haskell, if yes, is
> there a separate module for dealing/working with sets ?
>
> Thanks,
> Shishir Srivastava
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> 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:
<http://mail.haskell.org/pipermail/beginners/attachments/20150326/2f53f259/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 81, Issue 62
*****************************************