Scheme

TinyScheme, uno Scheme piccolo (sottoinsieme di R5RS): prompt interattivo e script, con interi a 64 bit, virgola mobile, stringhe, vettori e file. Scrivi scheme per il prompt, scheme file.scm per eseguire un file.

versione 1.42 · Licenza: BSD-3-Clause · Sito del progetto

Scheme

A cosa serve

Scheme è il dialetto del Lisp nato per insegnare a programmare: tutto è un'espressione tra parentesi, le funzioni sono valori, la ricorsione è di casa. Questo è TinyScheme, un interprete piccolo e veloce ad avviarsi che copre buona parte di R5RS: liste, vettori, stringhe, caratteri, let, lambda, macro, continuazioni, porte su file e su stringa.

Come si usa

Comando Cosa fa
scheme apre il prompt interattivo (ts>)
scheme programma.scm esegue un file dalla cartella home
scheme programma.scm a b passa argomenti, letti in *args*
scheme -i programma.scm esegue il file e poi resta nel prompt
scheme -e '(display (* 6 7))' valuta un'espressione al volo

Nel prompt scrivi un'espressione e premi Invio; se lasci parentesi aperte continua alla riga dopo:

ts> (+ 1 2 3)
6
ts> (define (fatt n) (if (< n 2) 1 (* n (fatt (- n 1)))))
ts> (fatt 20)
2432902008176640000
ts> (map (lambda (x) (* x x)) (list 1 2 3 4))
(1 4 9 16)
ts> (let loop ((i 0) (s 0)) (if (> i 100) s (loop (+ i 1) (+ s i))))
5050
ts> (load "/programma.scm")

File: (define p (open-output-file "/dati.txt")) (write '(1 2 3) p) (close-output-port p) e poi (read (open-input-file "/dati.txt")).

Per uscire: (exit), oppure EOF. Dopo display il prompt mostra anche #t: è il valore restituito, non un errore.

Limiti

Gli interi sono a 64 bit (niente numeri enormi: (fatt 25) sfora; per quelli c'è bc) e non ci sono frazioni (1/3). Niente syntax-rules: le macro si scrivono con define-macro. Niente moduli R7RS (define-library, import) né processi esterni. Memoria: circa 300 000 celle (liste fino a un centinaio di migliaia di elementi), in tutto fino a 12 MB.

Nel Terminale

Scheme

TinyScheme, a small Scheme (R5RS subset): interactive prompt and scripts, with 64-bit integers, floats, strings, vectors and files. Type scheme for the prompt, scheme file.scm to run a file.

version 1.42 · License: BSD-3-Clause · Project site

Scheme

What it is

Scheme is the Lisp dialect made for teaching programming: everything is an expression in parentheses, functions are values, recursion feels at home. This is TinyScheme, a small interpreter that starts fast and covers much of R5RS: lists, vectors, strings, characters, let, lambda, macros, continuations, file and string ports.

How to use it

Command What it does
scheme opens the interactive prompt (ts>)
scheme program.scm runs a file from the home folder
scheme program.scm a b passes arguments, read from *args*
scheme -i program.scm runs the file, then stays at the prompt
scheme -e '(display (* 6 7))' evaluates one expression

At the prompt type an expression and press Enter; with parentheses left open the input continues on the next line:

ts> (+ 1 2 3)
6
ts> (define (fact n) (if (< n 2) 1 (* n (fact (- n 1)))))
ts> (fact 20)
2432902008176640000
ts> (map (lambda (x) (* x x)) (list 1 2 3 4))
(1 4 9 16)
ts> (let loop ((i 0) (s 0)) (if (> i 100) s (loop (+ i 1) (+ s i))))
5050
ts> (load "/program.scm")

Files: (define p (open-output-file "/data.txt")) (write '(1 2 3) p) (close-output-port p), then (read (open-input-file "/data.txt")).

To quit: (exit), or EOF. After display the prompt also shows #t: that's the returned value, not an error.

Limits

Integers are 64-bit (no huge numbers: (fact 25) overflows; use bc for those) and there are no fractions (1/3). No syntax-rules: macros are written with define-macro. No R7RS libraries (define-library, import) or external processes. Memory: about 300,000 cells (lists up to a hundred thousand or so items), up to 12 MB in all.

In the Terminal