Go to the first, previous, next, last section, table of contents.

How to start up guile - a quick tour

Invoking guile and ggl

[This section is taken almost entirely from the SCM manual (see section `Invoking SCM' in SCM).]

Here is the synopsis for guile (ggl has exactly the same options):

guile  [-a kbytes] [-ibvqmu] [-p number]
       [-c expression] [-e expression] [-f filename] [-l filename]
       [-r feature] [- | -- | -s] [filename] [arguments ...]

Upon startup guile loads the file specified by by the environment variable SCM_INIT_PATH or by the parameter IMPLINIT in the makefile (or `scmfig.h') if SCM_INIT_PATH is not defined. The makefiles attempt to set IMPLINIT to `Ginit.scm' in the source directory.

Unless the option -no-init-file occurs in the command line, `Ginit.scm' checks to see if there is file `ScmInit.scm' in the path specified by the environment variable HOME (or in the current directory if HOME is undefined). If it finds such a file it is loaded.

`Ginit.scm' then looks for command input from one of three sources: From an option on the command line, from a file named on the command line, or from standard input.

Scheme-code files can also invoke guile or ggl (see section `Syntax Extensions' in SCM).

-o kbytes
specifies that guile should allocate an initial heapsize of kbytes. This option, if present, must be the first on the command line.
-no-init-file
inhibits the loading of `ScmInit.scm' as described above.
-e expression
-c expression
specifies that the scheme expression expression is to be evaluated. These options are inspired by perl and sh respectively. On Amiga systems the entire option and argument need to be enclosed in quotes. For instance `"-e(newline)"'.
-r feature
requires feature. This will load a file from [SLIB] if that feature is not already supported. If feature is 2, 3, 4, or 5 guile will require the features neccessary to support [R2RS], [R3RS], [R4RS], or proposed [R5RS], respectively.
-l filename
-f filename
loads filename. guile will load the first (unoptioned) file named on the command line if no -c, -e, -f, -l, or -s option preceeds it.
-p level
sets the prolixity (verboseness) to level. This is the same as the guile command (verobse level).
-v
(verbose mode) specifies that guile will print prompts, evaluation times, notice of loading files, and garbage collection statistics. This is the same as -p3.
-q
(quiet mode) specifies that guile will print no extra information. This is the same as -p0.
-m
specifies that subsequent loads, evaluations, and user interactions will be with [R4RS] macro capability. To use a specific [R4RS] macro implementation from [SLIB] (instead of [SLIB]'s default) put -r macropackage before -m on the command line.
-u
specifies that subsequent loads, evaluations, and user interactions will be without [R4RS] macro capability. [R4RS] macro capability can be restored by a subsequent -m on the command line or from Scheme code.
-i
specifies that guile should run interactively. That means that guile will not terminate until the (quit) or (exit) command is given, even if there are errors. It also sets the prolixity level to 2 if it is less than 2. This will print prompts, evaluation times, and notice of loading files. The prolixity level can be set by subsequent options. If guile is started from a tty, it will assume that it should be interactive unless given a subsequent -b option.
-b
specifies that guile should run non-interactively. That means that guile will terminate after processing the command line or if there are errors.
-s
specifies, by analogy with sh, that further options are to be treated as program aguments.
-
--
specifies that there are no more options on the command line.

Guile as a basic scheme interpreter

You could start by running the actual guile program. In a most basic sense, guile is a souped-up version of Aubrey Jaffer's scheme interpreter SCM.

If the binary directory into which you installed guile is in your path, you should be able to just type guile. Here is an example session:

<shell-prompt> guile
;;; [lots of output]
guile> (+ 20 35)
55
;Evaluation took 0 mSec (0 in scm_gc) 65 cells work, 44 bytes other
guile> (define (recursive-factorial n)
         (if (= n 0)
             1
            (* n (recursive-factorial (- n 1)))))
recursive-factorial
;Evaluation took 0 mSec (0 in scm_gc) 191 cells work, 87 bytes other
guile> (recursive-factorial 5)
120
;Evaluation took 0 mSec (0 in scm_gc) 229 cells work, 42 bytes other
guile> (recursive-factorial 500)
1220136825991110068701238785423046926253574342803192842192413588
3858453731538819976054964475022032818630136164771482035841633787
2207817720048078520515932928547790757193933060377296085908627042
9174547882424912726344305670173270769461062802310452644218878789
4657547771498634943677810376442740338273653974713864778784954384
8959553753799042324106127132698432774571554630997720278101456108
1188373709531016356324432987029563896628911658974769572087926928
8712817800702651745077684107196243903943225364226052349458501299
1857150124870696156814162535905669342381300885624924689156412677
5654481886506593847951775360894005745238940335798476363944905313
0623237490664450488246650759467358620746379251842004593696929810
2226397195259719094521782333175693458150855233282076282002340262
6907898342451712006207714640979456116127629145951237229913340169
5523638509428855920187274337951730145863575708283557801587354327
6888868012039988238470215146760544540766353598417443048012893831
3896881639487469658817504506926365338175055478128640000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000
;Evaluation took 140 mSec (30 in scm_gc) 2042 cells work, 110102 bytes other
guile> (quit)
;;; QUIT executed, repl exiting
;Evaluation took 0 mSec (0 in scm_gc) 55 cells work, 40 bytes other
<shell-prompt>

In this last example we did some simple arithmetic (+ 20 35) and got the answer 55. Then we coded the classic (and rather wasteful) factorial algorithm, and got a glimpse of scheme's nice bignumbers by asking for the factorial of 1000. Then we quit with (quit).

Using Tk widgets

Here are some small programs that use the Tk toolkit to do graphics. More is said about this in section More on the Guile interface to Tk. You might also want to look at how the Tk interface is implemented in STk ([STK]).

Start with a trivial program (located in `examples/tk/quit-button.scm' that brings up a button that destroys itself when clicked:

#!/packages/bin/guile -qb
; -*-scheme-*-

;; a simple first program that uses Tk from scheme; puts up
;; a simple quit button.

(require 'Gwish)
(use-library tcl)
(use-interface tcl)
(use-interface tclhack)

;; prepare the callback for the button
(define (quit-callback)
  (begin
    (display "quitting now\n")
    (destroy ".")))

;; create the button widget
(button '.quit :text "quit" :command (tcl-lambda() (quit-callback)))
(pack .quit)
(tk-main-loop)
(quit)

Notice that in this one-button program, the quit-callback procedure could have been put on the creation line for that widget:

(button '.quit :text "quit" :command
  (tcl-lambda()
    (begin
      (display "quitting now\n")
      (destroy "."))))

Now we try out menus (you can find this example in `examples/tk/short-menu.scm'):

#!/packages/bin/guile -qb
; -*-scheme-*-

(require 'Gwish)
(use-library tcl)
(use-interface tcl)
(use-interface tclhack)

;; create the menubutton, and when the user clicks, invoke the menu .mb.m
(menubutton '.mb :text "sample menu" :menu ".mb.m")
(pack '.mb)

;; create the actual drop-down menu
(menu '.mb.m)
(.mb.m 'add 'command :label "Open" :command (tcl-lambda() (display "Open\n")))
(.mb.m 'add 'command :label "Close" :command (tcl-lambda()
							(display "Close\n")))
(.mb.m 'add 'separator)
(.mb.m 'add 'command :label "Exit" :command (tcl-lambda()
						       (begin
							 (display "Exit\n")
							 (destroy "."))))

(tk-main-loop)
(quit)

Notice that the Exit menu option will actually work.

The next example shows how to read the mouse in Tk (`examples/tk/track-mouse.scm').

#!/packages/bin/guile -
; -*-scheme-*-

(require 'Gwish)
(use-library tcl)
(use-interface tcl)
(use-interface tclhack)

(canvas '.c)
(pack .c :fill "both" :expand "yes")

(bind .c "<B1-Motion>" (tcl-lambda ("%x %y" (number x) (number y))
                         (.c 'create 'rectangle x y x y :width 5)))

(tk-main-loop)

Applets in Guile

More advanced and interesting examples of all these uses of Guile are given in other chapters; the last example I want to show you here involves World Wide Web browsers and applets written in scheme.

You might have noticed that one of the programs created when you installed scheme is called SurfIt! (invoked by surfit, without the !). SurfIt! is a full-featured (although in early release) WWW brouser developed by Steve Ball at Australian National University. It is capable of executing applets written in Tcl, much like Sun's Hot Java broser and Netscape Navigator can execute applets written in Java, and Grail executes Python applets.

The version of SurfIt! shipped with Guile has been extended to allow execution of Guile applets. To see a simple example, do three things:

Congratulations! You have just written your first applet, and it has been executed by a browser. [This is also the author's first applet; he is also excited.]

Notice how the applet uses Tk widgets that are shown directly on your own display.

The files mentioned above are stored in `examples/applets'.


Go to the first, previous, next, last section, table of contents.