From d83ffc7b5849d8ff4fd95c3132aac3462a616908 Mon Sep 17 00:00:00 2001 From: s-kybound Date: Mon, 15 Apr 2024 03:44:09 +0800 Subject: [PATCH] fix bug due to greedy evaluation of code --- src/stdlib/__tests__/base.ts | 10 ++++++++++ src/stdlib/base.ts | 15 +++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/stdlib/__tests__/base.ts b/src/stdlib/__tests__/base.ts index 70a9500..acc3599 100644 --- a/src/stdlib/__tests__/base.ts +++ b/src/stdlib/__tests__/base.ts @@ -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)); +}); diff --git a/src/stdlib/base.ts b/src/stdlib/base.ts index 5efe5da..7295aa5 100644 --- a/src/stdlib/base.ts +++ b/src/stdlib/base.ts @@ -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 = ( @@ -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; @@ -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) => @@ -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))) {