Skip to content

Commit

Permalink
fix bug due to greedy evaluation of code
Browse files Browse the repository at this point in the history
  • Loading branch information
s-kybound committed Apr 14, 2024
1 parent b59df36 commit d83ffc7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/stdlib/__tests__/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ test("lcm", () => {
expect(base.lcm(makeInteger(0), makeInteger(13))).toEqual(makeInteger(0));
expect(base.lcm(makeInteger(4), makeInteger(-2))).toEqual(makeInteger(4));
});

test("fold works as expected", () => {
expect(
base.fold(
(acc: any, x: any) => x,
makeInteger(0),
base.list(makeInteger(1), makeInteger(2), makeInteger(3)),
),
).toEqual(makeInteger(3));
});
15 changes: 5 additions & 10 deletions src/stdlib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,9 @@ export const map: Function = (
...rest: core.List[]
) => {
const all_lists = [xs, ...rest];
const all_elements = (all_lists as any).map(car);
const all_tails = (all_lists as any).map(cdr);
return any(null$63$, core.vector$45$$62$list(all_lists))
? null
: cons(f(...all_elements), map(f, ...all_tails));
: cons(f(...all_lists.map(car)), map(f, ...all_lists.map(cdr)));
};

export const fold: Function = (
Expand All @@ -679,11 +677,9 @@ export const fold: Function = (
...rest: core.List[]
) => {
const all_lists = [xs, ...rest];
const all_elements = (all_lists as any).map(car);
const all_tails = (all_lists as any).map(cdr);
return any(null$63$, core.vector$45$$62$list(all_lists))
? acc
: fold(f, f(acc, ...all_elements), ...all_tails);
: fold(f, f(acc, ...all_lists.map(car)), ...all_lists.map(cdr));
};

export const fold$45$left: Function = fold;
Expand All @@ -695,11 +691,9 @@ export const fold$45$right: Function = (
...rest: core.List[]
) => {
const all_lists = [xs, ...rest];
const all_elements = (all_lists as any).map(car);
const all_tails = (all_lists as any).map(cdr);
return any(null$63$, core.vector$45$$62$list(all_lists))
? acc
: f(...all_elements, fold$45$right(f, acc, ...all_tails));
: f(...all_lists.map(car), fold$45$right(f, acc, ...all_lists.map(cdr)));
};

export const reduce: Function = (f: Function, ridentity: any, xs: core.List) =>
Expand Down Expand Up @@ -759,7 +753,8 @@ export const append: Function = (...xss: core.List[]) => {

export const concatenate: Function = (xss: core.List) => apply(append, xss);

export const reverse: Function = (xs: core.List) => fold(cons, null, xs);
export const reverse: Function = (xs: core.List) =>
fold$45$right(cons, null, xs);

export const take: Function = (xs: core.List, i: core.SchemeNumber) => {
if ($61$(i, make_number(0))) {
Expand Down

0 comments on commit d83ffc7

Please sign in to comment.