Maybe?

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

Maybe?

Alexander Chen
Hi,

safeHead.hs

safeHead :: [a] -> Maybe a
safeHead []    = Nothing
safeHead (x:_) = Just x

prelude> safeHead [23,1,4,2,4,2]
Just 23


Question:
what am i doing wrong?

best,

Alexander



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

Re: Maybe?

Imants Cekusins
prelude> safeHead [23,1,4,2,4,2]
Just 23

Looks good.

What did you expect to see?

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

Re: Maybe?

Alexander Chen
I expected 

prelude> safeHead [23,1,4,2,4,2]
23

not
Just 23

May 13, 2020 4:11:47 PM CEST Imants Cekusins <[hidden email]> wrote:">
prelude> safeHead [23,1,4,2,4,2]
Just 23

Looks good.

What did you expect to see?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




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

Re: Maybe?

Velichko Lefterov
The function returns Maybe a.

On Wed, May 13, 2020 at 5:16 PM Alexander Chen <[hidden email]> wrote:
I expected 

prelude> safeHead [23,1,4,2,4,2]
23

not
Just 23

May 13, 2020 4:11:47 PM CEST Imants Cekusins <[hidden email]> wrote:
prelude> safeHead [23,1,4,2,4,2]
Just 23

Looks good.

What did you expect to see?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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

Re: Maybe?

Imants Cekusins
Maybe a is a sum type.

Show instance for sum types displays the constructor.

For example, if we used another sum type:
Either Int Int,
then 
Left 23 /= Right 23

So we'd be interested to see the constructor to tell them apart. 23 wouldn't be sufficiently clear.


On Wed, May 13, 2020 at 5:18 PM Velichko Lefterov <[hidden email]> wrote:
The function returns Maybe a.

On Wed, May 13, 2020 at 5:16 PM Alexander Chen <[hidden email]> wrote:
I expected 

prelude> safeHead [23,1,4,2,4,2]
23

not
Just 23

May 13, 2020 4:11:47 PM CEST Imants Cekusins <[hidden email]> wrote:
prelude> safeHead [23,1,4,2,4,2]
Just 23

Looks good.

What did you expect to see?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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

Re: Maybe?

Imants Cekusins
Actually, derived Show instance for _any_ type displays the constructor.

I hope this statement is correct ;)

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners