summaryrefslogtreecommitdiff
path: root/depre/main.scm
blob: 37a3b35143464b58e1bd92e2b243e29b58788a2b (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
53
54
55
56
57
; https://xn--rpa.cc/irl/term.html
(define-module (depre main))

(define %depression 50)
(define %tiredness  50)

(define (nth n lst)
  (if (> n (length lst))
    (error "Chosen element longer than provided list")
    (let loop ((lst lst)
               (n n))
    (if (= 1 n)
      (car lst)
      (loop (cdr lst) (- n 1))))))

(define (combine . functions)
  "Calls given functions in given order."
  (lambda ()
    (let loop ((fs functions))
      (when (not (null? fs))
        ((car fs))
        (loop (cdr fs))))))

(define (answer message)
  (lambda ()
    (display message)
    (newline)))

(define (state heading menu)
  (lambda ()
    (newline)
    (display heading)
    (newline)
    (map (lambda (opt num)
           (display (string-append "  " (number->string num) "- " (car opt)))
           (newline))
         menu (iota (length menu) 1))
    (display "You choose >  ")
    (let loop ((chosen (read)))
      (if (and (number? chosen) (<= chosen (length menu)))
        ((cdr (nth chosen menu)))
        (begin
          (display "Wrong answer. Try again >  ")
          (loop (read)))))))

(define start
  (state "Hi, game starts"
         (list (cons "Start game"
                     (combine
                       (answer "Let's start, then.")
                       (state "heading"
                              (list (cons "hola"  (lambda () (display "HOLA")))
                                    (cons "adios" (lambda () (display "adios")))))))
               (cons "Quit game"
                     (answer "Good bye!")))))

(start)