Skip to content

Instantly share code, notes, and snippets.

@microamp
Created July 13, 2020 09:40
Show Gist options
  • Save microamp/dec157cdeed8f8918483638c184a13a2 to your computer and use it in GitHub Desktop.
Save microamp/dec157cdeed8f8918483638c184a13a2 to your computer and use it in GitHub Desktop.
Stop playing games with my funcs
(define (pow1 x y)
(if (zero? y)
1
(* x (pow1 x (- y 1)))))
(define pow2
(lambda (x)
(lambda (y)
(if (zero? y)
1
(* x ((pow2 x) (- y 1)))))))
(define three-to-the (pow2 3))
(define (sum xs)
(if (null? xs)
0
(+ (car xs) (sum (cdr xs)))))
(define (my-append xs ys)
(if (null? xs)
ys
(cons (car xs) (my-append (cdr xs) ys))))
(define (my-map f xs)
(if (null? xs)
null
(cons (f (car xs))
(my-map f (cdr xs)))))
(define (inc x) (+ x 1))
(define (fact n)
(if (= n 1)
1
(* n (fact (- n 1)))))
(define (sum-of-all xs)
(if (null? xs)
0
(if (number? (car xs))
(+ (car xs) (sum-of-all (cdr xs)))
(+ (sum-of-all (car xs)) (sum-of-all (cdr xs))))))
;; (sum-of-all '(1 (2 (3 (4) 5))))
(define (sum-of-all-nums xs)
(if (null? xs)
0
(let [(head (car xs))
(tail (cdr xs))]
(cond [(number? head) (+ head (sum-of-all-nums tail))]
[(list? head) (+ (sum-of-all-nums head) (sum-of-all-nums tail))]
[#t (sum-of-all-nums tail)]))))
;; (sum-of-all-nums '(1 "hello" (2 ("world" (3 (4))))))
;; Everything is truthy except #f
(define (falses xs)
(cond [(null? xs) 0]
[(car xs) (falses (cdr xs))]
[#t (+ 1 (falses (cdr xs)))]))
;; (falses (list "hello" #t "world" #f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment