[Haskell-cafe] Trouble with non-exhaustive patterns
Hi, I defiend the following function to get the last element of a list: final [a] = a final (_:t) = final t and it works as expected. Since I didn't want to have a non exhaustive pattern, I added the following case: final [] = [] - I consider that the end of an empty list is the empty list final [a] = a final (_:t) = final t Suddenly, the function stoped working with a rather cryptic (for a newbie at least) error message: *Temp final [4,5] interactive:1:9: No instance for (Num [a]) arising from the literal `5' at interactive:1:9 Possible fix: add an instance declaration for (Num [a]) In the expr*Temp ession: 5 In the first argument of `final', namely `[4, 5]' In the expression: final [4, 5] What have I done so wrong? Thanks in advance, Fernando ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Access to list
Hi, If I define the follwoing functions: car (x:_) = x car [] = [] cdr (_:xs) = xs cdr [] = [] and try to apply them to some list, such as car [1,2,3] I get this odd error: interactive:1:9: No instance for (Num [a]) arising from the literal `3' at interactive:1:9 Possible fix: add an instance declaration for (Num [a]) In the expression: 3 In the first argument of `car', namely `[1, 2, 3]' In the expression: car [1, 2, 3] What am I doing wrong this time? :-P Thanks ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Patterns overlapped?
Hi, When I compile this code, ghc complains about some overlapped patterns in function depth. What on Earth is ghc talking about? O:-) data BinTree a = EmptyTree | NodeBT a (BinTree a) (BinTree a) deriving Show emptyBT = EmptyTree depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Patterns overlapped?
Hello Fernando, Hi, When I compile this code, ghc complains about some overlapped patterns in function depth. What on Earth is ghc talking about? O:-) data BinTree a = EmptyTree | NodeBT a (BinTree a) (BinTree a) deriving Show emptyBT = EmptyTree depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right) Sorry, the exact error is: Warning: Pattern match(es) are overlapped In the definition of `depth': depth (NodeBT _ left right) = ... ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Patterns overlapped?
Hello Brandon S. Allbery KF8NH, depth emptyBT = 0 depth (NodeBT _ left right) = max (1 + depth left) (1 + depth right) If you use a variable in a pattern match, it creates a new binding which irrefutably matches the corresponding argument. In other words, you get a new local variable emptyBT, ignoring your global. You must use the actual constructor instead. I was wondering why depth always returned zero... Thanks. :-) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Currying and Partial Evaluation
Hello Jules, I'm not sure you got a straight answer, although you provoked some discussion. Thanks for your answer, now everything is much clearer. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Trouble with indentation
Hi, I'm trying to write a function that finds out the week day using the Zeller congruence (http://en.wikipedia.org/wiki/Zeller's_congruence). However, ghc complains about: parse error on input `=' at the if m = 2 then line. I believe this is some sort of layout error. Can someone point out what I am doing wrong? Here's the code: data DiaSemana = Lunes | Martes | Miercoles | Jueves | Viernes | Sabado | Domingo deriving (Show, Eq, Ord, Enum) diaDeSemana d m a = toEnum num :: DiaSemana where num = zeller x y z zeller x y z = (700 + (26 * x - 2) `div` 10 + d + y + y `div` 4 + z `div` 4 - 2 * z) `mod` 7 if m = 2 then x = m + 10 y = (a - 1) `mod` 100 z = (a-1) 'div' 100 else x = m - 2 y = a `mod` 100 z = a `div` 100 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Silly question: accessing a slot in a data type
Hi, I'm writing a very simple address book. I defined the follwoing types for Contact and AddressBook: type Name= String type PhoneNumber= Integer data Contact = Contact {name :: Name, phone:: PhoneNumber} deriving Show -- The addressBook is a lista of Contacts data AddressBook= AddressBook [Contact] deriving Show -- Create a test one ag = Agenda [Contact BillG 618965332, Contact Linus 5897458] I wan't to write a function to search for a given phone by name and return a Maybe PhoneNumber. I know how to do this recursively on a raw list, but how do I access the list inside AddressBook? Also, how do I represent an AddressBook with an empty list and one with data on its list for my pattern matching? Thanks! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Trouble with function with two clauses
Hi, I have the following type and function: data ConsCell a = Nil | Cons a (ConsCell a) deriving Show head' Nil = Nothing head' (Cons a _) = Just a Works fine, however, what's wrong with the following function? head'' | Nil = Nothing | Cons a _ = Just a Thanks! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Silly question: accessing a slot in a data type
Hello Miguel, Also, how do I represent an AddressBook with an empty list and one with data on its list for my pattern matching? phoneLookup (AddressBook []) = Nothing Thanks. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Newbie question: the need for parentheses
Hi, I have this function that sums up a list of numbers: suma [] = 0 suma (h:t) = h + suma t However, why do I need the parenthes in the second clause? What does the compiler think I'm trying to do when I type suma [] = 0 suma h:t = h + suma t Thanks! :-) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Displaying # of reductions after each computation in ghci?
Hi, Is there a way to configure ghci to display the number of reuctions after each compution (as in winhugs)? Thanks! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Currying and Partial Evaluation
Hi, Is currying in Haskell the same thing as Partial Evaluation (http://en.wikipedia.org/wiki/Partial_evaluation)? Am I getting partial evaluation for free just by using Haskell? Thanks! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Displaying # of reductions after eachcomputation in ghci?
Hello Stefan O'Rear, No. (rambling explanation snipped awaiting further request) Stefan OK, I'll take the bait: why not? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Type error with simple list function
Hi, This simple function definition that should rotate a list (put the first item in the last place) fails with a rather cryptic error with ghc: f :: [a] -[a] -[a] f (w : ws) = ws : w Couldn't match expected type `[a] - [a]' against inferred type `[[a]]' In the expression: ws : w In the definition of `f': f (w : ws) = ws : w What's Haskell trying to tell me? I'm a newby so please forgive my ignorance. Thanks! Fernando ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe