3

On the first code example in "Programming in Haskell" the following is stated:

Recall the function sum used earlier in this chapter, which produces the sum of a list of numbers. In Haskell, sum can be defined using two equations:

sum [] = 0      
sum (n:ns) = n + sum ns

Immediately, this code fails both in the ghci interpreter and upon compilation with the error: "Non-exhaustive patterns in function sum"

After further research it seems this is because the case of a single number isn't covered. What gets me is the next few examples in the book also fail to work.

Am I missing something here? The book was released rather recently in 2016 and I can't find anyone else complaining aout this.

1 Answer 1

10

When you enter the first clause of the definition of sum and press enter GHCI assume that you've finished and it should evaluate the program. The program

sum [] = 0

Doesn't specify what to do if the argument is non-empty, so you get the "non-exhaustive patterns" error.

GHCI has :{ and :} commands to allow you to enter multiple clauses (or other code that spans multiple lines:

Prelude> :{
Prelude| sum [] = 0
Prelude| sum (n:ns) = n + sum ns
Prelude| :}
Prelude> sum []
0
Prelude> 

In general I would recommend saving the definitions you are working with in a file and loading that file in GHCI. Then you can :reload when you make some changes and call your function/s with various arguments to see what happens.

Sign up to request clarification or add additional context in comments.

11 Comments

@user4779 (1/2) I think you may be a bit confused about what you put in a file. A Haskell file just contains a list of functions (plus possibly some import statements and a module header) — here’s an example from my own code. You can load this file into GHCi using the :load command, after which you can call the functions from GHCi.
@user4779 (2/2) If one of your functions is called main, then when you compile your code with GHC, the resulting standalone executable will start by automatically calling main. However, if you load that file into GHCi, then main will just be another function. Note that main is defined the same way as all other functions, and as such, it shouldn’t have an impact on the syntax you use to define your other functions. (Of course, you can use let to define your function within main, but that’s different to what you want to do.)
@user4779 (1/2) There’s no reason why you can’t do both! Many Haskell programs — probably the vast majority of them, in fact — implement the overall control flow of the program in main, but then call out to other functions for the actual logic. For instance, the main function of my program here just outlines the overall processing — open a file, parse it, convert it, render it to text and then save the result…
@user4779 (2/2) …while calling functions such as parse, convert and render (defined elsewhere) for the actual logic. This lets me keep my code readable, ensures a nice separation of concerns between the functions, and lets me test the various parts (each of which corresponds to a function) separately in GHCi while still letting me run the whole executable.
@user4779 (And by the way, when using GHCi, main is just a function like all the others — there’s no reason why you can’t load your module and then call main from GHCi, although in practise you don’t often need to do that. However, as I already implied, it’s generally considered good practise to structure your program using many small functions rather than one large function. One reason for this is that you can then test each small function in GHCi if you need to, while you can’t easily test a part of a large monolithic main function.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.