sigildocs

(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?procedure

Test object identity (same memory location).

(eq? 'a 'a)           ; => #t
(eq? '() '())         ; => #t
(eq? "a" "a")         ; => unspecified
(define x '(1 2))
(eq? x x)             ; => #t

Re-exported from (sigil core)

eqv?procedure

Test equivalence (same type and value).

(eqv? 1 1)        ; => #t
(eqv? 1 1.0)      ; => #f
(eqv? #\a #\a)    ; => #t

Re-exported from (sigil core)

equal?procedure

Test structural equality (deep comparison).

(equal? '(1 2 3) '(1 2 3))   ; => #t
(equal? "hello" "hello")     ; => #t
(equal? #(1 2) #(1 2))       ; => #t

Re-exported from (sigil core)

notprocedure

Boolean negation.

(not #f)      ; => #t
(not #t)      ; => #f
(not '())     ; => #f  (only #f is false)
(not 0)       ; => #f

Re-exported from (sigil core)

boolean?procedure

Test if a value is a boolean.

(boolean? #t)  ; => #t
(boolean? #f)  ; => #t
(boolean? 0)   ; => #f

Re-exported from (sigil core)

pair?procedure

Test if a value is a pair.

(pair? '(1 . 2))  ; => #t
(pair? '(1 2))    ; => #t
(pair? '())       ; => #f

Re-exported from (sigil core)

consprocedure

Construct 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)

carprocedure

Return 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)

cdrprocedure

Return 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!procedure

Mutate the car of a pair.

(define p (cons 1 2))
(set-car! p 'a)
p  ; => (a . 2)

Re-exported from (sigil core)

set-cdr!procedure

Mutate the cdr of a pair.

(define p (cons 1 2))
(set-cdr! p 'b)
p  ; => (1 . b)

Re-exported from (sigil core)

caarprocedure

Get the car of the car: (car (car x)).

(caar '((1 2) 3))  ; => 1

Re-exported from (sigil core)

cadrprocedure

Get the car of the cdr (second element): (car (cdr x)).

(cadr '(1 2 3))  ; => 2

Re-exported from (sigil core)

cdarprocedure

Get the cdr of the car: (cdr (car x)).

(cdar '((1 2 3) 4))  ; => (2 3)

Re-exported from (sigil core)

cddrprocedure

Get the cdr of the cdr (tail after second): (cdr (cdr x)).

(cddr '(1 2 3 4))  ; => (3 4)

Re-exported from (sigil core)

null?procedure

Test if a value is the empty list.

(null? '())   ; => #t
(null? '(1))  ; => #f

Re-exported from (sigil core)

list?procedure

Test 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)

listprocedure

Create a list from the arguments.

(list 1 2 3)    ; => (1 2 3)
(list)          ; => ()
(list 'a 'b)    ; => (a b)

Re-exported from (sigil core)

make-listprocedure

Create 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)

lengthprocedure

Return the length of a list.

(length '(1 2 3))  ; => 3
(length '())       ; => 0

Re-exported from (sigil core)

appendprocedure

Concatenate 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)

reverseprocedure

Reverse 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-refvariable

Re-exported from (sigil core)

memqvariable

Re-exported from (sigil core)

memvvariable

Re-exported from (sigil core)

membervariable

Re-exported from (sigil core)

assqvariable

Re-exported from (sigil core)

assvvariable

Re-exported from (sigil core)

assocvariable

Re-exported from (sigil core)

mapprocedure

Apply 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-eachprocedure

Apply 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?procedure

Test if a value is a symbol.

(symbol? 'foo)    ; => #t
(symbol? "foo")   ; => #f

Re-exported from (sigil core)

Convert a symbol to its string name.

(symbol->string 'hello)  ; => "hello"
(symbol->string '+)      ; => "+"

Re-exported from (sigil core)

Convert a string to a symbol (interned).

(string->symbol "hello")  ; => hello
(eq? (string->symbol "x") (string->symbol "x"))  ; => #t

Re-exported from (sigil core)

number?procedure

Test if a value is a number.

(number? 42)    ; => #t
(number? 3.14)  ; => #t
(number? "42")  ; => #f

Re-exported from (sigil core)

integer?procedure

Test if a value is an exact integer.

(integer? 42)    ; => #t
(integer? 3.0)   ; => #f
(integer? 3.5)   ; => #f

Re-exported from (sigil core)

=procedure

Test if all arguments are numerically equal.

(= 1 1)       ; => #t
(= 1 1 1)     ; => #t
(= 1 2)       ; => #f
(= 1 1.0)     ; => #t

Re-exported from (sigil core)

<procedure

Test if arguments are in strictly increasing order.

(< 1 2)       ; => #t
(< 1 2 3)     ; => #t
(< 1 1)       ; => #f

Re-exported from (sigil core)

>procedure

Test if arguments are in strictly decreasing order.

(> 2 1)       ; => #t
(> 3 2 1)     ; => #t
(> 1 1)       ; => #f

Re-exported from (sigil core)

<=procedure

Test if arguments are in non-decreasing order.

(<= 1 2)      ; => #t
(<= 1 1)      ; => #t
(<= 2 1)      ; => #f

Re-exported from (sigil core)

>=procedure

Test if arguments are in non-increasing order.

(>= 2 1)      ; => #t
(>= 1 1)      ; => #t
(>= 1 2)      ; => #f

Re-exported from (sigil core)

zero?procedure

Check if a number is zero.

(zero? 0)    ; => #t
(zero? 1)    ; => #f
(zero? -1)   ; => #f
(zero? 0.0)  ; => #t

Re-exported from (sigil core)

positive?procedure

Check if a number is positive (greater than zero).

(positive? 5)    ; => #t
(positive? 0)    ; => #f
(positive? -3)   ; => #f
(positive? 0.1)  ; => #t

Re-exported from (sigil core)

negative?procedure

Check if a number is negative (less than zero).

(negative? -5)   ; => #t
(negative? 0)    ; => #f
(negative? 3)    ; => #f
(negative? -0.1) ; => #t

Re-exported from (sigil core)

odd?procedure

odd? - Test if an integer is odd (odd? 3) => #t (odd? 4) => #f

Re-exported from (sigil core)

even?procedure

even? - Test if an integer is even (even? 4) => #t (even? 3) => #f

Re-exported from (sigil core)

+procedure

Add zero or more numbers.

(+)           ; => 0
(+ 1)         ; => 1
(+ 1 2 3)     ; => 6
(+ 1.5 2.5)   ; => 4.0

Re-exported from (sigil core)

-procedure

Subtract numbers or negate a single number.

(- 5)         ; => -5  (negation)
(- 5 3)       ; => 2
(- 10 3 2)    ; => 5

Re-exported from (sigil core)

*procedure

Multiply zero or more numbers.

(*)           ; => 1
(* 2)         ; => 2
(* 2 3 4)     ; => 24

Re-exported from (sigil core)

/procedure

Divide numbers or compute reciprocal.

(/ 2)         ; => 0.5  (reciprocal)
(/ 10 2)      ; => 5
(/ 24 2 3)    ; => 4

Re-exported from (sigil core)

Convert 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)

Parse 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")    ; => #f

Re-exported from (sigil core)

char?procedure

Test if a value is a character.

(char? #\a)    ; => #t
(char? "a")    ; => #f

Re-exported from (sigil core)

char=?procedure

Test if two characters are equal.

(char=? #\a #\a)  ; => #t
(char=? #\a #\A)  ; => #f

Re-exported from (sigil core)

char<?procedure

Test 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>?procedure

Test if the first character is greater than the second.

(char>? #\b #\a)  ; => #t
(char>? #\a #\b)  ; => #f

Re-exported from (sigil core)

char<=?procedure

Test if the first character is less than or equal to the second.

(char<=? #\a #\a)  ; => #t
(char<=? #\a #\b)  ; => #t
(char<=? #\b #\a)  ; => #f

Re-exported from (sigil core)

char>=?procedure

Test if the first character is greater than or equal to the second.

(char>=? #\a #\a)  ; => #t
(char>=? #\b #\a)  ; => #t
(char>=? #\a #\b)  ; => #f

Re-exported from (sigil core)

char->integerprocedure

Convert a character to its Unicode code point.

(char->integer #\a)    ; => 97
(char->integer #\A)    ; => 65
(char->integer #\λ)    ; => 955
(char->integer #\space) ; => 32

Re-exported from (sigil core)

integer->charprocedure

Convert a Unicode code point to its character.

(integer->char 97)   ; => #\a
(integer->char 65)   ; => #\A
(integer->char 955)  ; => #\λ

Re-exported from (sigil core)

string?procedure

Test if a value is a string.

(string? "hello")  ; => #t
(string? 'hello)   ; => #f

Re-exported from (sigil core)

make-stringprocedure

Create 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)

stringprocedure

Create a string from zero or more character arguments.

(string #\h #\i)  ; => "hi"
(string)          ; => ""
(string #\λ)      ; => "λ"

Re-exported from (sigil core)

string-lengthprocedure

Return 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-refprocedure

Return the character at position k in the string.

(string-ref "hello" 0)  ; => #\h
(string-ref "hello" 4)  ; => #\o

The 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=?procedure

Test if two strings are equal.

(string=? "hello" "hello")  ; => #t
(string=? "hello" "Hello")  ; => #f  (case-sensitive)
(string=? "" "")            ; => #t

Re-exported from (sigil core)

substringprocedure

Extract 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-appendprocedure

Concatenate 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-copyprocedure

Copy 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->listprocedure

Convert a string to a list of characters.

(string->list "hello")  ; => (#\h #\e #\l #\l #\o)
(string->list "")       ; => ()

Re-exported from (sigil core)

list->stringprocedure

Convert a list of characters to a string.

(list->string '(#\h #\i))  ; => "hi"
(list->string '())         ; => ""

Re-exported from (sigil core)

Apply procedure to each character of a string for side effects. (string-for-each display "abc") displays abc

Re-exported from (sigil core)

string-mapprocedure

Apply procedure to each character, return string of results. (string-map char-upcase "abc") => "ABC"

Re-exported from (sigil core)

vector?procedure

Test if a value is a vector.

(vector? #(1 2 3))  ; => #t
(vector? '(1 2 3))  ; => #f

Re-exported from (sigil core)

make-vectorprocedure

Create 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-lengthprocedure

Return the number of elements in the vector.

(vector-length #(a b c))  ; => 3
(vector-length #())       ; => 0

Re-exported from (sigil core)

vector-refprocedure

Return the element at position k in the vector.

(vector-ref #(a b c) 0)  ; => a
(vector-ref #(a b c) 2)  ; => c

The index must be a non-negative integer less than the vector length.

Re-exported from (sigil core)

vector-set!procedure

Set 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->listprocedure

Convert 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->vectorprocedure

Convert a list to a vector. (list->vector lst) -> vector

Re-exported from (sigil core)

vector-copyprocedure

Copy 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!procedure

Fill 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)

Apply 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-mapprocedure

Apply 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?procedure

Test if a value is a bytevector.

(bytevector? #u8(1 2 3))  ; => #t
(bytevector? "string")    ; => #f

Re-exported from (sigil core)

Create 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)

Return the number of bytes in the bytevector.

(bytevector-length #u8(1 2 3))  ; => 3

Re-exported from (sigil core)

Return the byte at position k in the bytevector.

(bytevector-u8-ref #u8(10 20 30) 1)  ; => 20

Re-exported from (sigil core)

Set 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)

Copy 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)

Append multiple bytevectors. (bytevector-append #u8(1 2) #u8(3 4)) => #u8(1 2 3 4)

Re-exported from (sigil core)

utf8->stringvariable

Re-exported from (sigil io)

string->utf8variable

Re-exported from (sigil io)

procedure?procedure

Test if a value is a procedure.

(procedure? +)              ; => #t
(procedure? (lambda (x) x)) ; => #t
(procedure? 42)             ; => #f

Re-exported from (sigil core)

applyprocedure

Apply 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)

valuesprocedure

Return multiple values.

(values 1 2 3)  ; returns 3 values
(call-with-values (lambda () (values 1 2))
                  (lambda (a b) (+ a b)))  ; => 3

Re-exported from (sigil core)

Call producer with no args, pass its values to consumer.

(call-with-values
  (lambda () (values 1 2))
  (lambda (a b) (+ a b)))  ; => 3

Re-exported from (sigil core)

errorprocedure

Convenience function to raise an error exception. (error "message" irritant ...)

Re-exported from (sigil core)

raiseprocedure

Raise an exception (non-continuable). Aborts to the nearest exception handler.

Re-exported from (sigil core)

Raise a continuable exception. The handler can return a value to continue execution.

Re-exported from (sigil core)

Install 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?variable

Re-exported from (sigil core)

output-port?variable

Re-exported from (sigil core)

port?variable

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

close-portvariable

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

port-open?variable

Re-exported from (sigil core)

Create 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)

Create 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 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-charprocedure

Read 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)  ; => #\e

Re-exported from (sigil io)

peek-charprocedure

Look 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-lineprocedure

Read 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-charprocedure

Write a single character to a port.

(write-char #\A port)
(write-char #\newline port)

Re-exported from (sigil io)

write-stringprocedure

Write 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)

newlineprocedure

Write a newline to a port (default: current output port).

(newline)  ; prints a newline character

Re-exported from (sigil core)

eof-object?procedure

Test if a value is the end-of-file object.

(eof-object? (eof-object))  ; => #t
(eof-object? #f)            ; => #f

Re-exported from (sigil core)

call-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)

Create a parameter object (dynamic variable).

(define my-param (make-parameter 10))
(my-param)        ; => 10
(my-param 20)     ; set to 20
(my-param)        ; => 20

Re-exported from (sigil core)

Re-exported from (srfi srfi-9)