Skip to content

Commit

Permalink
add list-set! and nth-pair functions #269
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 27, 2024
1 parent 0940194 commit 8647f53
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* add `string-for-each`, `string-downcase`, and `string-upcase` from R7RS
* add `typecheck-number` function
* add `char-foldcase` and `string-foldcase` functions
* add `list-set!` and `nth-pair` functions
* add `SRFI-210`
* add `syntax-parameterize` from SRFI-139 to the core [#210](https://github.com/jcubic/lips/issues/210)
### Bugfix
Expand Down
5 changes: 4 additions & 1 deletion dist/std.min.scm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 36 additions & 12 deletions dist/std.scm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/std.xcb
Binary file not shown.
16 changes: 4 additions & 12 deletions lib/R5RS.scm
Original file line number Diff line number Diff line change
Expand Up @@ -577,18 +577,10 @@
"(list-ref list n)

Returns n-th element of a list."
(typecheck "list-ref" l '("pair" "nil"))
(if (< k 0)
(throw (new Error "list-ref: index out of range"))
(let ((l l) (k k))
(while (> k 0)
(if (or (null? (cdr l)) (null? l))
(throw (new Error "list-ref: not enough elements in the list")))
(set! l (cdr l))
(set! k (- k 1)))
(if (null? l)
l
(car l)))))
(let ((l (%nth-pair "list-ref" l k)))
(if (null? l)
l
(car l))))

;; -----------------------------------------------------------------------------
(define (not x)
Expand Down
8 changes: 8 additions & 0 deletions lib/R7RS.scm
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
The macro work similar to let* but variable is list of values and value
need to evaluate to result of calling values.")

;; -----------------------------------------------------------------------------
;; R7RS division operators (Gauche Scheme) BSD license
;; Copyright (c) 2000-2020 Shiro Kawai <[email protected]>
Expand Down Expand Up @@ -1452,6 +1453,13 @@
obj
(obj.clone false)))

;; -----------------------------------------------------------------------------
(define (list-set! l k obj)
"(list-set! list n)

Returns n-th element of a list."
(set-car! (%nth-pair "list-set!" l k) obj))

;; -----------------------------------------------------------------------------
(define-macro (define-record-type name constructor pred . fields)
"(define-record-type name constructor pred . fields)
Expand Down
24 changes: 24 additions & 0 deletions lib/bootstrap.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,30 @@
(define string-join join)
(define string-split split)

;; -----------------------------------------------------------------------------
(define (%nth-pair msg l k)
"(%nth-pair msg list n)
Returns nth pair of a list with given message on error."
(typecheck msg l '("pair" "nil"))
(typecheck-number msg k '("integer" "bigint"))
(if (< k 0)
(throw (new Error (string-append msg ": index out of range")))
(let ((l l) (k k))
(while (> k 0)
(if (or (null? (cdr l)) (null? l))
(throw (new Error (string-append msg ": not enough elements in the list"))))
(set! l (cdr l))
(set! k (- k 1)))
l)))

;; -----------------------------------------------------------------------------
(define (nth-pair l k)
"(nth-pair list n)

Returns nth pair of a list."
(%nth-pair "nth-pair" l k))

;; -----------------------------------------------------------------------------
(define (symbol-append . rest)
"(symbol-append s1 s2 ...)
Expand Down

0 comments on commit 8647f53

Please sign in to comment.