fork download
  1. ; three homework problems
  2.  
  3. (define (sum-odd n)
  4. (if (zero? n) 0
  5. (+ (sum-odd (- n 1)) n n -1)))
  6.  
  7. (display (sum-odd 5)) (newline)
  8.  
  9. (define (sum-odd-aux n sum)
  10. (if (zero? n) sum
  11. (sum-odd-aux (- n 1) (+ sum n n -1))))
  12.  
  13. (define (sum-odd n)
  14. (sum-odd-aux n 0))
  15.  
  16. (display (sum-odd 5)) (newline)
  17.  
  18. (define (sum-odd n) (* n n))
  19.  
  20. (display (sum-odd 5)) (newline)
  21.  
  22. (define (freq str)
  23. (let ((counts (make-vector 256 0)))
  24. (do ((i 0 (+ i 1)))
  25. ((= i (string-length str)))
  26. (let ((c (char->integer (string-ref str i))))
  27. (vector-set! counts c (+ (vector-ref counts c) 1))))
  28. (do ((i 0 (+ i 1))) ((= 256 i))
  29. (when (positive? (vector-ref counts i))
  30. (display (integer->char i)) (display #\tab)
  31. (display (vector-ref counts i)) (newline)))))
  32.  
  33. (freq "hello")
  34.  
  35. (define (uniq-c eql? xs)
  36. (if (null? xs) xs
  37. (let loop ((xs (cdr xs)) (prev (car xs)) (k 1) (result '()))
  38. (cond ((null? xs) (reverse (cons (cons prev k) result)))
  39. ((eql? (car xs) prev) (loop (cdr xs) prev (+ k 1) result))
  40. (else (loop (cdr xs) (car xs) 1 (cons (cons prev k) result)))))))
  41.  
  42. (define (freq str)
  43. (uniq-c char=? (sort (string->list str) char<?)))
  44.  
  45. (display (freq "hello")) (newline)
  46.  
  47. (define (caesar n str)
  48. (define (rotate c)
  49. (if (not (and (char<=? #\A c) (char<=? c #\Z))) c
  50. (integer->char (+ (modulo (+ (char->integer c) n -65) 26) 65))))
  51. (list->string (map rotate (string->list str))))
  52.  
  53. (display (caesar 5 "ATTACK AT DAWN")) (newline)
  54. (display (caesar -5 "FYYFHP FY IFBS")) (newline)
Success #stdin #stdout 0s 7264KB
stdin
Standard input is empty
stdout
25
25
25
e	1
h	1
l	2
o	1
((e . 1) (h . 1) (l . 2) (o . 1))
FYYFHP FY IFBS
ATTACK AT DAWN