#lang racket (module+ test (require rackunit)) (define (my-even? n) (cond [(< n 0) #f] [(= n 0) #t] [(= n 1) #f] [#t (my-even? (- n 2))])) (module+ test (check-eq? (my-even? 3) #f)) (define (copy-str n str) (cond [(< n 1) ""] [#t (string-append str (copy-str (- n 1) str))])) (module+ test (check-equal? (copy-str 3 "abc") "abcabcabc")) (define (copy-str-2 n str [acc ""]) (cond [(< n 1) acc] [#t (copy-str-2 (- n 1) str (string-append str acc))])) (module+ test (check-equal? (copy-str-2 3 "abc") "abcabcabc")) (define (consecutive-chars fst lst [acc '()]) (define (next-char c) (integer->char (add1 (char->integer c)))) (define (prev-char c) (integer->char (sub1 (char->integer c)))) (cond [(char=? fst lst) (list->string (reverse (cons fst acc)))] [(char? fst lst) (consecutive-chars (prev-char fst) lst (cons fst acc))])) (module+ test (check-equal? (consecutive-chars #\a #\d) "abcd") (check-equal? (consecutive-chars #\Z #\A) "ZYXWVUTSRQPONMLKJIHGFEDCBA")) (define (num-of-digits n [acc 0]) (cond [(negative? n) (num-of-digits (- n))] [(< n 10) (add1 acc)] [else (num-of-digits (/ n 10) (add1 acc))])) (module+ test (check-equal? (num-of-digits 123) 3) (check-equal? (num-of-digits -3456) 4)) (define numeric-alphabet "0123456789ABCDEF") (define (num->char n [radix 10]) (string-ref numeric-alphabet (remainder n radix))) (define (num->str n [radix 10] [acc '()]) (cond [(negative? n) (num->str (- n) radix '(#\-))] [(< n radix) (list->string (cons (num->char n radix) acc))] [else (num->str (quotient n radix) radix (cons (num->char n radix) acc))])) (module+ test (check-equal? (num->str 52) "52") (check-equal? (num->str 10 2) "1010") (check-equal? (num->str 255 16) "FF"))