Ver Fonte

first examples on racket

Andrea Gus há 9 anos atrás
commit
6257baa8e1
2 ficheiros alterados com 92 adições e 0 exclusões
  1. 29 0
      scheme/scheme1.rkt
  2. 63 0
      scheme/scheme2.rkt

+ 29 - 0
scheme/scheme1.rkt

@@ -0,0 +1,29 @@
+#lang racket
+(let ((x 2)
+      (y 3))
+  (let ((f (lambda () (+ (* x x) (* y y)))))
+    (let ((x 5)
+          (y 7))
+     (f))))
+
+(let ((x 1)
+      (y 2))
+  (let ((x y)
+        (y x))
+    (begin
+      (display x)
+      (newline)
+      (display y)
+      (newline))))
+
+(let ((x (quasiquote (+ 1 (unquote (+ 1 1)) 3))))
+  x)
+
+(begin
+  (define x 23)
+  (set! x 42)
+  x)
+
+(define (k . j) j)
+(k 1 2 3)
+(apply + '(1 2 3))

+ 63 - 0
scheme/scheme2.rkt

@@ -0,0 +1,63 @@
+#lang racket
+(define (minimum L)
+  (let ((x (car L))
+        (xs (cdr L)))
+        (if (null? xs)
+            x
+            (minimum
+             (cons
+              (if (< x (car xs))
+              x
+              (car xs))
+              (cdr xs))))))
+(minimum '(2 2 1 4 5))
+
+(define (minimum2 x . rest)
+  (if (null? rest)
+      x
+      (apply minimum2
+       (cons
+        (if (< x (car rest))
+            x
+            (car rest))
+            (cdr rest)))))
+(minimum2 5 9 7 88 54 79)
+
+(let ((x 0))
+  (let label ()
+    (when (< x 10)
+      (display x)
+      (newline)
+      (set! x (+ x 1))
+      (label))))
+
+(let label ((x 10))
+  (when (> x 0)
+    (display x)
+    (newline)
+    (label (- x 1))))
+
+(define (fact x)
+  (define (fact-tail x accum)
+    (if (= x 0)
+        accum
+        (fact-tail (- x 1) (* accum x))))
+  (fact-tail x 1))
+(fact 10)
+
+(for-each (lambda (x)
+            (display x) (newline))
+          '(this is it))
+
+(define (vector-for-each body vect)
+  (let ((max (- (vector-length vect) 1)))
+    (let loop ((i 0))
+      (body (vector-ref vect i))
+      (when (< i max)
+        (loop (+ i 1))))))
+
+(vector-for-each (lambda (x)
+                   (display x) (newline))
+                 #(this is it))
+
+