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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

(stringify obj) converts object obj to a string. Possible objects are char, int, double, but also variables<strong>and</strong> functions that evaluate to char, int or double. stringify can be called <strong>with</strong> several parameters. Returnedis the concatenated string of all parameters.(define point (cons i j)) defines a pair (i .j).(pair?point) the type test typename? returns true.(car point) returns the car-field (the first field of the pair).(set-car! point 3) sets the car-field of point to 3.(cdr point) returns the cdr-field of point. This is the list of the last element.(set-cdr! point (list 5)) sets the cdr-field to 5.(cadr point) gets the 2nd element of point. This is equivalent to (car (cdr point)). Lists are alsopairs. You can use car to access the first element, cadr to access the second, ... , cadddr to access the fourthelement.(define clist (append alist blist)) appends the lists alist <strong>and</strong> blist <strong>and</strong> places the list inclist(list-ref alist num) returns element num of alist.(define filenames (list ))(for-each (lambda (prefix)(for-each (lambda (num)(set! filenames (append filenames (list (stringify prefix num ".ppm")))))(list 1 2 3 4)))(list "frontal" "side"))These comm<strong>and</strong>s produce following list ("frontal1.ppm" "frontal2.ppm" "frontal3.ppm""frontal4.ppm" "side1.ppm" "side2.ppm" "side3.ppm" "side4.ppm").4.3 I/O from/to filesI/O is done by ports. You can write or read characters from ports. A port can be connected to a file or the shell.(open-input-file filename) opens input file, returns input port(open-output-file filename) opens output file, returns output port, file is created if it does not exist.(close-input-port port) closes input port.(close-output-port port) closes output port.(display obj port) writes human readable version of object obj to port port.(write obj port) writes machine readable version of object obj to port port.(newline port) writes a newline to port.5

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

Saved successfully!

Ooh no, something went wrong!