Skip to content

Commit

Permalink
Update procedure max and min to built-in
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Oct 2, 2023
1 parent 88bf5c6 commit 15faa56
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.15_amd64.deb
sudo apt install build/meevax_0.5.16_amd64.deb
```

or
Expand Down Expand Up @@ -131,9 +131,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.15.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.16.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.15_amd64.deb`
| `package` | Generate debian package `meevax_0.5.16_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.15
0.5.16
29 changes: 1 addition & 28 deletions basis/r4rs.ss
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
exact? inexact?
= < > <= >=
zero? positive? negative? odd? even?
max min
+ * - /
abs
quotient remainder modulo
Expand Down Expand Up @@ -325,34 +326,6 @@
(car alist)
(assoc (cdr alist)))))))

(define (max x . xs) ; Chibi-Scheme
(define (max-aux x xs)
(if (null? xs)
(inexact x)
(max-aux (if (< x (car xs)) (car xs) x)
(cdr xs))))
(if (inexact? x)
(max-aux x xs)
(let rec ((x x) (xs xs))
(cond ((null? xs) x)
((inexact? (car xs)) (max-aux x xs))
(else (rec (if (< x (car xs)) (car xs) x)
(cdr xs)))))))

(define (min x . xs) ; Chibi-Scheme
(define (min-aux x xs)
(if (null? xs)
(inexact x)
(min-aux (if (< (car xs) x) (car xs) x)
(cdr xs))))
(if (inexact? x)
(min-aux x xs)
(let rec ((x x) (xs xs))
(cond ((null? xs) x)
((inexact? (car xs)) (min-aux x xs))
(else (rec (if (< (car xs) x) (car xs) x)
(cdr xs)))))))

(define (gcd . xs) ; Chibi-Scheme
(define (gcd-2 a b)
(if (zero? b)
Expand Down
24 changes: 24 additions & 0 deletions src/kernel/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,30 @@ inline namespace kernel
return is_even(xs[0]);
});

library.define<function>("max", [](let const& xs)
{
if (auto iter = std::max_element(xs.begin(), xs.end(), less_than); iter != xs.end())
{
return std::any_of(xs.begin(), xs.end(), is_inexact) ? inexact(*iter) : *iter;
}
else
{
throw error(make<string>("procedure max requires at least one argument"));
}
});

library.define<function>("min", [](let const& xs)
{
if (auto iter = std::min_element(xs.begin(), xs.end(), less_than); iter != xs.end())
{
return std::any_of(xs.begin(), xs.end(), is_inexact) ? inexact(*iter) : *iter;
}
else
{
throw error(make<string>("procedure min requires at least one argument"));
}
});

library.define<function>("+", [](let const& xs)
{
return std::accumulate(std::begin(xs), std::end(xs), e0, std::plus());
Expand Down

0 comments on commit 15faa56

Please sign in to comment.