Language Oriented Programming: Building a #lang | Schema Programming Part 15
Racket's manifesto is Language Oriented Programming (LOP). The idea is: don't just solve the problem; design a language that makes the problem easy to solve.
Advertisement
What is a #lang?
When you write #lang racket, you are telling the Racket system to use the "racket" reader and expander. You can make your own!
A Simple Language: #lang stacker
Let's imagine a language that just pushes numbers onto a stack.
stacker.rkt (The implementation)
#lang racket
(provide (all-defined-out))
(define stack empty)
(define (push x)
(set! stack (cons x stack)))
(define (pop)
(define top (first stack))
(set! stack (rest stack))
top)
; Reader hook
(module reader syntax/module-reader
stacker)
my-program.rkt (Using the language)
#lang stacker
(push 10)
(push 20)
Use syntax/module-reader to define how your language parses text. You can define entirely new syntaxes (like Python-style indentation or datalog logic) that compile down to Racket macros.
The Power of LOP
People have built:
- Scribble: A language for documentation (which these docs are often written in).
- Video: A language for video editing.
- Typed Racket: A statically typed dialect.
Advertisement
Summary
Building a language in Racket isn't a PhD thesis project; it's a weekend afternoon project. This capability makes Racket a unique tool in the programmer's arsenal.
What is the philosophy behind Language Oriented Programming?
Md Nasim Sheikh
Software Developer at softexForge