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: map type explanation ([email protected])
----------------------------------------------------------------------
Message: 1
Date: Sun, 20 Dec 2020 19:43:24 +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] map type explanation
Message-ID:
<cXaXkpbIu_NtavVZrdIduhjTnoacQADcpQ42mJLHswOtVTaqUDcA6CYG0uVI1Uyx4DKukobwQCTar9MjHGLVvAMCCMTeIH711JKEzAvW6YQ=@coot.me>
Content-Type: text/plain; charset="utf-8"
Hi,
The `(a -> b)` in `map :: (a -> b) -> [a] -> [b]` is a type of function from a
type `a` to a type `b`. Haskell, by default, is using implicit
quantification, however using `ScopedTypeVariables` extension you could write
`map :: forall a b. (a -> b) -> [a] -> [b]` to make it explicit. This way you
see that both `a` and `b` are introduced by `forall`. In particular, you can
substitute any types, e.g. if you substitute `a` with `Char` and `b` with
`Int`, you'll get `map :: (Char -> Int) -> [Char] -> [Int]`. If you enable
`TypeApplication` extension you can do that in `ghci`: `:t map @Char @Int`
You can read the type of `map` as follows: given a function `f` from type `a`
to some type `b`, `map f :: [a] -> [b]`, i.e. `map f` is a function from list
of `a` to list of `b`'s.
Being able to write functions like `map`, is called parametric polymorphism.
Using such polymorphism you guarantee that the implentation of `map` cannot do
any thing with `a`'s and `b`'s, as there's no knowledge about what they are,
beside the recepe how to transform `a`'s into `b` given by `f`. This limits
what `map f` can do to a list of `a`'s.
Best regards,
Marcin Szamotulski
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Friday, December 18th, 2020 at 19:30, Lawrence Bottorff <[email protected]>
wrote:
> I'm looking at this
>
> ghci> :type map
>
> map :: (a -> b) -> [a] -> [b]
>
> and wondering what the (a -> b) part is about. map takes a function and
> applies it to an incoming list. Good. Understood. I'm guessing that the whole
> Haskell type declaration idea is based on currying, and I do understand how
> the (a -> b) part "takes" an incoming list, [a] and produces the [b] output.
> Also, I don't understand a and b very well either. Typically, a is just a
> generic variable, then b is another generic variable not necessarily the same
> as a. But how are they being used in this type declaration?
>
> LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20201220/887c6b4a/attachment-0001.html>
-------------- 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/20201220/887c6b4a/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 149, Issue 15
******************************************