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
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
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
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.