scheme2.rkt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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))
  55. (eq? 'casa 'casa)
  56. (eq? "casa" "casa")
  57. (eqv? 2 2)
  58. (make-vector 5 'a)
  59. (equal? (make-vector 5 'a) (make-vector 5 'a))
  60. (equal? (make-vector 5 'a) (make-vector 5 'b))
  61. (eqv? (make-vector 5 'a) (make-vector 5 'a))
  62. (case (car(cdr '(c a)))
  63. ((a e i o u) 'vowel)
  64. ((w y) 'semivowel)
  65. (else 'consonant))
  66. (cond ((> 3 2) 'greater)
  67. ((< 1 3) 'less)
  68. (else 'equal))
  69. (define (f)
  70. (vector 1 2 3))
  71. (define v (f))
  72. (vector-set! v 0 20)
  73. (display v)
  74. (struct being (
  75. name
  76. (age #:mutable)
  77. ))
  78. (define (being-show x)
  79. (newline)
  80. (display (being-name x))
  81. (newline)
  82. (display (being-age x)))
  83. (define james (being "James" 30))
  84. (being-show james)
  85. (set-being-age! james 40)
  86. (being-show james)
  87. (define (say-hello x)
  88. (if (being? x)
  89. (begin
  90. (being-show x)
  91. (newline)
  92. (display "regards")
  93. )
  94. (error "not a being" x)))
  95. (say-hello james)
  96. (struct may-being being
  97. ((alive? #:mutable)))
  98. (define john (may-being "John" 87 #t))
  99. (say-hello john)