Guile & guile-scsh

Chaos <chaos@shlug.org>
License: CC BY

GNU Guile


GNU Ubiquitous Intelligent Language for Extensions

Usually Scheme R5RS, partially R6RS

Originally developed as a glue language

But with Strong Feature and actively developed

Why Scheme


Minimalism/ Expressive

Simple, clean and easy to implement.

R5RS has only 23 s-exps.

First Class Functions

  (define (my-apply 
  	f x1 x2)
  	 (f x1 x2))
  
  (my-apply + 1 2)
  ;; 3
  (my-apply (lambda (x y) (+ x (* 2 y))) 1 2)
  ;; 5

Shared namespace bewteen procedures and variables

Different with common-lisp.

Lexical Scope


Unlike emacs lisp and ancient lisp machines, Scheme is lexicaly scoped.

  (define (my-complex-construct x y)
          (let ((r x) (i y))
               (lambda (m) (if (eq? m 'r) r i))))
  
  (define c (my-complex-construct 1 3))
  (c 'r)
  ;; 1
  (c 'i)
  ;; 3

Macros and Hygienic Macros


easily to extend

  (define-syntax let
    (syntax-rules ()
      ((let ((var expr) ...) body ...)
        ((lambda (var ...) body ...) expr ...))))

* Codes from wikipedia

Proper tail recursion


Tail Call Optimization

  (define (fact n)
    (define (iter n x)
      (cond ((= n 0) x)
            (#t (iter (- n 1) (* n x)))))
    (iter n 1)
  )
  (fact 4)
  ;; 24
  (fact 100000)
  ;; Output is intentially omitted

First Class Continuations


Contination is a snapshot of the current state of running process, call/cc will let process return to a saved snapshot.

From Wikipedia:

First-class continuations enable the programmer to create non-local control constructs such as iterators, coroutines, and backtracking.

call/cc

  (let* ((yin
           ((lambda (cc) (display "@") cc) (call-with-current-continuation (lambda (c) c))))
         (yang
           ((lambda (cc) (display "*") cc) (call-with-current-continuation (lambda (c) c)))))
      (yin yang))

Delimited Continuations

More easier to use and control. (Full-continuation considered harmful)

Guile Features


  • SLIB::
  • POSIX::
  • Web::
  • SRFI Support::
  • R6RS Support::
  • Pattern Matching::
  • Readline Support::
  • Pretty Printing::
  • File Tree Walk::
  • Queues::
  • Streams::
  • Expect::
  • sxml-match::
  • Curried Definitions::
  • Statprof::
  • SXML::

Not only Scheme!


Guile compiler tower

Layered infrastucture , from high language to middle layer then virtual machine with JITs(in-development).

10x faster promised in 2.2 release

Emacs Lisp

elisp compiled using Guile.

And theres is someone in doing [http://www.emacswiki.org/emacs/GuileEmacs].

BrainF***

Python/Lua...

And More


Guile-XCB

[https://github.com/mwitmer/guile-xcb]

X11 protocal compiled using Guile!

Guile-WM

Window Manager! [https://github.com/mwitmer/guile-wm]

Other resources


Web Frame work

A very lightweight and fastest web-framework of Scheme language:

[https://github.com/NalaGinrut/artanis]

Pure Functional Data Structures

[https://github.com/ijp/pfds/]

OO

Things using Guile


From WikiPedia:

guile-scsh


In the beginning, there is only scsh

Scheme Shell, Unix Shell using s-exps and scheme language. [http://scsh.net/]

scsh ported to guile

reborn to guile-2.x

[https://gitorious.org/guile-scsh]

BSD-license.

scsh details


Run Process

  scheme@(guile-user)> (run (ls))
  COPYING  docs  examples  INSTALL  install.scm  pkg-list.scm  scsh  temp.scm  test 
  test-suite  TODO  USAGE
  $2 = 0

Pipe

  scheme@(guile-user)> (run (| (ls) (grep COPY)))
  COPYING
  $1 = 0
  scheme@(guile-user)> 

running process in background, with pipe

  scheme@(guile-user)> (& (| (ls) (grep COP)))
  $4 = #{proc 3084}
  scheme@(guile-user)> COPYING

More Examples


quasi-quote

  scheme@(guile-user)> (define ls '(ls --color=auto))
  scheme@(guile-user)> (run ,ls)
  COPYING  guile-scsh.scm  scsh  test  USAGE
  $3 = 0

input/output

  scheme@(guile-user)> (run (wc -l ) (< /etc/issue))
  2
  $2 = 0
  scheme@(guile-user)> (run (cat ) (< /etc/issue))                                                                                                               
  Arch Linux \r (\l)
  
  $3 = 0
  scheme@(guile-user)> (run (grep Arch ,@(glob /etc/*)) (> 2 /dev/null))
  /etc/issue:Arch Linux \r (\l)
  ...
  /etc/os-release:PRETTY_NAME="Arch Linux"
  $4 = 512
  scheme@(guile-user)> 

and more


temp-files

  scheme@(guile-user)> (call/temp-file (lambda () (run (ls )
   (= 1 (current-output-port)))) (lambda (x) (run (,@ls -l ,x))))
  -rw------- 1 chaos chaos 44 Mar  6 15:41 /tmp/5310.61038
  $12 = 0

globbing

  scheme@(guile-user)> (run (ls ,@(glob /bin/l*)))
  /bin/l2ping        /bin/ld.gold             /bin/lex    
  /bin/l2test        /bin/ld_iscsi.so         /bin/lexgrog
  /bin/labltk        /bin/ldns-chaos          /bin/lftp 

TODO