Welcome to your first Lispex project via the LENA code transformer! In the Getting Started guide, you learned how to use the web transformer to convert Lispex into a target language and run it with that language’s toolchain. Now we’ll build a small, complete program that processes data and prints a formatted report.
The Goal
We will write a program that:
- Defines a list of tasks, each with a name and a priority.
- Filters this list to find only the high-priority tasks.
- Formats the high-priority tasks into readable strings.
- Prints the final report to the console.
This will teach you how to define data structures, create helper functions, and use powerful built-in functions alongside standard library modules like io and string.
Step 1: Open LENA code (no local file yet)
Open LENA code at https://www.lenacode.com. A new Lispex file is auto‑named LENA_001.lspx. We’ll paste the following code in the Input editor.
Step 2: Define Your Data
First, let's define our list of tasks. We'll use a variable named tasks to hold a list of lists. Each inner list will contain a task name (a string) and its priority (a number, where higher is more important).
;; LENA_001.lspx (auto‑named in LENA code)
;; Define our source data: a list of tasks.
(define tasks
(list (list "Write documentation" 2)
(list "Fix critical bug" 3)
(list "Refactor the core engine" 3)
(list "Add new feature" 1)
(list "Plan marketing campaign" 2)))
Step 3: Write a Helper Function
It's good practice to break down logic into small, reusable functions. Let's create a function that checks if a task is "high priority" (we'll define this as priority 3 or higher).
;; (Add this in the same LENA_001.lspx file)
;; A helper function that returns `#t` if a task's priority is ≥ 3.
;; `(list-first (list-rest task))` is a common Lisp-family idiom to get the second element of a list.
(define (is-high-priority? task)
(>= (list-first (list-rest task)) 3))
Step 4: Process the Data into a Report
Now for the main event! We will chain together built-in functions to transform our raw tasks data into a final list of formatted report strings.
filterwill use ouris-high-priority?function to create a new list containing only the high-priority tasks.mapwill take that filtered list and apply alambdafunction to format each task into a nicely readable string, usingstr-concatfor safety.
;; (Add this below, still in 001.lspx)
;; 1. Filter the tasks list.
(define high-priority-tasks (filter is-high-priority? tasks))
;; 2. Map the filtered list to a new list of formatted report lines.
(define report-lines
(map (lambda (task)
(string-append "URGENT: " (list-first task)))
high-priority-tasks))
Step 5: Print the Final Report
Our report-lines variable now holds the data we want to display. To actually show it to the user, we need to perform an "I/O action" (Input/Output). We'll use the println function we imported earlier, applying it to each line in our report.
;; (Add this as the final expressions in 001.lspx)
;; 3. Print each line of the report to the console.
;; (Add this code to task_report.lspx)
(println "--- High-Priority Task Report ---")
(map println report-lines)
(println "---------------------------------")
Step 6: Transform and run in your target
In LENA code, choose an Output language and click Transform. Copy the generated code and run it with that language’s toolchain. Examples:
- Python: save as
report.py, runpython report.py - Node.js (TypeScript): save as
report.ts, runtsx report.ts(or transpile to JS andnode report.js)
Expected output:
--- High-Priority Task Report ---
URGENT: Fix critical bug
URGENT: Refactor the core engine
---------------------------------
Conclusion
Congratulations! You’ve built a complete Lispex program that does something useful. You learned how to structure Lispex code, define data and functions, and use a powerful filter/map pattern to transform data into a user-facing report. Use the transformer to target any runtime you like.
From here, try modifying the program:
- Change the priority threshold in the
is-high-priority?function. - Alter the report format in the
lambdafunction passed tomap. - Add more tasks to the initial
taskslist.