1

I'm going through Learn you a Haskell and some of the examples don't seem to work. I'm assuming that tutorial is reputable enough, so I'm a bit surprised. A couple of examples, in GHCi 8.0.2:

length' [] = 0  
length' (_:xs) = 1 + length' xs
l = [1,2,3]
length' l
*** Exception: <interactive>:2:1-31: Non-exhaustive patterns in function length'
sum' [] = 0
sum' (x:xs) = x + sum' xs
l = [1,2,3]
sum' l
*** Exception: <interactive>:2:1-25: Non-exhaustive patterns in function sum'

From here: http://learnyouahaskell.com/syntax-in-functions.

The functions look correct enough, so, what's going on here, what did I do wrong?

duplode
  • 33,731
  • 7
  • 79
  • 150
Erik Vesterlund
  • 481
  • 6
  • 19
  • I assume you defined this in the terminal? https://stackoverflow.com/q/26738392/67579 – Willem Van Onsem May 19 '19 at 18:46
  • 2
    In addition to the question suggested by Willem, there are ways to make it work directly in GHCi. See [*Function definition by special cases in GHCi*](https://stackoverflow.com/q/42593284/2751851). – duplode May 19 '19 at 18:52

0 Answers0