Why do i need to take out the list for this to work

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why do i need to take out the list for this to work

Alexander Chen
hi,

assigment: make your own element function with the any function.

--elem with any
myElemAny :: Eq a => a -> [a] -> Bool
myElemAny a = any (== a)

--elem with any
myElemAny' :: Eq a => a -> [a] -> Bool
myElemAny' a [x]= any (== a) [x]


myElemAny' compiles but throws an error because it has a non-exhaustive pattern. Could somebody tell me why the list gives the function grieveness?

thanks,


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

Re: Why do i need to take out the list for this to work

Bob Ippolito
Square brackets [] are pattern match syntax for lists. This will only work for lists of length 1, anything else will be an error. Get rid of the brackets on both sides of the equation and it will do what you expect.

Typically list variables are given plural names, such as xs instead of x.

On Wed, Jun 10, 2020 at 11:42 Alexander Chen <[hidden email]> wrote:
hi,

assigment: make your own element function with the any function.

--elem with any
myElemAny :: Eq a => a -> [a] -> Bool
myElemAny a = any (== a)

--elem with any
myElemAny' :: Eq a => a -> [a] -> Bool
myElemAny' a [x]= any (== a) [x]


myElemAny' compiles but throws an error because it has a non-exhaustive pattern. Could somebody tell me why the list gives the function grieveness?

thanks,

_______________________________________________
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: Why do i need to take out the list for this to work

Kim-Ee Yeoh
Administrator
In reply to this post by Alexander Chen


On Thu, Jun 11, 2020 at 1:42 AM Alexander Chen <[hidden email]> wrote:
hi,

assigment: make your own element function with the any function.

--elem with any
myElemAny :: Eq a => a -> [a] -> Bool
myElemAny a = any (== a)

--elem with any
myElemAny' :: Eq a => a -> [a] -> Bool
myElemAny' a [x]= any (== a) [x]


From a beginners perspective, the second function looks perfectly cromulent. After all, how should one indicate a list if not by enclosing it with square brackets like this, [x]? 

An x might or might not be a list, but [x] surely has to be one, yes?

But consider all the things that could be a list. Specifically, look at the following:

1. An empty list—which haskell denotes using []—is a list
2. [1,2,3] is a list
3. [1,1,1,…] is a list

In each of the cases above, what is the value of x in [x]?

Recall that haskell is a value-oriented language. Every identifier at the term-level evaluates to something that has a well-defined type.



myElemAny' compiles but throws an error because it has a non-exhaustive pattern. Could somebody tell me why the list gives the function grieveness?

thanks,

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

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