[Haskell-beginners] Type declarations
Patrik Iselind
patrik.mrx at gmail.com
Sun Nov 26 18:39:49 UTC 2017
Den 2017-11-26 kl. 19:20, skrev Francesco Ariis:
> On Sun, Nov 26, 2017 at 07:02:36PM +0100, mrx wrote:
>> What do you mean by parameter of Point a?
> Let's start with a type you probably know, Maybe:
>
> data Maybe a = Just a
> | Nothing
Sorry, i've not used Maybe yet. Chapter 3 that i'm trying to get through
now mention a Maybe ever so briefly. I've heard of a Maybe monad, is
that it?
> The `a` in `Maybe a` is a type parameter, as the whole thing can
> be a `Maybe Int`, `Maybe String`, etc.
>
> Now let's check what `Point a` does
>
> data Point a = Coordinate Double Double
>
> Uhhh, suspicious, there is an `a` on the left side, but it's pretty
> useless, because there is no `a` on the right side. This is
> most likely not correct.
Ah, i see. Thanks for the clarification.
> Better to write
>
> -- this, concrete
> data Point = Coordinate Double Double
> -- or parametric
> data Point a = Coordinate a a
Does this mean that i can write `delta :: Point Double t -> Point Double
t -> Direction d` as a type declaration. Then i would require
`Coordinate Double Double` as in parameters. Correct?
>> Do you think it would be a mistake to simply skip writing the type
>> declarations completely until I've reached type classes?
> As now you know how write signatures like `something :: Int -> [String]`,
> when you meet `Something a => etc.` tread with care until you reach
> the chapter on typeclasses.
When i write type declarations, then i should stick with the non-`(Foo
f) =>` version until i've reached that chapter on type classes. It's
stilla few chapters until i reach it, i'm on chapter 3 and type classes
are chapter 6.
// Patrik
More information about the Beginners
mailing list