Skip to content

Commit

Permalink
in? in (liii base) (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Aug 15, 2024
1 parent 9ca4cad commit 605a701
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion goldfish/liii/base.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
;

(define-library (liii base)
(export == != display*)
(export == != display* in?)
(begin

(define == equal?)
Expand All @@ -26,5 +26,18 @@
(define (display* . params)
(for-each display params))

(define (in? elem l)
(cond ((list? l) (not (not (member elem l))))
((vector? l)
(let loop ((i (- (vector-length l) 1)))
(if (< i 0)
#f
(if (== elem (vector-ref l i))
#t
(loop (- i 1))))))
((and (char? elem) (string? l))
(in? elem (string->list l)))
(else (error 'type-error "type mismatch"))))

) ; end of begin
)
9 changes: 9 additions & 0 deletions tests/liii/base-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
(import (liii check)
(liii base))

(check-set-mode! 'report-failed)

(check (== (list 1 2) (list 1 2)) => #t)
(check (!= (list 1 2) (list 1 2)) => #f)

Expand All @@ -26,4 +28,11 @@
(display* "hello world" "\n")))
=> "hello world\n")

(check (in? 1 (list )) => #f)
(check (in? 1 (list 3 2 1)) => #t)
(check (in? #\x "texmacs") => #t)
(check (in? 1 (vector )) => #f)
(check (in? 1 (vector 3 2 1)) => #t)
(check-catch 'type-error (lambda () (in? 1 "123")))

(check-report)

0 comments on commit 605a701

Please sign in to comment.