-- This is an etude which constructs a "function" in ALAN by blatantly -- cheating the rules of the language. -- -- To be specific, the function is an object hidden in limbo. Its -- attributes become variables that can be set by whoever "calls" the -- function. Calling is done by issuing the DESCRIBE command at it. The -- DESCRIBE section can contain any commands -- loops, conditionals, -- other DESCRIBEs, arithmetic operations, anything. There's your function! -- -- This example is simple, awkward, and fairly useless, but it should -- express the idea sufficiently. For example, without too much more -- work a four-function calculator could be built. $include 'shell.alan' SYNTAX press = 'press' (button) 'on' (obj) WHERE button ISA STRING ELSE "What?" -- The basic room and one object to play with. LOCATION waiting_room NAME Waiting Room DESCRIPTION "You're standing in a nondescript, slightly dingy waiting room with white painted walls and a tile floor." END LOCATION. OBJECT widget NAME CompuWidget AT waiting_room IS takable. HAS weight 5. HAS num1 0. HAS num2 0. HAS num3 0. VERB examine DOES ONLY "This is a sort of compu-widget; it has the numbers" SAY num1 OF widget. "," SAY num2 OF widget. "and" SAY num3 OF widget. "on its screen, and a couple of buttons -- 'RND' and 'SUM'." END VERB. VERB press DOES ONLY IF button = "RND" THEN SET num1 OF widget TO RANDOM 1 TO 50. SET num2 OF widget TO RANDOM 1 TO 50. SET num3 OF widget TO RANDOM 1 TO 50. ELSIF button = "SUM" THEN SET num1 OF sum_function TO num1 OF widget. SET num2 OF sum_function TO num2 OF widget. SET num3 OF sum_function TO num3 OF widget. DESCRIBE sum_function. "The Compu-Widget whirrs and beeps and says," SAY result OF sum_function. ELSE "There is no such button." END IF. END VERB. END OBJECT. OBJECT sum_function AT limbo HAS num1 0. HAS num2 0. HAS num3 0. HAS result 0. DESCRIPTION SET result OF sum_function TO num1 OF sum_function + (num2 OF sum_function + num3 OF sum_function). END OBJECT. -- START section START AT waiting_room.