Skip to content

Commit abd0713

Browse files
Abhikgitbook-bot
Abhik
authored andcommitted
GITBOOK-15: No subject
1 parent 806aa4b commit abd0713

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

build-your-first-interactive-program.md

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ In the Exec, type:  
4242

4343
Medley is now aware of a new region named `window-region`. We can use this region for any new window we make.
4444

45-
2. `(SETQ display-window (CREATEW window-region "DISPLAY WINDOW" 10))` 
45+
2. `(SETQ display-window (CREATEW window-region "DISPLAY MODULE" 10))` 
4646

4747
This creates a new variable `display-window` which when called will create a new window at the region `window-region`.
4848

49-
3. Create a couple of instances of our new window by typing `display-window` in the Exec.
49+
3. We can open or close this window with `(OPENW display-window)` and `(CLOSEW display-window)`.at
5050

5151
{% hint style="info" %}
5252
We don't write variable names inside " " because they are not static strings to store but data containers that Medley can open.
@@ -69,35 +69,86 @@ Creating a menu defines a menu object in the background but doesn't create an in
6969
Similar to MENU, we can also use:
7070

7171
* `(ATTACHMENU button-menu display 'TOP 'CENTER)` : Attaches the menu `button-menu` to the window `display` at the top, centered.
72-
* `(ADDMENU button-menu display)` : Adds `button-menu` to the bottom-left corner of `display` . 
72+
* `(ADDMENU button-menu display)` : Adds `button-menu` to the bottom-left corner of `display-window` . 
7373

7474
In your Exec type:
7575

76-
1. `(SETQ button-menu (CREATE MENU ITEMS ← '("NAVIGATION SYSTEM FAILED"))` 
76+
1. `(SETQ button-menu (CREATE MENU ITEMS ← '("NAVIGATION SYSTEM FAILURE")))` 
7777

7878
This creates a menu called `button-menu` with no title and one item only.
7979

80-
2. `(ATTACHMENU button-menu display 'LEFT 'CENTER)`
81-
82-
You should see a button-like window appear on the left edge of the window `display` with the text "NAVIGATION SYSTEM FAILED".
80+
2. `(ATTACHMENU button-menu display-window 'LEFT 'CENTER)`\
81+
You should see a button-like window appear on the left edge of the window `display-window`with the text "NAVIGATION SYSTEM FAILED".
82+
3. Right now, the button doesn't do anything. Next, we'll connect a function to the button which can print text to `display-window` .
8383

8484
{% hint style="info" %}
85-
Right now, the button doesn't do anything. Next, we'll connect a function to the button which can print text to `display` .
85+
We can detach and remove this menu with `(DELETEMENU button-menu display-window)` . This does not delete the menu from memory. You can always repeat Step 2 to reattach the menu.
8686
{% endhint %}
8787

8888
***
8989

90+
Let's create a list of distress calls we can randomly cycle through each time we press our button! In your Exec, type:
91+
92+
{% code overflow="wrap" lineNumbers="true" fullWidth="false" %}
93+
```lisp
94+
(SETQ distress-calls (LIST
95+
"Help! Nav systems compromised. What is lost will never be found."
96+
"Help! Nav systems compromised. Lethal solar flare imminent."
97+
"Help! Nav systems compromised. Stuck in orbit. Planetfall- ETA: 2 cycles."
98+
"Help! Nav systems compromised. Class 3 Destroyer approaching. Contact- ETA: 6 cycles."
99+
"Help! Nav systems compromised. Direction constant but unknown. Debris field. Contact- High."
100+
))
101+
```
102+
{% endcode %}
103+
104+
Now, that we have a list named `distress-calls` , we can set up a function called `nav-sys` that displays a random message from this list to our `display-window` . Next, we'll connect this function to our `button-menu`.
105+
106+
***
107+
90108
We can define a new function with `DEFINEQ` . In your Exec, type:
91109

92-
`(DEFINEQ (print2display (LAMBDA (X) (PRIN1 "Help! Navigation system compromised. What is lost will never be found." display))))`
110+
`(DEFINEQ (nav-sys (LAMBDA (X) (PRIN1 (CAR (NTH distress-calls (RAND 1 (LENGTH distress-calls)))) display-window))))`
111+
112+
Let's break down our function:
113+
114+
1. `nav-sys`: Name of function
115+
2. `PRIN1`: for printing to a specific window
116+
3. `CAR`: returns the first element of a list
117+
4. `(NTH list number)`: returns the tail of the specified `list`starting from the specified `number`. So, if we have a list `(A B C D)` and we use `(NTH (ABCD) 2)`, we'll get `(B C D)` as the output.
118+
5. `(RAND value1 value2)`: returns a random value in a range from `value1` from `value2`.
119+
6. `(LENGTH list)`: returns the length of the list.
120+
7. `(CAR (NTH distress-calls (RAND 1 (LENGTH distress-calls))))` : 
121+
1. `(RAND 1 (LENGTH distress-calls))` returns a random number between 1 and the total length of the list `distress-calls`.
122+
2. `(NTH distress-calls` returns the tail of the list starting from the element at that random number.
123+
3. `(CAR` returns the first element of that tail.
124+
4. `(PRIN1 ... display-window)` prints that element to `display-window` .
125+
5. And of course, `(DEFINEQ (nav-sys (LAMBDA (X)` gives the name `nav-sys` to our function. For our current function, we have no use for any parameters, so X is declared following conventions but unused.
126+
127+
***
128+
129+
Let's delete the button we made before with `(DELETEMENU button-menu display-window)` .
130+
131+
We'll make a new `nav-button` with our `nav-sys` function attached!
132+
133+
In your Exec, type:
93134

94-
This tells Medley to create a function called print2display which has a parameter `X`and a definition `(PRIN1 "Help! Navigation system compromised. What is lost will never be found." display)` .
135+
{% code overflow="wrap" lineNumbers="true" %}
136+
```lisp
137+
(SETQ nav-button
138+
(CREATE MENU
139+
ITEMS ← '("NAVIGATION SYSTEM FAILURE")
140+
WHENSELECTEDFN ← (FUNCTION nav-sys)
141+
))
142+
```
143+
{% endcode %}
95144

96-
The function `prints2display`prints the text in the window `display` .
145+
Our new button only has a small addition (and a different name). 
97146

98-
\[unfinished section about creating lists and cycling through them]
147+
`WHENSELECTEDFN ← (FUNCTION nav-sys)` executes the function `nav-sys` when menu-item (our button `nav-button` ) is selected.
99148

149+
Go ahead and interact with your button!
100150

151+
Is your distress call module working? Cool! In the upcoming chapters, we'll add more features to our Display Module. While building this project, for each feature we'll explore and learn new bits of Medley Interlisp!
101152

102153

103154

0 commit comments

Comments
 (0)