scheme2.rkt 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #lang racket
  2. (define (minimum L)
  3. (let ((x (car L))
  4. (xs (cdr L)))
  5. (if (null? xs)
  6. x
  7. (minimum
  8. (cons
  9. (if (< x (car xs))
  10. x
  11. (car xs))
  12. (cdr xs))))))
  13. (minimum '(2 2 1 4 5))
  14. (define (minimum2 x . rest)
  15. (if (null? rest)
  16. x
  17. (apply minimum2
  18. (cons
  19. (if (< x (car rest))
  20. x
  21. (car rest))
  22. (cdr rest)))))
  23. (minimum2 5 9 7 88 54 79)
  24. (let ((x 0))
  25. (let label ()
  26. (when (< x 10)
  27. (display x)
  28. (newline)
  29. (set! x (+ x 1))
  30. (label))))
  31. (let label ((x 10))
  32. (when (> x 0)
  33. (display x)
  34. (newline)
  35. (label (- x 1))))
  36. (define (fact x)
  37. (define (fact-tail x accum)
  38. (if (= x 0)
  39. accum
  40. (fact-tail (- x 1) (* accum x))))
  41. (fact-tail x 1))
  42. (fact 10)
  43. (for-each (lambda (x)
  44. (display x) (newline))
  45. '(this is it))
  46. (define (vector-for-each body vect)
  47. (let ((max (- (vector-length vect) 1)))
  48. (let loop ((i 0))
  49. (body (vector-ref vect i))
  50. (when (< i max)
  51. (loop (+ i 1))))))
  52. (vector-for-each (lambda (x)
  53. (display x) (newline))
  54. #(this is it))