Reputation: 29
I have been reading the book "Programming in Haskell by Hutton".
This is one of the exercises about lazy evaluation from the book:
1. Identify the redexes in the following expressions, and determine whether each redex is innermost, outermost, neither, or both:
a. 1 + (2*3)
b. (1+2) * (2+3)
Since this is the question from the textbook, I checked the answers and it shows:
a. The only redex in 1+(2*3) is 2*3, which is both innermost and outermost.
I completely understand the fact 2*3 is innermost because there is no other redex in the bracket. But why is 2*3 outermost redex? If I am not mistaken, outermost redex
b. The redexes in (1+2)*(2+3) are 1+2 and 2+3, with the first being innermost.
Then the answer for b, shows that only 1+2 is the innermost while 2+3 is nether innermost nor outermost.
I am a bit confused about the main difference between outermost and innermost redex after reading this question. Any help would be appreciated, thank you.
Upvotes: 1
Views: 422
Reputation: 50864
Hutton's presentation and definitions here are quite confusing. In the context of this chapter, the +
and *
operations are considered strict, primitive functions, so an expression a + b
or a * b
is only reducible (and so a redex) if both a
and b
are numbers.
As he defines them, the innermost redex is the leftmost redex that contains no other redexes. The outermost redex is the leftmost redex that is contained within no other redex.
In part (a), the only redex is 2*3
. It is innermost because it contains no other redexes, and it is outermost because it is contained within no other redexes.
In part (b), the only redexes are 1+2
and 2+3
. Neither contains other redexes, and neither is contained within another redex. Since ties are broken by choosing the leftmost redex, 1+2
is clearly and unambiguously both the innermost and outermost redex by his definitions. So, the textbook answer appears to be wrong.
In any event, I wouldn't sweat it. This is the least important exercise in this chapter.
Upvotes: 1