;; Hello World! in LispyScript. (console.log "Hello LispyScript!") ;; A more intricate Hello World! (if (undefined? window) (console.log "Hello LispyScript!") (alert "Hello LispyScript!")) ; Functions ;; An anonymous function in LispyScript (function (x) (* x x)) ;; The first element in an expression can be an anonymous function. ((function (x) (* x x)) 2) ;; You can set a variable name to a function. (var square (function (x) (* x x))) (console.log (square 10)) ; LispyScript is Javascript! (Array.prototype.forEach.call [1, 2, 3] (function (elem index list) (console.log elem))) ;; You can access object methods and properties using the "." notation. (console.log (.greet {greet: "hello"})) ;; You can also use the 'get' expression to access a property of an object. (console.log (get "greet" {greet: "hello"})) (console.log (get 1 [1, 2, 3])) ;; You can 'set' variables too. (set window.onload (function () (alert "Page Loaded"))) ; Node ;; The node server example in LispyScript. (var http (require "http")) (var server (http.createServer (function (request response) (response.writeHead 200 {'Content-Type': 'text/plain'}) (response.end "Hello World\n")))) (server.listen 1337 "127.0.0.1") (console.log "Server running at http://127.0.0.1:1337/") ; Macros ;; You can define a macro. (macro array? (obj) (= (toString.call ~obj) "[object Array]")) ;; Now let us create a Lisp like 'let' macro in LispyScript. (macro let (names vals rest...) ((function ~names ~rest...) ~@vals)) (let (name email tel) ("John" "john@example.org" "555-555-5555") (console.log name) (console.log email) (console.log tel)) ;; Conditions (if (= document.readyState "complete") (console.log "loaded") ;; true expression (console.log "loading")) ;; optional false expression ;; Do expression (if (= process.argv.length 2) (do (process.stdin.resume) (process.stdin.setEncoding "utf8") (compile process.stdin process.stdout (process.cwd)))) ;; Each macro (each [1, 2, 3] (function (elem index list) (console.log elem))) ;; Exception handling (var fs (require 'fs')) (var outfile "text.txt") (try (fs.writeFileSync outfile "Hello World") (function (e) (console.log (+ "Cannot write file " outfile) (process.exit 1))))