write-to-file str filename
Writes a string to a file.
nth-pair lst n
Returns the n-th pair of a list.
list-set! lst n elt
Destructively modifies a list element at the given index.
capture-output program arguments
Runs the given program with the supplied arguments and returns the output as a string. Example:
(capture-output "date" '("--universal")) "Tue Jan 25 20:34:43 UTC 2011\n"
take-until pred lst
Splits a list into two parts: head elements for which the predicate does *not* hold, and the rest of the list. For example:
(take-until even? '(1 3 5 1 2 3 4 5 6 7 8)) ((1 3 5 1) 2 3 4 5 6 7 8)
member-of lst
For a given list, returns a function which given an element checks if the element is in the list.
split-at delimiters lst
Splits a list at delimiters. Empty splits resulting from multiple delimiters are ignored. Example:
(split-at '(split space) '(1 2 split space 3 split 4 space 5 split space split)) ((1 2) (3) (4) (5))
tokenize delimiters str
Splits a string at delimiters. Equivalent to split-at
, if the string is viewed as a list of characters. Example:
(tokenize (string->list " ,!") "Hello, world!") ("Hello" "world")
fast-string-append {string}
Uses a string output port to concatenate many strings. May be faster than generic string-append, although the performance has not been tested.
default-on-error default thunk
Executes a thunk under an error handler and returns the supplied value if an error occurs. For example:
(default-on-error 'error (lambda () (/ 1 0))) ;Value: error (default-on-error 'error (lambda () (/ 1 10))) ;Value: 1/10
flatten tree
Flattens a tree. If the tree is an atom, flatten
is equivalent to the identity function.
(flatten '(((1 2 ((3))) 4) (((5))))) (1 2 3 4 5) (flatten 1) 1
tree-fold-right func init tree
Folds a tree from the right. Equivalent to flattening the tree first and then calling fold-right on the result.
(tree-fold-right (lambda (sofar elt) (+ sofar elt)) 0 '(((1 2 ((3))) 4) (((5))))) (1 2 3 4 5)
compose {g}
Composes multiple functions, and returns the composed function.
(map (compose even? (lambda (x) (* 3 x))) '(1 2 3 4 5)) (#f #t #f #t #f)
curry {arguments}
Curries a function with the supplied arguments.
(map (curry + 1 2) '(1 2 3 4 5)) (4 5 6 7 8)
identity x
The one-argument identity function.
intersperse elt lst
Inserts the given element between any two consecutive elements of a list
(intersperse 'a '(b c d)) (b a c a d)
expand-map func tree
An extension of map
:
expand-map
can be used to recursively list files and directories: (define (list-files pathname) (define (expand filename) (if (and (file-directory? filename) (not (member (pathname-name filename) '("." "..")))) (directory-read filename) filename)) (flatten (expand-map expand pathname)))
unlazy-ref n
Creates a list-ref getter which automatically forces promises, writing the result back to the list.
(define lst (list 10 (delay 20))) ;Value: lst lst ;Value: (10 #[promise 22743]) ((unlazy-ref 1) lst) ;Value: 20 lst ;Value: (10 20)
list-accessor index
Returns a procedure which given a list, calls list-ref with the specified index as its argument.
lift f g
Creates a new function which first applies g
to all arguments and then calls f
. For example, here is how lift
can be used to "lift" the <
operator to work on string lengths:
((lift < string-length) "a" "aa") #t
make-set-ref n
Creates a function that can be used to set the n-th element of a list.
apply-if-not-null thing f
Checks whether an object is not null, and applies the given function. Returns false if the object is null.
constant val
Creates an n-ary procedure which always returns the given value.
hash-table/has-key? hash-table key
Checks whether a hash table has the given key.
undefined
Throws an error saying that the given piece of functionality has not yet been implemented.
t? tag [optional: obj]
If called with one argument, returns a procedure that checks whether a list is tagged with the given symbol, or performs the check if called with two arguments.
tag t f
Returns a new procedure which tags the result of another one.
(define doubler (tag 'double (curry * 2))) ;Value: doubler (doubler 21) ;Value: (double 42)
untag f
Returns a function which untags the result returned by another one.
(doubler 21) ;Value: (double 42) ((untag doubler) 21) ;Value: 42
identity? val
Returns a predicate that checks for equality with the supplied value.
prompt message
Prompts a user for input using stdin.
prompt-list messages
Like prompt
, but for multiple inputs which are returned as a list.
(prompt-list '("first name" "last name")) first name: Alyssa last name: Hacker ;Value 22747: ("Alyssa" "Hacker")
safe-car x
Same as car, but returns #f if the argument is #f.
set-union lst1 lst2
Calculates a union of two sets, without duplicates.
(set-union '(1 2 3) '(4 5 6)) ;Value 22748: (6 5 4 3 2 1) (set-union '(1 2 3) '(1 2 3 4 5 6)) ;Value 22749: (6 5 4 3 2 1)
no-dups lst
Removes duplicates from a list
for-any? pred lst
Checks if a predicate holds for at least one element in a list.
sum lst
Sums all elements of a list, which must all be numbers.
range lo hi
Returns a sorted list of all integers from lo
inclusive to hi
exclusive. Returns an empty list if lo
>= hi
.
make-vector-table rows cols default-value
Creates a two-dimensional rectangular vector array (a table), initializing all entries to default-value
.
table-ref table r c
References a table element at row r
and column c
table-set! table r c val
Sets a table element at row r
and column c
levenshtein-distance s t
Computes the edit distance between two strings. Implemented based on the Wikipedia article.
improper-map f val
Like map, but works on improper lists. Returns f(val)
if the argument is an atom, and ()
if it's an empty list
inspect
Pretty prints an object and its value before returning it. Useful for debugging.
(inspect ((lambda (a b) (* a b)) 3 4)) ((lambda (a b) (* a b)) 3 4): 12 ;Value: 12
default
Scheme Power Tools Documentation
(c) Maciej Pacula 2010-2011
http://mpacula.com