1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-16 08:40:19 +02:00

Added the new `examples' directory to the distribution.

This commit is contained in:
Martin Grabmüller 2001-05-31 15:54:25 +00:00
parent 413a1367e2
commit 2de7ddb766
28 changed files with 1075 additions and 1 deletions

View file

@ -0,0 +1,22 @@
## Process this file with Automake to create Makefile.in
##
## Copyright (C) 2001 Free Software Foundation, Inc.
##
## This file is part of GUILE.
##
## GUILE is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2, or
## (at your option) any later version.
##
## GUILE is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public
## License along with GUILE; see the file COPYING. If not, write
## to the Free Software Foundation, Inc., 59 Temple Place, Suite
## 330, Boston, MA 02111-1307 USA
EXTRA_DIST = README simple-hello.scm hello fact

33
examples/scripts/README Normal file
View file

@ -0,0 +1,33 @@
-*- text -*-
This directory includes examples which show how to write scripts using
Guile.
The descriptions below assume that you have a working copy of Guile
installed and available with the standard installation prefix
`/usr/local'.
simple-hello.scm:
The simplest "Hello World!" program for Guile. Run it like this:
$ guile -s simple-hello.scm
hello:
An advanced version of the script above, with command line handling
for the important options --help and --version. Run it like this:
./hello
or
guile -s hello
fact:
Command-line factorial calculator. Run it like this:
./fact 5
to calculate the factorial of 5.

70
examples/scripts/fact Executable file
View file

@ -0,0 +1,70 @@
#! /usr/local/bin/guile -s
!#
;;; Commentary:
;;; This is a command-line factorial calculator. Run like this:
;;;
;;; ./fact 5
;;;
;;; to calculate the factorial of 5
;;; Author: Martin Grabmueller
;;; Date: 2001-05-29
;;; Code:
(use-modules (ice-9 getopt-long))
;; This is the grammar for the command line synopsis we expect.
;;
(define command-synopsis
'((version (single-char #\v) (value #f))
(help (single-char #\h) (value #f))))
;; Display version information and exit.
;;
(define (display-version)
(display "fact 0.0.1\n"))
;; Display the usage help message and exit.
;;
(define (display-help)
(display "Usage: fact [options...] number\n")
(display " --help, -h Show this usage information\n")
(display " --version, -v Show version information\n"))
;; Interpret options, if --help or --version was given, print out the
;; requested information and exit. Otherwise, calculate the factorial
;; of the argument.
;;
(define (main options)
(let ((help-wanted (option-ref options 'help #f))
(version-wanted (option-ref options 'version #f))
(args (option-ref options '() '())))
(cond
((or version-wanted help-wanted)
(if version-wanted
(display-version))
(if help-wanted
(display-help)))
((not (= (length args) 1))
(display-help))
(else
(display (fact (string->number (car args))))
(newline)))))
;; Calculate the factorial of n.
;;
(define (fact n)
(if (< n 2)
1
(* n (fact (- n 1)))))
;; Call the main program with parsed command line options.
;;
(main (getopt-long (command-line) command-synopsis))
;; Local variables:
;; mode: scheme
;; End:

58
examples/scripts/hello Executable file
View file

@ -0,0 +1,58 @@
#! /usr/local/bin/guile -s
!#
;;; Commentary:
;;; This is the famous Hello-World-program, written for Guile. It is a
;;; little bit enhanced in that it understands the command line options
;;; `--help' (-h) and `--version' (-v), which print a short usage
;;; decription or version information, respectively.
;;; Author: Martin Grabmueller
;;; Date: 2001-05-29
;;; Code:
(use-modules (ice-9 getopt-long))
;; This is the grammar for the command line synopsis we expect.
;;
(define command-synopsis
'((version (single-char #\v) (value #f))
(help (single-char #\h) (value #f))))
;; Display version information and exit.
;;
(define (display-version)
(display "hello 0.0.1\n"))
;; Display the usage help message and exit.
;;
(define (display-help)
(display "Usage: hello [options...]\n")
(display " --help, -h Show this usage information\n")
(display " --version, -v Show version information\n"))
;; Interpret options, if --help or --version was given, print out the
;; requested information and exit. Otherwise, print the famous
;; message.
;;
(define (main options)
(let ((help-wanted (option-ref options 'help #f))
(version-wanted (option-ref options 'version #f)))
(if (or version-wanted help-wanted)
(begin
(if version-wanted
(display-version))
(if help-wanted
(display-help)))
(begin
(display "Hello, World!") (newline)))))
;; Call the main program with parsed command line options.
;;
(main (getopt-long (command-line) command-synopsis))
;; Local variables:
;; mode: scheme
;; End:

View file

@ -0,0 +1,14 @@
;;; Commentary:
;;; This is the famous Hello-World-program, written for Guile.
;;;
;;; For an advanced version, see the script `hello' in the same
;;; directory.
;;; Author: Martin Grabmueller
;;; Date: 2001-05-29
;;; Code:
(display "Hello, World!")
(newline)