Guile and Scheme links
From AbInitio
Revision as of 19:59, 17 October 2005 (edit) Stevenj (Talk | contribs) (→How to write a loop in Scheme) ← Previous diff |
Current revision (17:56, 21 February 2017) (edit) Ardavan (Talk | contribs) (→How to write a loop in Scheme) |
||
Line 8: | Line 8: | ||
* A [[w:Scheme programming language|history and introduction to Scheme]], with links to more sites, at the [http://en.wikipedia.org/ Wikipedia] collaborative encyclopedia. | * A [[w:Scheme programming language|history and introduction to Scheme]], with links to more sites, at the [http://en.wikipedia.org/ Wikipedia] collaborative encyclopedia. | ||
* [http://www.swiss.ai.mit.edu/ftpdir/scheme-reports/r5rs-html/r5rs_toc.html R5RS] is the official Scheme language definition and reference. | * [http://www.swiss.ai.mit.edu/ftpdir/scheme-reports/r5rs-html/r5rs_toc.html R5RS] is the official Scheme language definition and reference. | ||
- | * A classic [http://www-2.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/scheme/doc/intro/intro.txt introduction] to Scheme by Ken Dickey. | + | * A classic [ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/intro.txt introduction] to Scheme by Ken Dickey. |
* [http://mitpress.mit.edu/sicp/sicp.html Structure and Interpretation of Computer Programs] by Abelson, Sussman, and Sussman (full text online). | * [http://mitpress.mit.edu/sicp/sicp.html Structure and Interpretation of Computer Programs] by Abelson, Sussman, and Sussman (full text online). | ||
- | * [http://www.cs.utexas.edu/users/wilson/schintro/schintro_toc.html Introduction to Scheme and its Implementation] (the complete book on-line) by [http://www.cs.utexas.edu/users/wilson/ Prof. Paul R. Wilson] ([http://www.cs.utexas.edu/ Univ. of Texas]). | + | * [ftp://ftp.cs.utexas.edu/pub/garbage/cs345/schintro-v14/schintro_toc.html Introduction to Scheme and its Implementation] (the complete book on-line) by Prof. Paul R. Wilson ([http://www.cs.utexas.edu/ Univ. of Texas]). |
+ | * [http://ds26gte.github.io/tyscheme/index.html Teach Yourself Scheme] is a nice tutorial-style introduction to Scheme programming. | ||
* The [http://www.swiss.ai.mit.edu/projects/scheme/index.html MIT Scheme Home Page] (where do you think Scheme was invented?) | * The [http://www.swiss.ai.mit.edu/projects/scheme/index.html MIT Scheme Home Page] (where do you think Scheme was invented?) | ||
** also check out the MIT [http://www.ai.mit.edu/projects/su/su.html Scheme Underground] | ** also check out the MIT [http://www.ai.mit.edu/projects/su/su.html Scheme Underground] | ||
Line 22: | Line 23: | ||
* The [http://www.gnu.org/software/guile/ home site] for the GNU Guile project. | * The [http://www.gnu.org/software/guile/ home site] for the GNU Guile project. | ||
- | * 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/manual/html_node/index.html 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 == | == 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 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: | The classic way, in Scheme, is to write a [[w:Tail recursion|tail-recursive]] function: | ||
Line 35: | Line 35: | ||
(begin | (begin | ||
''...perform loop body with x...'' | ''...perform loop body with x...'' | ||
- | (doit (+ x dx) x-max)))) | + | (doit (+ x dx) x-max dx)))) |
(doit a b dx) ; execute loop from a to b in steps of dx | (doit a b dx) ; execute loop from a to b in steps of dx | ||
Line 47: | Line 47: | ||
(map (lambda (x) ''...do stuff with x...'') ''list-of-x-values'') | (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: | + | == How to read in values from a text file in Scheme == |
+ | |||
+ | A simple command to read a text file and store its values within a variable in Scheme is <code>read</code>. As an example, suppose a file ''foo.dat'' contains the following text, including parentheses: | ||
+ | |||
+ | (1 3 12.2 | ||
+ | 14.5 16 18) | ||
+ | |||
+ | In Scheme, we would then use | ||
+ | |||
+ | (define port (open-input-file "foo.dat")) | ||
+ | (define foo (read port)) | ||
+ | (close-input-port port) | ||
+ | |||
+ | The variable ''foo'' would then be a list of numbers '(1 3 12.2 14.5 16 18). | ||
+ | |||
+ | ===Tricks specific to libctl-using programs such as [[MPB]] or [[Meep]]=== | ||
+ | |||
+ | [[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, which you can use in conjunction with <code>map</code> as above: | ||
(map (lambda (x) ''...do stuff with x...'') | (map (lambda (x) ''...do stuff with x...'') | ||
Line 57: | Line 74: | ||
(interpolate num-x (list a b))) | (interpolate num-x (list a b))) | ||
- | Finally, if you have an entire libctl list that you want to vary over some [[Libctl Advanced User Experience#Command-line parameters|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: | + | Finally, if you have an entire libctl input file ''myfile.ctl'' that you want to loop, varying over some [[Libctl Advanced User Experience#Command-line parameters|parameter]] ''x'', you can do so by writing a loop on the Unix command-line. Using the [[w:bash|bash]] shell, you could do: |
for x in `seq ''a dx b''`; do ''program'' x=$x ''myfile.ctl''; done | for x in `seq ''a dx b''`; do ''program'' x=$x ''myfile.ctl''; done | ||
[[Category:libctl]] | [[Category:libctl]] |
Current revision
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:
Contents |
Scheme:
Scheme is a simplified derivative of Lisp, and is a small and beautiful dynamically typed, lexically scoped, functional language.
- A history and introduction to Scheme, with links to more sites, at the Wikipedia collaborative encyclopedia.
- R5RS is the official Scheme language definition and reference.
- A classic introduction to Scheme by Ken Dickey.
- Structure and Interpretation of Computer Programs by Abelson, Sussman, and Sussman (full text online).
- Introduction to Scheme and its Implementation (the complete book on-line) by Prof. Paul R. Wilson (Univ. of Texas).
- Teach Yourself Scheme is a nice tutorial-style introduction to Scheme programming.
- The MIT Scheme Home Page (where do you think Scheme was invented?)
- also check out the MIT Scheme Underground
- There is the comp.lang.scheme newsgroup, and its FAQ.
- The Internet Scheme Repository has a lot of code and documentation.
- schemers.org is another Scheme site and collection of resources.
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.
- 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 dx)))) (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)
How to read in values from a text file in Scheme
A simple command to read a text file and store its values within a variable in Scheme is read
. As an example, suppose a file foo.dat contains the following text, including parentheses:
(1 3 12.2 14.5 16 18)
In Scheme, we would then use
(define port (open-input-file "foo.dat")) (define foo (read port)) (close-input-port port)
The variable foo would then be a list of numbers '(1 3 12.2 14.5 16 18).
Tricks specific to libctl-using programs such as MPB or Meep
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, which you can use in conjunction with map
as above:
(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 input file myfile.ctl that you want to loop, varying 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