t3x.org / sketchy / vol1 / sl28.html

Sketchy LISP

  By Nils M Holm, 2006,2007,2008
Buy a copy at Lulu.com

An Introduction to Functional Programming in Scheme

A.4 Scheme Syntax and Procedures

This is a summary of the Scheme syntax and procedures discussed in this book.

Key

body  a sequence of expressions
char  a char
expr  any type
form  any datum
int   an integer
list  a list
num   any numeric type
pair  a pair
proc  a procedure
procn a procedure of n arguments
str   a string
sym   a symbol
tval  a truth value
void  an unspecific value
[x]   x is optional
x ... zero or more occurrences of x
x|y   x or y

Syntax

'form => form
Turn form into a datum. Alias: quote.
,expr => form
Evaluate expr in a quasiquote template. Alias: unquote.
,@expr => spliced-form
Evaluate expr in a quasiquote template and splice into surrounding list. Alias: unquote-splicing.
`qq-template => form
Quasiquote the quasiquote template qq-template. Alias: quasiquote.
(expr0 expr1 ...)
Apply expr0 to expr1... Expr0 must reduce to a procedure.
(and expr1 ...) => form
Evaluate expressions in sequence until one gives #f.
(begin expr1 ...) => form
Evaluate expressions in sequence.
(case expr (list1 body1) (list2 body2) ...) => form
Evaluate first body whose list contains expr.
(cond (expr1 body1) (expr2 body2) ...) => form
Evaluate first body whose expr reduces to truth.
(define sym expr) => void
Introduce sym and bind it to (a location referring to) expr.
(define (sym1 sym2 ...) body) => void
(define (sym1 sym2 ... . symn) body) => void
Introduce sym1 and bind it to (a location referring to) a procedure with the variables sym2... and the given body.
(define-syntax keyword transformer) => void
Introduce the keyword keyword and bind it to the syntax transformer transformer. See also: syntax-rules.
(if expr1 expr2 [expr3]) => form
Evaluate to expr2 if expr1 reduces to truth, and to expr3 otherwise.
(lambda sym body) => proc
(lambda (sym1 sym2 ... . symn) body) => proc
Evaluate to a variadic procedure with the variable sym (or the variables sym1...) and the given body.
(lambda (sym1 ...) body) => proc
Evaluate to a procedure with the variables sym1... and the given body.
(let ((sym1 expr1) ...) body) => form
Bind symi to expri locally giving a new context and evaluate body in that context.
(let* ((sym1 expr1) ...) body) => form
Like let, but bind expressions to symbols sequentially.
(letrec ((sym1 expr1) ...) body) => form
Like let, but fix recursive references.
(or expr1 ...) => form
Evaluate expressions in sequence until one gives truth.
(quasiquote qq-template) => form
Quasiquote the quasiquote template qq-template. Alias: `.
(quote form) => form
Turn form into a datum. Alias: '.
(set! sym expr) => void
Change the value of sym to expr.
(syntax-rules (sym1 ...) (pat1 temp1) (pat2 temp2) ...) => transformer
Evaluate to a syntax transformer. Sym1... are local keywords, pat is a pattern and temp is a template. See also: define-syntax.
(unquote expr) => form
Evaluate expr in a quasiquote template. Alias: ,.
(unquote-splicing expr) => spliced-form
Evaluate expr in a quasiquote template and splice into surrounding list. Alias: ,@.

Procedures

