Skip to content

Getting Started

Jakub T. Jankiewicz edited this page May 1, 2019 · 27 revisions

Installation

You can use LIPS from unpkg

<script src="https://unpkg.com/@jcubic/lips"></script>

or download from npm:

npm install @jcubic/lips

You can also use webpack to bundle your dependencies together with LIPS and include that one bundle.

Running your first script

After you included the LIPS script into your page. You can put your LIPS code into script tag:

<script type="text/x-lips">
   (load "examples/helpers.lips") ;; helpers file have definition of --> macro
   (--> console (log "hello world!"))
<script>

You can also use src attribute to run your script:

<script type="text/x-lips" src="script.lips"></script>

If you use emacs you can put comment at the beginning

;; -*- scheme -*-

so you will have Scheme syntax highlighting.

Scheme

If you already know scheme here are the differences:

  • no big Standard library,
  • print instead of display,
  • no hygienic macros only pure lisp macros with define-macro syntax and quasi quotation,
  • no tail call eliminations,
  • no call/cc (continuations).

Defining functions

to define a function use define macro:

(define (square x)
  (* x x))

This is equivalent of:

(define square (lambda (x)
                 (* x x)))

and you will get this code if you run:

(macroexpand (define (square x)
                     (* x x)))

Some of the internal macros written in JavaScript are expanding like define.

Macros

To define macros use:

(define-macro (when x . body) `(if ,x (begin ,@body)))

this will create macro when that is like if but you can put more then one expression and it don't have else case.

To learn more about macros I suggest book let over lambda.

JavaScript

You can access JavaScript objects from LIPS, if object is global you can use it like normal variable:

(. document 'getElementById)
(. document "getElementById")

Those two calls will return function getElementById taken from document object. Dot is function that can be used to get the property of an object. You can't use it in every place since it's also special token in LIPS (to mark pair (foo . bar), if you want to use in different place, for instance as variable you can use get function that is alias for dot function).

When you get function property from object (method) the function is bound to that object so you can call it without any issues for example:

(define log (. console 'log))
(log "hello world")

jQuery

NOTE: In LIPS demo by default there is loaded jQuery library.

In examples/helper.lips file there is helper macro --> that you can use to interact better with jQuery, you can use chaining:

(--> ($ "body")
     (on "click" (lambda (e) (print "click event")))
     (find "ul")
     (append "<li>Hello</li>")
     (next)
     (css "color" "red"))

It's equivalent of:

$("body")
  .on("click", function(e) { console.log("click event"); })
  .find("ul")
  .append("<li>Hello</li>")
  .next()
  .css("color", "red");

in helpers.lips file there is also one more macro make-object that allow to create object from key like syntax, so you can use it to create JavaScript objects (e.g. to be used with css() jQuery function or any other JavaScript function):

(let ((css (. ($ ".terminal") 'css)))
  (css (make-object :color "red" :border "1px solid red")))

this function use built in function dot and make-object macro. You can shorten this code using:

(--> ($ ".terminal")
     (css (make-object :color "red" :border "1px solid blue")))
Clone this wiki locally