Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in? in (liii base) #25

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading