#lang racket (define (dbl x) (* x 2)) (define (inc y) (+ y 1)) (define (sq z) (* z z)) (define (o f g) (λ (x) (f (g x)))) (define id (λ (x) x)) (define (curry2 binop) (λ (x) (λ (y) (binop x y)))) (define (uncurry2 curried-binop) (λ (x y) ((curried-binop x) y))) (define (uncurry3 curried-ternop) (λ (x y z) (((curried-ternop x) y) z))) (define map-scale (uncurry2 (o (curry2 map) (curry2 *)))) (define map-cons (uncurry2 (o (curry2 map) (curry2 cons)))) (define (divisible-by? num factor) (= (remainder num factor) 0)) (define (ssm35-monolithic-count-down n) (if (= n 0) 0 (if (or (divisible-by? n 3) (divisible-by? n 5)) (+ (* n n) (ssm35-monolithic-count-down (- n 1))) (ssm35-monolithic-count-down (- n 1))))) (define (ssm35-monolithic-count-up n) (define (helper num) (if (> num n) 0 (if (or (divisible-by? num 3) (divisible-by? num 5)) (+ (* num num) (helper (+ num 1))) (helper (+ num 1))))) (helper 1)) (define (ssm35-holo n) (foldr + 0 (map (λ (x) (* x x)) (filter (λ (num) (or (divisible-by? num 3) (divisible-by? num 5))) (range 1 (+ n 1)))))) (define ssm35-compose (o (λ (squares) (foldr + 0 squares)) (o (λ (filtered-nums) (map (λ (x) (* x x)) filtered-nums)) (o (λ (nums) (filter (λ (num) (or (divisible-by? num 3) (divisible-by? num 5))) nums)) (o (λ (hi) (range 1 hi)) inc))))) (define (o-list funlist) (foldr o id funlist)) (define ssm35-compose-list (o-list (list (λ (squares) (foldr + 0 squares)) (λ (filtered-nums) (map (λ (x) (* x x)) filtered-nums)) (λ (nums) (filter (λ (num) (or (divisible-by? num 3) (divisible-by? num 5))) nums)) (λ (hi) (range 1 hi)) inc))) (define app-5 (λ (f) (f 5))) (define (triple a b c) (list a b c)) (define (dup-arg curried-binop) (λ (x) ((curried-binop x) x))) (define (o-and f g) (λ (x) (and (f x) (g x)))) (define (o-or f g) (λ (x) (or (f x) (g x)))) (define (flip2 binop) (λ (x y) (binop y x))) (define ssm35-no-lambdas (o-list (list (curry foldr + 0) ((curry2 map) (dup-arg (curry2 *))) ((curry2 filter) (o-or ((curry2 (flip2 divisible-by?)) 3) ((curry2 (flip2 divisible-by?)) 5))) ((curry2 range) 1) ((curry2 +) 1))))