
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

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

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
participants (2)
-
Chicão Fernandes Junior
-
Sean Bartell