main.scm
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; https://gist.github.com/stoitsev/1628616

(define (delay x)
  (lambda () x)
  )

(define (force x)
  (x)
)

(define (cons-stream x y)
  (cons x (delay y))
)

(define (head stream)
  (car stream)
)

(define (tail stream)
  (force (cdr stream))
)

(define empty-stream '())

(define empty-stream? null?)


(define (append-streams str1 str2)
  (if (empty-stream? s1) s2
    (cons-stream (head str1) (append-stream (tail str1)str2))
  )
)

(define (map-streams proc stream)
  (if(empty-stram? stream) the-empty-stream
     (cons-stream (proc (head stream)(map-stream proc (tail stream))))
   )
)

(define (filter-stream pred? stream)
  (cond ((empty-stream? stream) the-empty-stream)
        ((pred? (head stream))
        (cons-stream (head stream)(filter-stream pred? (tail stream))))
       (else (filter-stream pred? (tail stream))) 
  )
)

(define (add-streams str1 str2)
  (cons-stream (+ (head str1 )(head str2))
               (add-stream (tail str1)
                           (tail str2))
  )
)

(define ones (cons-stream 1 ones))

(define integers (cons-stream 1 (add-streams ones integers)))

(define (divisible? x y) (= (remainder x y) 0))

(define odds (filter-stream (lambda (x) (not (divisible? x 2))) integers))

;;;https://mitpress.mit.edu/sicp/full-text/sicp/book/node71.html
;;;https://scheme.cs61a.org/ runtime is cool

;;; second try form here: https://github.com/adamniazi/scheme-lisp-prolog-assignments/blob/master/Assignment3/q3.rkt


;Part 1 - code taken from COMP3007 website
;-----------------------------------
(define-syntax cons-stream
  (syntax-rules ()
    ((cons-stream a b)(cons a (delay b)))))

(define (stream-car s)(car s))

(define (stream-cdr s)(force (cdr s)))

(define (integers-starting-from n)
  (cons-stream n (integers-starting-from (+ n 1))))

(define integers (integers-starting-from 1))

(define (stream-null? stream ) 
     (null? stream))

(define the-empty-stream '())

(define (divisible? x y) (= (remainder x y) 0))

;Part 2 - Stream analogues of list processing functions
;-----------------------------------

;returns the nth value for the stream str
(define (stream-ref str n)
  (cond ((= n 0) (stream-car str))
        (else (stream-ref (stream-cdr str) (- n 1)))))

;returns a filtered stream satisfying the predicate, from Prof. Runka's website
(define (stream-filter predicate stream) 
     (cond  ((stream-null? stream) the-empty-stream) 
                  ((predicate (stream-car stream)) 
                           (cons-stream (stream-car stream) 
                                                (stream-filter predicate (stream-cdr stream)))) 
                  (else  (stream-filter predicate (stream-cdr stream)))))

;applies proc to each element of str, from Prof. Runka's website
(define (stream-map procedure stream )
  (if (stream-null? stream )
      the-empty-stream
      (cons-stream (procedure (stream-car stream ))
                   (stream-map procedure (stream-cdr stream )))))

;makes a new stream of the first n items in str
(define (first n str)
  (cond ((<= n 0) '())
        (else (cons-stream (stream-car str) (first (- n 1) (stream-cdr str))))))

;makes a stream from list lis
(define (list->stream lis)
  (cond ((null? (car lis)) '())
         (else (cons-stream (car lis) (list->stream (cdr lis))))))

;makes a list from a stream str
(define (stream->list str)
  (cond ((null? str) '())
        (else (cons (stream-car str) (stream->list (stream-cdr str))))))
;-----------------------------------

;Part 3 - Stream Generators to test Part 2 functions
;-----------------------------------

;infinite list of 1
(define (infinite-ones)
  (cons-stream 1 (infinite-ones)))

;an infinite stream of all odd integers
(define infinite-odds (stream-filter (lambda (x) (not (divisible? x 2))) integers))

;scm> integers
;(1 . #[promise (not forced)])
;scm> (define infinite-evens (stream-map (lambda (x) (+ x 1)) infinite-odds))
;infinite-evens
;scm> infinite-evens
;(2 . #[promise (not forced)])
BiwaScheme Interpreter version 0.6.4 Copyright (C) 2007-2014 Yutaka HARA and the BiwaScheme team