Your first app
For: Anyone who wants to make apps and games. Some Clojure helps; the Quick start is enough.
A p'lisp app is three plain functions and a description. You'll write a counter that you turn with the wheel and that beeps, run it in the simulator, and share it. It takes about ten minutes.
1. Open the Workbench
Open the Workbench. Open the menu at the top left and press New. You get an empty program, kept in your browser.
2. Paste the app
Replace everything in the editor with this, then press Run. The app opens in the simulator. Turn the wheel, press + and -, and click the wheel to go back to 0.
(ns my.counter
"My first app: turn the wheel, or press + and -, to count. Click the wheel to start again."
(:require [plisp.app :refer [defapp]]))
;; A short rising beep and a falling one.
(def up-song "tempo=400 step=16\npulse1: duty=50 vol=6 decay=2 C6 E6")
(def down-song "tempo=400 step=16\npulse1: duty=50 vol=6 decay=2 Eb6 C6")
(defn init
"Where the app starts: the model is a map, and there is nothing to do yet."
[cx]
[{:n 0} []])
(defn count-by
"Move the count by d, with a beep that goes up or down."
[m d]
[(update m :n + d) [[:chip/song (if (pos? d) up-song down-song)]]])
(defn step
"One event in, the new model and a list of things to do out."
[m [tag k] cx]
(case tag
:wheel/turn (count-by m k)
:wheel/click [(assoc m :n 0) []]
:key (case k
:k/add (count-by m 1)
:k/sub (count-by m -1)
[m []])
[m []]))
(defn draw
"What the screen shows, as a list of drawing steps."
[m screen]
[[:clear]
[:text 64 20 (str (:n m)) {:align :center}]
[:text 64 55 "click: back to 0" {:align :center :font :small}]])
(defapp counter
{:id :my/counter :name "Counter" :version 1
:input #{:raw} :uses #{:sound}}
:init init
:update step
:view draw)
3. How it works
An app never changes anything itself. Each function gets what it needs and returns data, and the calculator does the rest. That makes an app easy to test and easy to change while it runs.
initreturns the first model (a map with everything the app knows) and a list of effects (things for the calculator to do). Here the model is{:n 0}and there is nothing to do.stepgets the model and one event, like[:wheel/turn 3]or[:key :k/add], and returns the new model and its effects.[:chip/song ...]asks for a beep.drawturns the model into a list of drawing steps for the screen.defappnames the app and says what it uses.:input #{:raw}asks for the wheel and the keys as they are;:uses #{:sound}asks for the speaker.
4. Change it while it runs
Change (update m :n + d) to (update m :n + (* 10 d)), press Run again, and the wheel counts in tens. Try a new line in draw, like [:text 64 2 "COUNTER" {:align :center :font :small}].
5. Share it
Press Share. You get a link that opens your app for anyone, in their browser, as their own copy to play and change. Yours stays as it is. To remix someone else's app, open their link and change it the same way.
You can also put your app on your own site or blog: make a short link, then press Embed. Embed an app shows the code and its options.
6. Learn from the built-in apps
In the Workbench menu, Start from an app opens the code of every app on the calculator, games included. Each one is a remix you can change. Look up any name in the Clojure reference.