HOME | 3. Making Lists | Yet Another Scheme Tutorial | 5. Branching | Post Messages |
As it is quite inconvenient to define functions on the front end, you usually edit codes by an editor and load them to the interpreter.
Let's edit the code shown in [code 1] by any kinds of editor (including Notepad), and save it as 'hello.scm' under a directory like 'C:\doc\scheme\'. If possible, save under the default directory of MIT-Scheme, which you have defined in chapter 1.
[code 1] (hello.scm)
; Hello world as a variable (define vhello "Hello world") ;1 ; Hello world as a function (define fhello (lambda () ;2 "Hello world"))Next, give the following command to the Scheme interpreter.
(cd "C:\\doc\\scheme") ;Value 14: #[pathname 14 "c:\\doc\\scheme\\"] (load "hello.scm") ;Loading "hello.scm" -- done ;Value: fhelloBy this, hello.scm is loaded to the scheme interpreter. If the current directory of the interpreter has been set to the directory where the script is, you don't need the first line. Then give the following command to the interpreter.
vhello ;Value 15: "Hello world" fhello ;Value 16: #[compound-procedure 16 fhello] (fhello) ;Value 17: "Hello world"The define is a operator to declare variables and takes two arguments. The operator declares to use the first argument as a global parameter and binds it to the second argument. Thus, the line at ;1 in [code 1] means that it declares a global parameter vhello and binds it to "Hello world".
On the other hand, the line at ;2 declares
a procedure that returns "Hello world".
The lambda is a special form to define procedures.
The lambda takes more than one arguments and the first argument is the list of
parameters that the procedure takes as arguments.
As fhello takes no argument, the list of the parameters is empty.
Give vhello to the interpreter then it returns the value, "Hello world".
if you give fhello, it also returns the value like,
#[compound-procedure 16 fhello],
which means that the Scheme interpreter treats procedures and conventional data type in a same way.
As explained in the previous chapter, the Scheme interpreter manipulates all
the data by their addresses in the memory space and any kinds of object existing in the memory can be
treated in the same way.
To call fhello as a procedure, you should bracket off the symbol like (fhello).
Then the interpreter evaluates it and returns "Hello world"
taking steps shown in chapter 2.
[code 2] (farg.scm)
; hello with name (define hello (lambda (name) (string-append "Hello " name "!"))) ; sum of three numbers (define sum3 (lambda (a b c) (+ a b c)))After saving it, load the file into the interpreter and call the functions.
(load "farg.scm") ;Loading "farg.scm" -- done ;Value: sum3 (hello "Lucy") ;Value 20: "Hello Lucy!" (sum3 10 20 30) ;Value: 60
[code 3]
; hello with name (define (hello name) (string-append "Hello " name "!")) ; sum of three numbers (define (sum3 a b c) (+ a b c))In this form, functions are defined in the form that they are called. [code 2] and [code 3] are identical. Some people don't like the short way of defining functions, but I use it in this tutorials, because this way makes code shorter.
You will find an executable named runemacs.exe under directory bin. Double click it to start the editor. Even the key mapping is quite different from that of Windows standard, it is reasonably user friendly because it is equipped with a menu bar and mouse control. You can customize it by editing the configuration file named .emacs. A Scheme mode is available, which understands pre-defined words and indents code properly by pressing Ctrl-i or TAB. In addition, the editor shows corresponding open parenthesis automatically, when a close parenthesis are typed.
On Windows operation system, emacs cannot call MIT-Scheme interactively. You should save the source code and load it by hand. On Unix and Linux, on the other hand, emacs can call MIT-Scheme interactively and editing codes can be done interactively.
Press (Ctrl-x, Ctrl-s) to save files and press (Ctrl-x, Ctrl-w) to save files with different names. To exit from editor press (Ctrl-x, Ctrl-c).
In the next chapter, I will mention about branching.
; 1 (define (1+ x) (+ x 1)) ; 2 (define (1- x) (- x 1))
; definition of pi (define pi (* 4 (atan 1.0))) ; degree -> radian (define (radian deg) (* deg (/ pi 180.0))) ; free fall time (define (ff-time vy) (/ (* 2.0 vy) 9.8)) ; horizontal distance (define (dx vx t) (* vx t)) ; distance (define (distance v ang) (dx (* v (cos (radian ang))) ; vx (ff-time (* v (sin (radian ang)))))) ; tAfter loading to the interpreter, the distance will be calculated by;
(distance 40 30)
;Value: 141.39190265868385
The function returns a reasonable value 141.4 m, which is slightly larger because of
ignoring air drag.
HOME | 3. Making Lists | Yet Another Scheme Tutorial | 5. Branching | Post Messages |