Guile and Scheme links

From AbInitio

(Difference between revisions)
Jump to: navigation, search
Revision as of 23:51, 15 October 2005 (edit)
Stevenj (Talk | contribs)
(Guile:)
← Previous diff
Revision as of 19:31, 17 October 2005 (edit)
Stevenj (Talk | contribs)

Next diff →
Line 24: Line 24:
* The [http://www.glug.org/ GLUG] (Guile Lovers Use Guile) Guile user's site. * The [http://www.glug.org/ GLUG] (Guile Lovers Use Guile) Guile user's site.
* See parts IV and V of the [http://www.gnu.org/software/guile/docs/guile-ref/ Guile Reference Manual] for additional Scheme functions and types defined within the Guile environment. * See parts IV and V of the [http://www.gnu.org/software/guile/docs/guile-ref/ Guile Reference Manual] for additional Scheme functions and types defined within the Guile environment.
 +
 +== How to write a loop in Scheme ==
 +
 +The most frequently asked question seems to be: how do I write a loop in Scheme? We give a few answers to that here, supposing that we want to vary a parameter ''x'' from ''a'' to ''b'' in steps of ''dx'', and do something for each value of ''x''.
 +
 +The classic way, in Scheme, is to write a [[w:Tail recursion|tail-recursive]] function:
 +
 + (define (doit x x-max dx)
 + (if (<= x x-max)
 + (begin
 + ''...perform loop body with x...''
 + (doit (+ x dx) x-max))))
 +
 + (doit a b dx) ; execute loop from a to b in steps of dx
 +
 +There is also a [http://www.swiss.ai.mit.edu/ftpdir/scheme-reports/r5rs-html/r5rs_6.html#SEC36 do-loop construct] in Scheme that you can use
 +
 + (do ((x a (+ x dx))) ((> x b)) ''...perform loop body with x...'')
 +
 +If you have a list of values of ''x'' that you want to loop over, then you can use <code>map</code>:
 +
 + (map (lambda (x) ''...do stuff with x...'') ''list-of-x-values'')
 +
 +For example, [[libctl]] has a couple of built-in functions <code>arith-sequence</code> and <code>interpolate</code> (see the [[libctl User Reference|user reference]]) to construct lists of a regular sequence of values:
 +
 + (map (lambda (x) ''...do stuff with x...'')
 + (arith-sequence x-min dx num-x))
 +
 +or
 +
 + (map (lambda (x) ''...do stuff with x...'')
 + (interpolate num-x (list a b)))
 +
 +Finally, if you have an entire libctl list that you want to vary over some parameter ''x'', you can do so by writing a loop on the Unix command-line. Using the [[w:GNU Bash|bash]] shell, you could do:
 +
 + for x in `seq ''a dx b''`; do ''program'' x=$x ''myfile.ctl''; done
[[Category:libctl]] [[Category:libctl]]

Revision as of 19:31, 17 October 2005

libctl
Manual: Introduction
Basic User Experience
Advanced User Experience
User Reference
Developer Experience
Guile and Scheme links
License and Copyright

There are many places you can go to on the Web to find out more regarding Guile and the Scheme programming language. We list a few of them here:

Scheme:

Scheme is a simplified derivative of Lisp, and is a small and beautiful dynamically typed, lexically scoped, [[w:Functional programming language|functional] language.

Guile:

Guile is a free implementation of Scheme, designed to be plugged in to other programs as a scripting language.

  • The home site for the GNU Guile project.
  • The GLUG (Guile Lovers Use Guile) Guile user's site.
  • See parts IV and V of the Guile Reference Manual for additional Scheme functions and types defined within the Guile environment.

How to write a loop in Scheme

The most frequently asked question seems to be: how do I write a loop in Scheme? We give a few answers to that here, supposing that we want to vary a parameter x from a to b in steps of dx, and do something for each value of x.

The classic way, in Scheme, is to write a tail-recursive function:

(define (doit x x-max dx)
   (if (<= x x-max)
      (begin
         ...perform loop body with x...
         (doit (+ x dx) x-max))))

(doit a b dx) ; execute loop from a to b in steps of dx

There is also a do-loop construct in Scheme that you can use

(do ((x a (+ x dx))) ((> x b)) ...perform loop body with x...)

If you have a list of values of x that you want to loop over, then you can use map:

(map (lambda (x) ...do stuff with x...) list-of-x-values)

For example, libctl has a couple of built-in functions arith-sequence and interpolate (see the user reference) to construct lists of a regular sequence of values:

(map (lambda (x) ...do stuff with x...)
     (arith-sequence x-min dx num-x))

or

(map (lambda (x) ...do stuff with x...)
     (interpolate num-x (list a b)))

Finally, if you have an entire libctl list that you want to vary over some parameter x, you can do so by writing a loop on the Unix command-line. Using the bash shell, you could do:

for x in `seq a dx b`; do program x=$x myfile.ctl; done
Personal tools