summaryrefslogtreecommitdiff
path: root/basics.scm
blob: 4c52883cafc40f9a99ae0dedbcdeb08adf1cc156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
;;;; variables
(define my-var 15)
my-var

;; set to something else
(set! my-var 20)

;;;; functions
(define (find value lst)
  (if (pair? lst)
      (begin
        (display (string-append "Looking at: " (number->string (car lst))
                                "\n"))
        (if (equal? (car lst) value)
            (begin (display "Found it!\n")
                   (car lst))
            (find value (cdr lst))))
      #f))

(find 5 '(1 2 3 4 5 6))

;; note that variables and functions exist in the same
;; namespace, and that we can define functions in a
;; slightly more ugly way, but looks a lot like a var
;; declaration:
(define find-x (lambda (value lst)
                 (if (pair? lst)
                     (begin
                       (display (string-append "Looking at: " (number->string (car lst))
                                               "\n"))
                       (if (equal? (car lst) value)
                           (begin (display "Found it!\n")
                                  (car lst))
                           (find value (cdr lst))))
                     #f)))

(find-x 4 '(1 2 3 4 5 6))

;;;; let

;; let as a looping construct
(let find-loop ((value 5)
                (lst '(1 2 3 4 5)))
  (if (pair? lst)
      (begin
        (display (string-append "Looking at: " (number->string (car lst))
                                "\n"))
        (if (equal? (car lst) value)
            (begin (display "Found it!\n")
                   (car lst))
            (find-loop value (cdr lst))))
      #f))