From aa7a034368d146ec94f3dd4c87b9bfbc57bbaca1 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Mon, 2 Feb 2026 22:44:40 -0700 Subject: [PATCH] More stdlib fun --- src/main.ts | 1 + src/stdlib.cg | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/test.cg | 41 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 3aacc27..2b3d904 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,6 +24,7 @@ console.log(ast); const env: Env = new Map(Object.entries(builtins)); const res = evaluate(ast, env); console.log(res); + /* const appRecord = evaluate(ast, env); diff --git a/src/stdlib.cg b/src/stdlib.cg index fd8f575..dd450c2 100644 --- a/src/stdlib.cg +++ b/src/stdlib.cg @@ -1,9 +1,64 @@ +# map : (a \ b) \ List a \ List b map = f list \ list | [] \ [] | [x, ...xs] \ [f x, ...map f xs]; +# filter : (a \ Bool) \ List a \ List a filter = f list \ list | [] \ [] | [x, ...xs] \ (f x | True \ [x, ...filter f xs] | False \ filter f xs); + +# fold : (b \ a \ b) \ b \ List a \ b +fold = f acc list \ list + | [] \ acc + | [x, ...xs] \ fold f (f acc x) xs; + +# reverse : List a \ List a +reverse = list \ list + | [] \ [] + | [x, ...xs] \ (reverse xs) & [x]; + +# take : Int \ List a \ List a +take = n list \ { n = n, list = list } + | { n = _, list = [] } \ [] + | { n = 0, list = _ } \ [] + | { n = _, list = [x, ...xs] } \ [x, ...take (n - 1) xs]; + +# drop : Int \ List a \ List a +drop = n list \ { n = n, list = list } + | { n = _, list = [] } \ [] + | { n = 0, list = _ } \ list + | { n = _, list = [x, ...xs] } \ drop (n - 1) xs; + +# zipWith = (a \ b \ c) List a \ List b \ List c +zipWith = f l1 l2 \ l1 + | [] \ [] + | [x, ...xs] \ (l2 + | [] \ [] + | [y, ...ys] \ [f x y, ...zipWith f xs ys]); + +# and : Bool \ Bool \ Bool +and = x y \ x + | False \ False + | True \ (y | False \ False | True \ True); + +# or : Bool \ Bool \ Bool +or = x y \ x + | True \ True + | False \ (y | True \ True | False \ False); + +# not = Bool \ Bool +not = x \ x + | True \ False + | False \ True; + +# isSome = maybe \ ... +# isNone = maybe \ ... +# unwrapOr = default maybe \ ... + +# range = start end \ ... # [start, start+1, ..., end-1] +# sum = list \ ... +# any = f list \ ... +# all = f list \ ... diff --git a/src/test.cg b/src/test.cg index 4795b8c..de67e0a 100644 --- a/src/test.cg +++ b/src/test.cg @@ -1,7 +1,44 @@ +list = [1, 2, 3, 4, 5]; + add1 = x \ x + 1; -mapped = map add1 [1, 2, 3, 4, 5]; +mapped = map add1 list; isEven = x \ x % 2 == 0; -filter isEven mapped +sumList = list \ fold add 0 list; + +filtered = filter isEven mapped; + +summed = sumList mapped; + +l = len list; + +reversed = reverse list; + +take3 = take 3 list; + +drop3 = drop 3 list; + +list2 = [6, 7, 8]; + +zipped = zipWith add list list2; + +anded = and True False; + +ored = or True False; + +notted = not False; + +{ + filtered = filtered, + summed = summed, + l = l, + reversed = reversed, + take3 = take3, + drop3 = drop3, + zipped = zipped, + anded = anded, + ored = ored, + notted = notted +}