# Lesson II One motion is enough to shape the next. What repeats becomes habit; what becomes habit becomes cause. ## Try ```prolog ?- apply(over, [a, b], Stack). Stack = [b, a, b]. ?- apply(swap, In, [b, a]). In = [a, b]. ?- run([swap, over], [a, b], Stack). Stack = [a, b, a]. ?- run([swap, over], [A, B], Stack). Stack = [A, B, A]. ``` ## Learn A word relates an input stack to an output stack, and a program repeats that relation over a list of words. > [!info]- Change > ```diff > - word(dup). > - word(drop). > - word(swap). > - word(over). > - > - run([]). > - run([Word|Words]) :- > - word(Word), > - run(Words). > ``` These words are tiny stack transformers: * `dup` copies the top item. * `drop` removes the top item. * `swap` exchanges the top two items. * `over` copies the second item onto the top. ```prolog apply(dup, [A|S], [A, A|S]). apply(drop, [_|S], S). apply(swap, [A, B|S], [B, A|S]). apply(over, [A, B|S], [B, A, B|S]). ``` A concatenative program is a list of words. Running it threads the intermediate stack through each word. ```prolog run([], Stack, Stack). run([Word|Words], Stack0, Stack) :- apply(Word, Stack0, Stack1), run(Words, Stack1, Stack). ``` [[01_lesson|Prev]] | [[03_lesson|Next]]