Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 2.87 KB

2023-04-12--english--electric-clojure.org

File metadata and controls

115 lines (81 loc) · 2.87 KB

Clever Compilers III: Electric Clojure

Previously: Clever Compilers I-II

  • Svelte
    • Developer enjoys convenience
    • Compiler abstracts away reactivity, noted inside $-labels
  • Rust
    • Developer enjoys memory safety
    • Compiler enforces of ownership
  • Idris
    • Developer enjoys application correctness
    • Compiler checks types, based on an expressive type system

Lisp

  • Second-oldest programming language in use, from 1958, by John McCarthy
  • Today: Scheme, Common Lisp, Clojure, Emacs-Lisp

Symbolic Expressions

Note function application in paranthesed polish/prefix notation.

(+ 1 2)      ;; 3
(list 1 2 3) ;; (1 2 3)

(def my-one 1)
(defn add1 [x] (+ 1 x))

Quotes

Quote an expressions (with quote or ') to stop its evaluation.

(quote +)          ;; +
'+                 ;; +
(list '+ 1 2)      ;; (+ 1 2)
(list 'list 1 2)   ;; (list 1 2)
'(list 1 2)        ;; (list 1 2)
'(list (+ 1 2) 3)  ;; (list (+ 1 2) 3)

;; an expressions that evaluates to its own quotation:
(
  (fn [x] (list x (list (quote quote) x)))
  (quote (fn [x] (list x (list (quote quote) x)))))

Syntax-Quotes

Syntax-quote an expression (with `) to allow explicitely marked parts of it (prefixed with ~~~) to be evaluated.

'(list  (+ 1 2) 3) ;; (list (+ 1 2) 3)
`(list ~(+ 1 2) 3) ;; (list 3 3)

Evaluation

Evaluate (with eval) a quoted expression.

(eval '(+ 1 2 3))      ;; 6
(eval (list '+ 1 2 3)) ;; 6

Macros

Write simple but powerful macros (with defmacro) that will be expanded at compile-time.

(defmacro apply-thrice [f x] (quote (f x x x)))
(apply-thrice * 5) ;; evaluates to: 125

(defmacro definiere [f] `(defn ~f [x] (+ 1 x)))
(definiere plus1)
(definiere plusEins)
(plus1 5)
function definiere(funktionsName) {
    function ${funktionsName}(x) {
        return x + 1;
    }
}

definiere("plus1");
definiere("plusEins");

plus1(3);

Clojure(Script)

ClojureA Lisp for the Java Virtual Machine (JVM)
ClojureScriptClojure transpiled to JavaScript

Introduction

  • An experimental web-framework for Clojure(Script), featuring
    • reactivity, including reactive loops
    • multi-tier: macros for marking client- or server-side, including ability to arbitrarily nest
  • Developer writes single source code, wherein server- and client-side code can be arbitrarily nested
  • Compiler compiles/transpiles source code into server- and client-side code, including the networking in-between

Further Reading