From 2acd572e8e78259abcd3312bbfb329f9b9a2e3bf Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Sat, 3 Feb 2024 15:14:16 -0400 Subject: [PATCH] Add rainbow test script --- Rainbow/rainbow.js | 37 +++++++++++++++++++++++++++++++++++++ Rainbow/rainbow.rkt | 17 +++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Rainbow/rainbow.js create mode 100644 Rainbow/rainbow.rkt diff --git a/Rainbow/rainbow.js b/Rainbow/rainbow.js new file mode 100644 index 0000000..9226ba9 --- /dev/null +++ b/Rainbow/rainbow.js @@ -0,0 +1,37 @@ +// Approximate racket functions +function circle(color) { + print(`${color} circle`) +} +function square(color) { + print(`${color} square`) +} +function print(shape) { + console.log(shape) +} +// Cuz javascript doesn't have functional primitives +function first(list) { + return list[0] +} +function rest(list) { + return list.slice(1) +} + +// Helper to cycle through the functions +function cycle(list) { + return rest(list).concat(first(list)) +} + +// The actual function +function rainbow(...functions) { + let colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] + function rainbowRecursive(functions, colors) { + if (colors.length === 0) { + return + } + first(functions)(first(colors)) + rainbowRecursive(cycle(functions), rest(colors)) + } + rainbowRecursive(functions, colors) +} + +rainbow(circle, square) \ No newline at end of file diff --git a/Rainbow/rainbow.rkt b/Rainbow/rainbow.rkt new file mode 100644 index 0000000..3fc1b42 --- /dev/null +++ b/Rainbow/rainbow.rkt @@ -0,0 +1,17 @@ +#lang racket +(require pict) +(require pict/color) +; The actual function +(define (rainbow . shapes) + (define colors (list red orange yellow green blue purple)) + (define (cycle lst) (append (rest lst) (list (first lst)))) + (define (rainbow-recursive shapes colors) + (cond + [(empty? colors) (void)] + [(print((first colors) (first shapes))) + (rainbow-recursive (cycle shapes) (rest colors))] + ) + ) + (rainbow-recursive shapes colors)) +; Example usage +(rainbow (filled-rectangle 25 25) (filled-ellipse 25 25)) \ No newline at end of file