Unlike other Higher Order Functions which lazily returns a transformed sequence, the word
immediately iterates through a sequence. Because
is not lazy you have to ensure that the target sequence is finite otherwise
will never end:
1 2 ... 100 take
' max reduce .
The next thing to remember when using
is that it often requires some existing data ("initial values") on the stack. In the example above, the word
requires two arguments. Without the existing
on the stack, the reduction will fail because of
. Be reminded also that
requires an XT to perform a calculation. The word
immediately prints out every element of a sequence and therefore is not lazy. It can be defined using
: .list ['] . reduce cr ;
In the example above,
iterates through the sequence and calls
to print out each element. You may notice the word
prints a new line and avoids the confusion between the data list and subsequent items printed by other words.
Write a word
to find the minimum of a sequence (assume it is finite) without making any assumptions about the upper bound. Hint: you may consider using the first item (what is it called in the sequence?) as the initial upper bound.
Write a word
that reverses any list. Does your word work for empty sequences? Hint: when introducing Sequence, do you still remember the difference between two ways of creating a sequence?
Next: Main Menu