(scheme base)
(scheme base) - R7RS Base Library
The core R7RS library containing fundamental procedures, syntax, and values. This provides the standard Scheme interface for portable code.
See: R7RS Small §6.1-6.14
For Sigil-specific extensions, use (sigil core) instead.
Exports
eq?procedureTest object identity (same memory location).
(eq? 'a 'a) ; => #t
(eq? '() '()) ; => #t
(eq? "a" "a") ; => unspecified
(define x '(1 2))
(eq? x x) ; => #tRe-exported from (sigil core)
eqv?procedureTest equivalence (same type and value).
(eqv? 1 1) ; => #t
(eqv? 1 1.0) ; => #f
(eqv? #\a #\a) ; => #tRe-exported from (sigil core)
equal?procedureTest structural equality (deep comparison).
(equal? '(1 2 3) '(1 2 3)) ; => #t
(equal? "hello" "hello") ; => #t
(equal? #(1 2) #(1 2)) ; => #tRe-exported from (sigil core)
notprocedureBoolean negation.
(not #f) ; => #t
(not #t) ; => #f
(not '()) ; => #f (only #f is false)
(not 0) ; => #fRe-exported from (sigil core)
boolean?procedureTest if a value is a boolean.
(boolean? #t) ; => #t
(boolean? #f) ; => #t
(boolean? 0) ; => #fRe-exported from (sigil core)
pair?procedureTest if a value is a pair.
(pair? '(1 . 2)) ; => #t
(pair? '(1 2)) ; => #t
(pair? '()) ; => #fRe-exported from (sigil core)
consprocedureConstruct a new pair with the given car and cdr.
(cons 1 2) ; => (1 . 2)
(cons 1 '(2 3)) ; => (1 2 3)
(cons 'a '()) ; => (a)Re-exported from (sigil core)
carprocedureReturn the first element (car) of a pair.
(car '(1 2 3)) ; => 1
(car '(a . b)) ; => a
(car '((1 2) 3 4)) ; => (1 2)Re-exported from (sigil core)
cdrprocedureReturn the rest (cdr) of a pair.
(cdr '(1 2 3)) ; => (2 3)
(cdr '(a . b)) ; => b
(cdr '(1)) ; => ()Re-exported from (sigil core)
set-car!procedureMutate the car of a pair.
(define p (cons 1 2))
(set-car! p 'a)
p ; => (a . 2)Re-exported from (sigil core)
set-cdr!procedureMutate the cdr of a pair.
(define p (cons 1 2))
(set-cdr! p 'b)
p ; => (1 . b)Re-exported from (sigil core)
caarprocedurecadrprocedureGet the car of the cdr (second element): (car (cdr x)).
(cadr '(1 2 3)) ; => 2Re-exported from (sigil core)
cdarprocedurecddrprocedureGet the cdr of the cdr (tail after second): (cdr (cdr x)).
(cddr '(1 2 3 4)) ; => (3 4)Re-exported from (sigil core)
null?procedureTest if a value is the empty list.
(null? '()) ; => #t
(null? '(1)) ; => #fRe-exported from (sigil core)
list?procedureTest if a value is a proper list.
(list? '(1 2 3)) ; => #t
(list? '()) ; => #t
(list? '(1 . 2)) ; => #f (improper list)Re-exported from (sigil core)
listprocedureCreate a list from the arguments.
(list 1 2 3) ; => (1 2 3)
(list) ; => ()
(list 'a 'b) ; => (a b)Re-exported from (sigil core)
make-listprocedureCreate a list of n copies of fill.
(make-list 3 'a) ; => (a a a)
(make-list 5) ; => (#f #f #f #f #f)
(make-list 0 'x) ; => ()If fill is omitted, #f is used.
Re-exported from (sigil core)
lengthprocedureReturn the length of a list.
(length '(1 2 3)) ; => 3
(length '()) ; => 0Re-exported from (sigil core)
appendprocedureConcatenate any number of lists into one.
Returns a new list with elements from all lists in order. The last list is shared (not copied).
(append '(1 2) '(3 4)) ; => (1 2 3 4)
(append '(a) '(b) '(c d)) ; => (a b c d)
(append '(1 2) '()) ; => (1 2)Re-exported from (sigil core)
reverseprocedureReverse the order of elements in a list.
Returns a new list with elements in reverse order.
(reverse '(1 2 3)) ; => (3 2 1)
(reverse '(a b c d)) ; => (d c b a)
(reverse '()) ; => ()Re-exported from (sigil core)
list-refvariableRe-exported from (sigil core)
memqvariableRe-exported from (sigil core)
memvvariableRe-exported from (sigil core)
membervariableRe-exported from (sigil core)
assqvariableRe-exported from (sigil core)
assvvariableRe-exported from (sigil core)
assocvariableRe-exported from (sigil core)
mapprocedureApply a procedure to each element of a list, returning a new list.
(map (lambda (x) (* x 2)) '(1 2 3)) ; => (2 4 6)
(map symbol->string '(a b c)) ; => ("a" "b" "c")For multi-list mapping, use list-map from (sigil list):
(import (sigil list))
(list-map + '(1 2 3) '(10 20 30)) ; => (11 22 33)Re-exported from (sigil core)
for-eachprocedureApply a procedure to each element of a list for side effects.
Like map, but doesn't collect results—used for side effects like printing.
(for-each println '("a" "b" "c")) ; prints a, b, c on separate lines
(for-each (lambda (x) (set! sum (+ sum x))) '(1 2 3))For multi-list iteration, use list-for-each from (sigil list):
(import (sigil list))
(list-for-each (lambda (a b) (println "~a" (+ a b))) '(1 2) '(10 20))Re-exported from (sigil core)
symbol?procedureTest if a value is a symbol.
(symbol? 'foo) ; => #t
(symbol? "foo") ; => #fRe-exported from (sigil core)
symbol->stringprocedureConvert a symbol to its string name.
(symbol->string 'hello) ; => "hello"
(symbol->string '+) ; => "+"Re-exported from (sigil core)
string->symbolprocedureConvert a string to a symbol (interned).
(string->symbol "hello") ; => hello
(eq? (string->symbol "x") (string->symbol "x")) ; => #tRe-exported from (sigil core)
number?procedureTest if a value is a number.
(number? 42) ; => #t
(number? 3.14) ; => #t
(number? "42") ; => #fRe-exported from (sigil core)
integer?procedureTest if a value is an exact integer.
(integer? 42) ; => #t
(integer? 3.0) ; => #f
(integer? 3.5) ; => #fRe-exported from (sigil core)
=procedureTest if all arguments are numerically equal.
(= 1 1) ; => #t
(= 1 1 1) ; => #t
(= 1 2) ; => #f
(= 1 1.0) ; => #tRe-exported from (sigil core)
<procedureTest if arguments are in strictly increasing order.
(< 1 2) ; => #t
(< 1 2 3) ; => #t
(< 1 1) ; => #fRe-exported from (sigil core)
>procedureTest if arguments are in strictly decreasing order.
(> 2 1) ; => #t
(> 3 2 1) ; => #t
(> 1 1) ; => #fRe-exported from (sigil core)
<=procedureTest if arguments are in non-decreasing order.
(<= 1 2) ; => #t
(<= 1 1) ; => #t
(<= 2 1) ; => #fRe-exported from (sigil core)
>=procedureTest if arguments are in non-increasing order.
(>= 2 1) ; => #t
(>= 1 1) ; => #t
(>= 1 2) ; => #fRe-exported from (sigil core)
zero?procedureCheck if a number is zero.
(zero? 0) ; => #t
(zero? 1) ; => #f
(zero? -1) ; => #f
(zero? 0.0) ; => #tRe-exported from (sigil core)
positive?procedureCheck if a number is positive (greater than zero).
(positive? 5) ; => #t
(positive? 0) ; => #f
(positive? -3) ; => #f
(positive? 0.1) ; => #tRe-exported from (sigil core)
negative?procedureCheck if a number is negative (less than zero).
(negative? -5) ; => #t
(negative? 0) ; => #f
(negative? 3) ; => #f
(negative? -0.1) ; => #tRe-exported from (sigil core)
odd?procedureodd? - Test if an integer is odd (odd? 3) => #t (odd? 4) => #f
Re-exported from (sigil core)
even?procedureeven? - Test if an integer is even (even? 4) => #t (even? 3) => #f
Re-exported from (sigil core)
+procedureAdd zero or more numbers.
(+) ; => 0
(+ 1) ; => 1
(+ 1 2 3) ; => 6
(+ 1.5 2.5) ; => 4.0Re-exported from (sigil core)
-procedureSubtract numbers or negate a single number.
(- 5) ; => -5 (negation)
(- 5 3) ; => 2
(- 10 3 2) ; => 5Re-exported from (sigil core)
*procedureMultiply zero or more numbers.
(*) ; => 1
(* 2) ; => 2
(* 2 3 4) ; => 24Re-exported from (sigil core)
/procedureDivide numbers or compute reciprocal.
(/ 2) ; => 0.5 (reciprocal)
(/ 10 2) ; => 5
(/ 24 2 3) ; => 4Re-exported from (sigil core)
number->stringprocedureConvert a number to its string representation.
(number->string 42) ; => "42"
(number->string 3.14) ; => "3.14"
(number->string 255 16) ; => "ff" (with radix)Re-exported from (sigil core)
string->numberprocedureParse a string as a number, returning #f if invalid.
(string->number "42") ; => 42
(string->number "3.14") ; => 3.14
(string->number "ff" 16) ; => 255 (with radix)
(string->number "abc") ; => #fRe-exported from (sigil core)
char?procedurechar=?procedureTest if two characters are equal.
(char=? #\a #\a) ; => #t
(char=? #\a #\A) ; => #fRe-exported from (sigil core)
char<?procedureTest if the first character is less than the second.
(char<? #\a #\b) ; => #t
(char<? #\b #\a) ; => #f
(char<? #\A #\a) ; => #t (uppercase before lowercase in Unicode)Re-exported from (sigil core)
char>?procedureTest if the first character is greater than the second.
(char>? #\b #\a) ; => #t
(char>? #\a #\b) ; => #fRe-exported from (sigil core)
char<=?procedureTest if the first character is less than or equal to the second.
(char<=? #\a #\a) ; => #t
(char<=? #\a #\b) ; => #t
(char<=? #\b #\a) ; => #fRe-exported from (sigil core)
char>=?procedureTest if the first character is greater than or equal to the second.
(char>=? #\a #\a) ; => #t
(char>=? #\b #\a) ; => #t
(char>=? #\a #\b) ; => #fRe-exported from (sigil core)
char->integerprocedureConvert a character to its Unicode code point.
(char->integer #\a) ; => 97
(char->integer #\A) ; => 65
(char->integer #\λ) ; => 955
(char->integer #\space) ; => 32Re-exported from (sigil core)
integer->charprocedureConvert a Unicode code point to its character.
(integer->char 97) ; => #\a
(integer->char 65) ; => #\A
(integer->char 955) ; => #\λRe-exported from (sigil core)
string?procedureTest if a value is a string.
(string? "hello") ; => #t
(string? 'hello) ; => #fRe-exported from (sigil core)
make-stringprocedureCreate a string of n copies of char.
(make-string 3 #\a) ; => "aaa"
(make-string 5) ; => " "
(make-string 0 #\x) ; => ""If char is omitted, space is used.
Re-exported from (sigil core)
stringprocedureCreate a string from zero or more character arguments.
(string #\h #\i) ; => "hi"
(string) ; => ""
(string #\λ) ; => "λ"Re-exported from (sigil core)
string-lengthprocedureReturn the number of characters in the string.
(string-length "hello") ; => 5
(string-length "") ; => 0
(string-length "λ") ; => 1 (counts characters, not bytes)Re-exported from (sigil core)
string-refprocedureReturn the character at position k in the string.
(string-ref "hello" 0) ; => #\h
(string-ref "hello" 4) ; => #\oThe index k must be a non-negative integer less than the string length. Raises an error if the index is out of bounds.
Re-exported from (sigil core)
string=?procedureTest if two strings are equal.
(string=? "hello" "hello") ; => #t
(string=? "hello" "Hello") ; => #f (case-sensitive)
(string=? "" "") ; => #tRe-exported from (sigil core)
substringprocedureExtract a substring from start (inclusive) to end (exclusive).
(substring "hello" 1 4) ; => "ell"
(substring "hello" 0 5) ; => "hello"
(substring "hello" 2 2) ; => ""Both start and end must be valid indices where 0 <= start <= end <= length.
Re-exported from (sigil core)
string-appendprocedureConcatenate zero or more strings into a single string.
(string-append "hello" " " "world") ; => "hello world"
(string-append) ; => ""
(string-append "only") ; => "only"Re-exported from (sigil core)
string-copyprocedureCopy a string, optionally with start and end indices. (string-copy str) -> new string copy (string-copy str start) -> copy from start to end (string-copy str start end) -> copy from start to end
Re-exported from (sigil core)
string->listprocedureConvert a string to a list of characters.
(string->list "hello") ; => (#\h #\e #\l #\l #\o)
(string->list "") ; => ()Re-exported from (sigil core)
list->stringprocedureConvert a list of characters to a string.
(list->string '(#\h #\i)) ; => "hi"
(list->string '()) ; => ""Re-exported from (sigil core)
string-for-eachprocedureApply procedure to each character of a string for side effects. (string-for-each display "abc") displays abc
Re-exported from (sigil core)
string-mapprocedureApply procedure to each character, return string of results. (string-map char-upcase "abc") => "ABC"
Re-exported from (sigil core)
vector?procedureTest if a value is a vector.
(vector? #(1 2 3)) ; => #t
(vector? '(1 2 3)) ; => #fRe-exported from (sigil core)
make-vectorprocedureCreate a vector of the given length, optionally filled with a value.
(make-vector 3) ; => #(#<undefined> #<undefined> #<undefined>)
(make-vector 3 'x) ; => #(x x x)
(make-vector 0) ; => #()Re-exported from (sigil core)
vector-lengthprocedureReturn the number of elements in the vector.
(vector-length #(a b c)) ; => 3
(vector-length #()) ; => 0Re-exported from (sigil core)
vector-refprocedureReturn the element at position k in the vector.
(vector-ref #(a b c) 0) ; => a
(vector-ref #(a b c) 2) ; => cThe index must be a non-negative integer less than the vector length.
Re-exported from (sigil core)
vector-set!procedureSet the element at position k in the vector.
(define v #(1 2 3))
(vector-set! v 1 'x)
v ; => #(1 x 3)Re-exported from (sigil core)
vector->listprocedureConvert a vector to a list. (vector->list vec) -> list (vector->list vec start) -> list from start to end (vector->list vec start end) -> list from start to end
Re-exported from (sigil core)
list->vectorprocedureConvert a list to a vector. (list->vector lst) -> vector
Re-exported from (sigil core)
vector-copyprocedureCopy a vector, optionally with start and end indices. (vector-copy vec) -> new vector copy (vector-copy vec start) -> copy from start to end (vector-copy vec start end) -> copy from start to end
Re-exported from (sigil core)
vector-fill!procedureFill a vector with a value. (vector-fill! vec fill) -> fills entire vector (vector-fill! vec fill start) -> fills from start to end (vector-fill! vec fill start end) -> fills from start to end
Re-exported from (sigil core)
vector-for-eachprocedureApply procedure to each element of a vector for side effects. (vector-for-each display #(1 2 3)) displays 123
Re-exported from (sigil core)
vector-mapprocedureApply procedure to each element, return vector of results. (vector-map (lambda (x) (* x 2)) #(1 2 3)) => #(2 4 6)
Re-exported from (sigil core)
bytevector?procedureTest if a value is a bytevector.
(bytevector? #u8(1 2 3)) ; => #t
(bytevector? "string") ; => #fRe-exported from (sigil core)
make-bytevectorprocedureCreate a bytevector of the given size, optionally filled with a byte value.
(make-bytevector 3) ; => #u8(0 0 0)
(make-bytevector 3 42) ; => #u8(42 42 42)Re-exported from (sigil core)
bytevector-lengthprocedureReturn the number of bytes in the bytevector.
(bytevector-length #u8(1 2 3)) ; => 3Re-exported from (sigil core)
bytevector-u8-refprocedureReturn the byte at position k in the bytevector.
(bytevector-u8-ref #u8(10 20 30) 1) ; => 20Re-exported from (sigil core)
bytevector-u8-set!procedureSet the byte at position k in the bytevector.
(define bv (make-bytevector 3 0))
(bytevector-u8-set! bv 1 255)
bv ; => #u8(0 255 0)Re-exported from (sigil core)
bytevector-copyprocedureCopy a bytevector, optionally with start and end indices. (bytevector-copy bv) -> new bytevector copy (bytevector-copy bv start) -> copy from start to end (bytevector-copy bv start end) -> copy from start to end
Re-exported from (sigil core)
bytevector-appendprocedureAppend multiple bytevectors. (bytevector-append #u8(1 2) #u8(3 4)) => #u8(1 2 3 4)
Re-exported from (sigil core)
utf8->stringvariableRe-exported from (sigil io)
string->utf8variableRe-exported from (sigil io)
procedure?procedureTest if a value is a procedure.
(procedure? +) ; => #t
(procedure? (lambda (x) x)) ; => #t
(procedure? 42) ; => #fRe-exported from (sigil core)
applyprocedureApply a procedure to a list of arguments.
(apply + '(1 2 3)) ; => 6
(apply list 'a 'b '(c d)) ; => (a b c d)Re-exported from (sigil core)
valuesprocedureReturn multiple values.
(values 1 2 3) ; returns 3 values
(call-with-values (lambda () (values 1 2))
(lambda (a b) (+ a b))) ; => 3Re-exported from (sigil core)
call-with-valuesprocedureCall producer with no args, pass its values to consumer.
(call-with-values
(lambda () (values 1 2))
(lambda (a b) (+ a b))) ; => 3Re-exported from (sigil core)
errorprocedureConvenience function to raise an error exception. (error "message" irritant ...)
Re-exported from (sigil core)
raiseprocedureRaise an exception (non-continuable). Aborts to the nearest exception handler.
Re-exported from (sigil core)
raise-continuableprocedureRaise a continuable exception. The handler can return a value to continue execution.
Re-exported from (sigil core)
with-exception-handlerprocedureInstall an exception handler and execute thunk (R6RS style). Handler is a procedure taking the exception. Thunk is the body to execute.
Re-exported from (sigil core)
input-port?variableRe-exported from (sigil core)
output-port?variableRe-exported from (sigil core)
port?variableRe-exported from (sigil core)
current-input-portvariableRe-exported from (sigil core)
current-output-portvariableRe-exported from (sigil core)
current-error-portvariableRe-exported from (sigil core)
close-portvariableRe-exported from (sigil core)
close-input-portvariableRe-exported from (sigil core)
close-output-portvariableRe-exported from (sigil core)
port-open?variableRe-exported from (sigil core)
open-input-stringprocedureCreate an input port that reads from a string.
Useful for parsing strings as if they were files.
(let ((port (open-input-string "(1 2 3)")))
(read port))
; => (1 2 3)Re-exported from (sigil io)
open-output-stringprocedureCreate an output port that writes to a string.
Use get-output-string to retrieve the accumulated output. Much more efficient than building strings with repeated string-append calls.
(let ((port (open-output-string)))
(display "Hello, " port)
(display "World!" port)
(get-output-string port))
; => "Hello, World!"Re-exported from (sigil io)
get-output-stringprocedureGet the accumulated string from a string output port.
Can be called multiple times; each call returns all output written so far.
Re-exported from (sigil io)
read-charprocedureRead a single character from a port.
Returns the next character, or the end-of-file object if the port is exhausted.
(read-char port) ; => #\H
(read-char port) ; => #\eRe-exported from (sigil io)
peek-charprocedureLook at the next character without consuming it.
Returns the next character that would be read, but leaves it in the input stream.
(peek-char port) ; => #\H
(peek-char port) ; => #\H (still there)
(read-char port) ; => #\H (now consumed)Re-exported from (sigil io)
read-lineprocedureRead a line of text from a port.
Returns all characters up to and not including the newline. Returns the end-of-file object if already at end of input.
;; Read all lines from a file
(let loop ((lines '()))
(let ((line (read-line port)))
(if (eof-object? line)
(reverse lines)
(loop (cons line lines)))))Re-exported from (sigil io)
write-charprocedureWrite a single character to a port.
(write-char #\A port)
(write-char #\newline port)Re-exported from (sigil io)
write-stringprocedureWrite a string to a port.
Outputs the string's characters. Optional start and end indices write a substring.
(write-string "Hello, World!" port)
(write-string "Hello" port 0 4) ; writes "Hell"Re-exported from (sigil io)
newlineprocedureWrite a newline to a port (default: current output port).
(newline) ; prints a newline characterRe-exported from (sigil core)
eof-object?procedureTest if a value is the end-of-file object.
(eof-object? (eof-object)) ; => #t
(eof-object? #f) ; => #fRe-exported from (sigil core)
call-with-portprocedurecall-with-port - R7RS procedure for safe port handling Calls proc with port as argument, closes port when proc returns, and returns the result of proc.
Usage: (call-with-port (open-input-file "data.txt") (lambda (port) (read-line port)))
Re-exported from (sigil core)
make-parameterprocedureCreate a parameter object (dynamic variable).
(define my-param (make-parameter 10))
(my-param) ; => 10
(my-param 20) ; set to 20
(my-param) ; => 20Re-exported from (sigil core)
define-record-typevariableRe-exported from (srfi srfi-9)