Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-beginners] Empty list Exception

0 views
Skip to first unread message

Chicão Fernandes Junior

unread,
May 24, 2009, 3:13:26 PM5/24/09
to begi...@haskell.org
Hello!

I'm starting programming in haskell and I got some problems
running in GHCi (version 6.8.2) a module I've wrote.

The following ...

--The first string is the name and the second is the user ID, just for test
>
type User = (String, String, Integer)
>


> users = [("Marcelo Castro", "1234", 2),
> ("Joao Vicente Claudino", "1235", 2),
> ("Wilson Brandao", "1236", 2),
> ("Humberto Teixeira", "1237", 2),
> ("Luiz Gonzaga", "1238", 2),
> ("Severino Dias", "1239", 2),
> ("Hermeto Pascoal", "1234", 2)]
>


> checkUser :: User -> [User] -> Bool
> checkUser user userList
> = if (head (filter (\userParse -> userParse == user) userList)) ==
> user
> then True
> else False
>
>
> userRegistration :: User -> [User] -> [User]
> userRegistration user list
> | checkUser user list = list
> | not(checkUser user list) = (user : list)
> | otherwise = []
>


--
____________________________________________________________________________
Francisco Borges "Chic�o" Junior
.........................................................................................
http://pedepinico.blogspot.com
.........................................................................................
"Quem de boa vontade carrega o dif�cil, tamb�m carrega o menos dif�cil..."
Lao Ts� - Tao Te Ching

Chicão Fernandes Junior

unread,
May 24, 2009, 3:17:24 PM5/24/09
to begi...@haskell.org
Sorry, I send it accidently. I did't finish my question.

Anyway, when I run in GHCi:

userRegistration ("Joao Claudino","1240",2) users
>


It gives me the exception:

*** Exception: Prelude.head: empty list
>

Can anyone explain to me why the exception is thown and what should I do to
fix it?

Thanks

Sean Bartell

unread,
May 24, 2009, 3:35:19 PM5/24/09
to Chicão Fernandes Junior, begi...@haskell.org
When you use filter in this case, it's checking for users that are the same
as ("Joao Claudino", "1240", 2). There aren't any in the list, so filter
returns []. You can't use head on [], because there's no first element.

Other notes about your code:

- if x then True else False is the same as just x.
- The otherwise case for userRegistration can never be reached.


I would write it more like this:

checkUser user userList = elem user userList

userRegistration user list
| checkUser user list = list

| otherwise = user:list

Chicão Fernandes Junior

unread,
May 24, 2009, 10:14:50 PM5/24/09
to Joel Neely, begi...@haskell.org
Thanks everyone, for the answers.

I didn't knew the *elem* function. That will make my life easier.

For the sake of simplicity, I changed the code to a shorter
*userRegistration* function, eliminating the *checkUser* function:


userRegistration user list
> | elem user list = list
> | otherwise = user:list
>


It did worked fine.

I'm glad that I can count with this list in my learning process.

Thanks again.

0 new messages