No ghci return for homemade types

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

No ghci return for homemade types

Lawrence Bottorff-2
I've tried this

data MyMaybe a = Just a | Nothing

maybeHead :: [t] -> MyMaybe t
maybeHead [] = Nothing
maybeHead (x:_) = Just x

and it evaluates fine. But this

maybeHead [1,2,3]

produces error:
:     • No instance for (Show (MyMaybe Integer))
:         arising from a use of ‘show’
:     • In the expression: show (maybeHead [1, 2, 3])
:       In an equation for ‘it’: it = show (maybeHead [1, 2, 3])

Similarly,

data Color a = Blue a | Green a | Red a

myFavoriteColor :: Color Int
myFavoriteColor = Green 50

but now I'm stuck. How can I access the "Green 50" inside myFavoriteColor?

LB

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: No ghci return for homemade types

Francesco Ariis
Il 03 gennaio 2021 alle 16:22 Lawrence Bottorff ha scritto:

> I've tried this
>
> data MyMaybe a = Just a | Nothing
>
> maybeHead :: [t] -> MyMaybe t
> maybeHead [] = Nothing
> maybeHead (x:_) = Just x
>
> and it evaluates fine. But this
>
> maybeHead [1,2,3]
>
> produces error:
> :     • No instance for (Show (MyMaybe Integer))
> :         arising from a use of ‘show’
> :     • In the expression: show (maybeHead [1, 2, 3])
> :       In an equation for ‘it’: it = show (maybeHead [1, 2, 3])

    data MyMaybe a = Jus a | Not
            deriving (Show, Eq)

Your text should have introduced to/warned you about deriving and
typeclasses.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners