aboutsummaryrefslogtreecommitdiff
path: root/_pastebins/2021-09-02-sicp-exercise-3-19.md
blob: 75ee3460cac847413de1052b10bc5555e62b409d (about) (plain) (blame)
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
---

title: SICP exercise 3.19

date: 2021-09-02

layout: post

lang: en

ref: sicp-exercise-3-19

---

```scheme
(define (cycle? l)
  (define (rec l x)
    (cond
      ((null? x) false)
      ((eq? l x) true)
      (true (rec l (cdr x)))))
  (rec l (cdr l)))
```

Sample interactive session:

```scheme
scheme@(guile-user)> (define true #t)
scheme@(guile-user)> (define false #f)
scheme@(guile-user)>
(define (cycle? l)
  (define (rec l x)
    (cond
      ((null? x) false)
      ((eq? l x) true)
      (true (rec l (cdr x)))))
  (rec l (cdr l)))
scheme@(guile-user)> (cycle? '(1 2 3))
$9 = #f
scheme@(guile-user)> (cycle? (make-cycle '(1 2 3)))
$10 = #t
```