I am confused why my function nest
which composes f
with itself n
times
nest f 0 = id
nest f n = f . nest f (n - 1)
never terminates. I would have thought that it would "pattern match" on the case when n
becomes zero. I am defining it by typing these two lines into GHCI and calling with nest (+ 1) 2 3
for instance.
This represents the fact that Haskell programs are not guaranteed to terminate and can have exceptions. I.e., all Agda programs will terminate, and a Bool in Agda corresponds to exactly {True, False} .
The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program.
Essentially, a >> b can be read like "do a then do b , and return the result of b ". It's similar to the more common bind operator >>= .
If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.
By typing the function on two separate REPL lines, you are essentially redefining it the second time around, omitting the base case.
The correct way to enter this function into the REPL is:
nest f 0 = id; nest f n = f . nest f (n - 1)
Alternatively, you can enter multiline mode with the :{
command, and leave it using :}
.
When you pasted it into GHCi what you did was define one function of nest f 0 = id
. Then you said "ignore that function, I'm replacing it with a new function of the same name where the whole definition is nest f n = f . nest f (n - 1)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With