(* num1 ...) => num
Evaluate to the product of the given numbers.
(+ num1 ...) => num
Evaluate to the sum of the given numbers.
(- num1 num2 ...) => num
Evaluate to the difference of the given numbers. If only one argument n is given, return -n.
(/ num1 num2 ...) => num
Evaluate to the quotient of the given numbers. If only one argument n is given, return 1/n.
(< num1 num2 num3 ...) => tval
(<= num1 num2 num3 ...) => tval
(> num1 num2 num3 ...) => tval
(>= num1 num2 num3 ...) => tval
Check whether the given arguments are in a specific order. E.g., < checks whether its arguments are in strict ascending order.
(= num1 num2 num3 ...) => tval
Check whether the given numbers are equivalent.
(abs num) => num
Return the absolute value of num.
(append list1 ...) => list
Append lists, giving a (proper) list.
(append list1 ... expr) => pair
Append lists, giving a (potentially improper) list.
(apply proc expr1 ... list) => form
Apply proc to the arguments in list. If exprs are given, cons them to list.
(assoc expr alist) => pair|#f
Find pair with key=expr in alist. Use equal? to compare keys.
(assq expr alist) => pair|#f
Find pair with key=expr in alist. Use eq? to compare keys.
(assv expr alist) => pair|#f
Find pair with key=expr in alist. Use eqv? to compare keys.
(caar pair) ... (cddddr pair) => form
Extract members of lists. E.g. (cadr x) = (car (cdr x)).
(call-with-current-continuation proc1) => form
(call/cc proc1) => form
Capture current continuation and pass it to proc1.
(car pair) => form
Extract car part (head) of pair.
(cdr pair) => form
Extract cdr part (tail) of pair.
(char-ci<? char1 char2 char3 ...) => tval
(char-ci<=? char1 char2 char3 ...) => tval
(char-ci>? char1 char2 char3 ...) => tval
(char-ci>=? char1 char2 char3 ...) => tval
Check whether the given characters are in a specific lexical order. E.g., char-ci<? checks whether its arguments are in strict alphabetical order. Ignore the case of the characters.
(char-ci=? char1 char2 char3 ...) => tval
Check whether the given characters are equal. Ignore their case.
(char->integer char) => int
Convert char to integer.
(char-alphabetic? char) => tval
(char-lower-case? char) => tval
(char-numeric? char) => tval
(char-upper-case? char) => tval
(char-whitespace? char) => tval
Check properties of characters. E.g. (char-numeric? x) checks whether x is in the range #\0...#\9.
(char-downcase char) => char
(char-upcase char) => char
Change the case of a letter. E.g. (char-upcase #\c) => #\C.
(char<? char1 char2 char3 ...) => tval
(char<=? char1 char2 char3 ...) => tval
(char>? char1 char2 char3 ...) => tval
(char>=? char1 char2 char3 ...) => tval
Check whether the given characters are in a specific lexical order. E.g., char<? checks whether its arguments are in strict alphabetical order.
(char=? char1 char2 char3 ...) => tval
Check whether the given characters are equal.
(cons expr1 expr2) => pair
Construct a fresh pair with head=expr1 and tail=expr2.
(display expr [out-port]) => void
Print expr in a prettier but less accurate form than write. If an output port is specified, write to that port.
(eof-object? expr) => tval
Check whether expr is the end-of-file object.
(eq? expr1 expr2) => tval
Check whether expr1 and expr2 are identical.
(equal? expr1 expr2) => tval
Check whether expr1 and expr2 are equal.
(eqv? expr1 expr2) => tval
Check whether expr1 and expr2 are equivalent.
(floor num) => int
Return the greatest integer that is not greater than num.
(gcd int1 ...) => int
Return the greatest common divisor of int1....
(integer->char int) => char
Convert integer to char.
(lcm int1 ...) => int
Return the least common multiple of int1....
(length list) => int
Return the length of list.
(list expr1 ...) => list
Create a fresh list containing the given exprs and return it.
(list->string list) => str
Convert list to string.
(list-ref list int) => form
Extract the int'th member of list.
(list? expr) => tval
Check whether expr is a (proper) list.
(map proc list1 list2 ...) => list
Map procedure proc over the given lists.
(max num1 num2 ...) => num
Return the greatest of the given numbers.
(member expr list) => list|#f
Return the first sublist of list whose head equals expr, or #f if no such sublist exists. Use equal? to compare list elements.
(memq expr list) => list|#f
Return the first sublist of list whose head equals expr, or #f if no such sublist exists. Use eq? to compare list elements.
(memv expr list) => list|#f
Return the first sublist of list whose head equals expr, or #f if no such sublist exists. Use eqv? to compare list elements.
(min num1 num2 ...) => num
Return the least of the given numbers.
(modulo int1 int2) => int
Return int1 modulo int2 (division remainder).
(negative? num) => tval
Check whether num is negative.
(newline [out-port]) => void
Display a newline sequence. If an output port is given, write to that port.
(not expr) => tval
Logical not, i.e. (eq? expr #f).
(null? expr) => tval
Check whether expr is the empty list.
(number? expr) => tval
Check whether expr is a number.
(pair? expr) => tval
Check whether expr is a pair.
(quotient int1 int2) => int
Return the integer part of the quotient of int1 and int2.
(read [in-port]) => form
Read the external representation of a form and return a corresponding internal representation. If in input port is given, read from that port.
(read-char [in-port]) => char
Read a character and return it. If in input port is given, read from that port.
(remainder int1 int2) => int
Return the division remainder of int1 and int2.
(sqrt num) => num
Evaluate to the square root of num.
(string char1 ...) => str
Create a fresh string containing the given chars and return it.
(string->list str) => list
Convert string to list (of chars).
(string->symbol str) => sym
Convert string to symbol.
(string-ci<=? str1 str2 str3 ...) => tval
(string-ci<? str1 str2 str3 ...) => tval
(string-ci>=? str1 str2 str3 ...) => tval
(string-ci>? str1 str2 str3 ...) => tval
Check whether the given strings are in a specific lexical order. E.g., string-ci<? checks whether its arguments are in strict alphabetical order. Ignore case.
(string-ci=? str1 str2 str3 ...) => tval
Check whether the given strings are equal. Ignore case.
(string-length str) => int
Return the length of a string.
(string-ref str int) => char
Return int'th char of str.
(string<=? str1 str2 str3 ...) => tval
(string<? str1 str2 str3 ...) => tval
(string>=? str1 str2 str3 ...) => tval
(string>? str1 str2 str3 ...) => tval
Check whether the given strings are in a specific lexical order. E.g., string<? checks whether its arguments are in strict alphabetical order.
(string=? str1 str2 str3 ...) => tval
Check whether the given strings are equal.
(substring str int1 int2) => str
Extract substring starting at int1 and extending up to but not including int2 from str and return it.
(symbol->string sym) => str
Convert symbol to string.
(with-input-from-file str proc0) => form
Connect the standard input port to the file named in str and evaluate (proc0) in that context.
(with-output-to-file str proc0) => form
Connect the standard output port to the file named in str and evaluate (proc0) in that context.
(write expr [out-port]) => void
Write the external representation of expr. If an output port is specified, write to that port.
(zero? num) => tval
Check whether num equals zero.