data Test = [Int] | Int
foobar :: Test -> Int
What if I wanted something like foobar [1,2,3] = 1 and foobar 1 = 1. In erlang it would be
foobar(X) when is_list(X) -> hd(X);
foobar(X) -> X.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, your data-declaration is invalid. In Haskell, you have to start a data-declaration with a data-constructor, that is later matched upon. For instance, your type
Testwould be writtenNow you can simply match on the type-constructor; it’s field is a list or an int, depending on which constructor you match: