13.07.2015 Views

Getting started with Imalab and Scheme - PRIMA

Getting started with Imalab and Scheme - PRIMA

Getting started with Imalab and Scheme - PRIMA

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(read port) returns next object parsable from the given input port port.returned.If end of file is reached, eof is(read-char port) returns next character, updating port to point to the next character.(peek-char port) returns next character, <strong>with</strong>out updating port.(eof-object?obj) tests if obj is a end-of-file object.(define out (open-output-file "filename.txt"))(display (+ 3 4) out)(newline out)(close-output-port out)The first line opens an output port <strong>with</strong> name filename.txt. The file is created if it does not exist. The result of(+ 3 4) is written to the file followed by a newline. The port is closed.4.4 Useful macros(for (i from to step) (begin body )) runs a for loop from to "!# <strong>with</strong> increment step.4.5 Defining new scheme functions(define (count param1)(for (i 1 (+ param1 1))(display-l i " n")))Here we define a scheme function <strong>with</strong> name count <strong>with</strong> one argument that displays on the shell the numbers from0 to param1 separated by a newline. begin starts the body of the function. for is a macro (see useful macros).display-l displays the printable version of objects to the shell.(count 100) This is the call of the newly defined function. It displays the numbers up to 100.(define (sumupto param1)(let ((sum 0))(for (i 0 (+ param1 1))(set! sum (+ sum i)))sum))Here we define a scheme function that sums all integers up to param1. let defines <strong>and</strong> initialises local variables.set! assigns new values to a variable. A scheme function always returns the value of its last statement. Writing thelocal variable name in the last line, means that its value is returned by the function. After the comm<strong>and</strong> (definesum (sumupto 10)) sum holds 55. If the last line is omitted, the call (sumupto 10) returns nothing.5 Functions written in C++5.1 Help functions(help-classes) displays all loaded classes. To access a particular class, you type (help num) <strong>with</strong> numreferring to the class that you are interested in. If the class that you are interested in is not in the list, you need to loadthe module <strong>with</strong> a (require ’modMissingModule).(help num) displays information about the item number num in the previously displayed list.If (help num) gives you a default message currently defined as function of 0 or more6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!