|
SketchyLISP Reference |
Copyright (C) 2007 Nils M Holm |
| <<[substitute] | [Index] | [sum]>> |
Conformance: R5RS Scheme
Purpose: Extract a substring from a string. The extracted string begins at the position start and extends to (but does not include) the char at the position end. The assertion 0<=start<=end must hold.
Arguments:
STR - string
START - position of first char to extract
END - position of first char not to extract
Model:
(define (substring str start end)
(letrec
((skip
(lambda (lst n)
(cond ((zero? n) lst)
((null? lst)
(bottom '(bad start in substring)))
(else (skip (cdr lst) (- n 1))))))
(extract
(lambda (lst n res)
(cond ((zero? n) (reverse res))
((null? lst)
(bottom '(bad end in substring)))
(else (extract (cdr lst) (- n 1)
(cons (car lst) res)))))))
(cond ((< end start)
(bottom '(bad range in substring)))
(else (list->string
(extract (skip (string->list str) start)
(- end start) '()))))))
Implementation:
; This function is a primitive function.
Example:
(substring "abcdefxyz" 3 6) => "def"
See also:
string,
string-length,
string-ref,
string-append.
| <<[substitute] | [Index] | [sum]>> |