# 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 ```prolog % The four words are pure stack shufflers: % % * `dup` copies the top item. % * `drop` removes the top item. % * `swap` exchanges the top two items. % * `over` copies the second item onto the top. % % Runtime word clauses describe concrete stack transformations. 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. The program relation % keeps the same two-stack shape: % % run(Words, InputStack, OutputStack). % % `Stack1` is the output of the first word and the input to the rest % of the program. % % Runtime sequencing threads the intermediate stack through each word. run([], Stack, Stack). run([Word|Words], Stack0, Stack) :- apply(Word, Stack0, Stack1), run(Words, Stack1, Stack). ```