Akumulator (mi je crknil pred kino siska) in filtrirani akumulator. Vaji 1.31 in 1.32

main
Lio Novelli 2024-06-02 15:31:14 +02:00
parent 717cca9fe7
commit 93c1532164
3 changed files with 123 additions and 31 deletions

View File

@ -3,11 +3,11 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-05-29 sre 21:57 -->
<!-- 2024-06-02 Sun 15:31 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Kompotov programerski bralni krožek #1: SICP</title>
<meta name="author" content="Yuri" />
<meta name="author" content="Lio Novelli" />
<meta name="generator" content="Org Mode" />
<style>
#content { max-width: 60em; margin: auto; }
@ -198,7 +198,7 @@
<body>
<div id="content" class="content">
<h1 class="title">Kompotov programerski bralni krožek #1: SICP</h1>
<div class="footer" id="org25ac69a">
<div class="footer" id="orgb2b54d1">
<p>
Izvorna koda se nahaja na <a href="https://git.kompot.si/programerski-bralni-krozek/sicp">git.kompot.si</a>.
</p>
@ -206,7 +206,7 @@ Izvorna koda se nahaja na <a href="https://git.kompot.si/programerski-bralni-kro
</div>
<p>
Beremo <a href="https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs">Structure and Interpretation of Computer Programs</a>. (<a href="https://web.mit.edu/6.001/6.037/sicp.pdf">prenesi pdf</a>)
Beremo <a href="https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs">Structure and Interpretation of Computer Programs</a>. (<a href="https://web.mit.edu/6.001/6.037/sicp.pdf">prenesi pdf</a> ali <a href="https://melpa.org/#/sicp">melpa paket za Emacs</a>)
</p>
<p>
@ -214,18 +214,31 @@ Za bralni krožek se dogovarjamo prek (javne) mailing liste, na katero se lahko
</p>
<div id="org8e34476" class="figure">
<div id="org118a398" class="figure">
<p><img src="./tb.jpg" alt="tb.jpg" />
</p>
</div>
<div id="outline-container-orga2d4c32" class="outline-2">
<h2 id="orga2d4c32">Srečanja</h2>
<div class="outline-text-2" id="text-orga2d4c32">
<div id="outline-container-org02019a8" class="outline-2">
<h2 id="org02019a8">Srečanja</h2>
<div class="outline-text-2" id="text-org02019a8">
</div>
<div id="outline-container-orgd536e23" class="outline-3">
<h3 id="orgd536e23">#4: sreda, 29. 5. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-orgd536e23">
<div id="outline-container-org7fd9abd" class="outline-3">
<h3 id="org7fd9abd">#5: sreda, 12. 6. 2024 ob 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org7fd9abd">
<p>
<a href="https://dogodki.kompot.si/events/63a4ad96-7cc2-45a3-8560-18a1a28cf9b5">Povezava</a> do najave dogodka.
</p>
<p>
Beremo prvo podpoglavje drugega poglavja Grajenje abstrakcij s podatki.
</p>
</div>
</div>
<div id="outline-container-org6dabd7c" class="outline-3">
<h3 id="org6dabd7c">#4: sreda, 29. 5. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org6dabd7c">
<p>
Dogodek je <a href="https://dogodki.kompot.si/events/0f3df86f-abc0-4725-bb4c-3587262a672c">tukaj</a>.
Preglejujemo naloge iz prvega poglavja.
@ -233,9 +246,9 @@ Preglejujemo naloge iz prvega poglavja.
</div>
</div>
<div id="outline-container-org1d96448" class="outline-3">
<h3 id="org1d96448">#3: četrtek, 16. 5. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org1d96448">
<div id="outline-container-org8097d33" class="outline-3">
<h3 id="org8097d33">#3: četrtek, 16. 5. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org8097d33">
<p>
Kot ponavadi je dogodek na našem <a href="https://dogodki.kompot.si/events/8435c26d-4dbd-4e28-94c2-a237c837865d">mobilizonu</a>.
</p>
@ -247,9 +260,9 @@ Na srečanju smo skupaj reševali vajo 1.29.
</p>
</div>
<div id="outline-container-org32b8976" class="outline-4">
<h4 id="org32b8976">zapiski</h4>
<div class="outline-text-4" id="text-org32b8976">
<div id="outline-container-org15650ba" class="outline-4">
<h4 id="org15650ba">zapiski</h4>
<div class="outline-text-4" id="text-org15650ba">
<ul class="org-ul">
<li><a href="zapiski/sicp-lio.html#MissingReference">lio</a></li>
<li><a href="./zapiski/g1smo/1_3_zapiski_vaje.html">g1smo</a></li>
@ -258,9 +271,9 @@ Na srečanju smo skupaj reševali vajo 1.29.
</div>
</div>
<div id="outline-container-orgf64711d" class="outline-3">
<h3 id="orgf64711d">#2: torek, 30. 4. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-orgf64711d">
<div id="outline-container-org81e1376" class="outline-3">
<h3 id="org81e1376">#2: torek, 30. 4. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org81e1376">
<p>
Dogodek je <a href="https://dogodki.kompot.si/events/8435c26d-4dbd-4e28-94c2-a237c837865d">tukaj</a>.
</p>
@ -270,9 +283,9 @@ Nadaljujemo z branjem poglavja 1.2, Procedures and the processes they generate.
</p>
</div>
<div id="outline-container-orga789869" class="outline-4">
<h4 id="orga789869">zapiski</h4>
<div class="outline-text-4" id="text-orga789869">
<div id="outline-container-orgb685cb5" class="outline-4">
<h4 id="orgb685cb5">zapiski</h4>
<div class="outline-text-4" id="text-orgb685cb5">
<ul class="org-ul">
<li><a href="zapiski/sicp_1_2_tina.md">tina</a> (<a href="zapiski/sicp_1_2_tina.scm">vaje</a>, <a href="zapiski/sicp_sec_1_2.scm">druga varjanta</a>)</li>
<li><a href="zapiski/sicp-lio.html#MissingReference">lio</a></li>
@ -282,9 +295,9 @@ Nadaljujemo z branjem poglavja 1.2, Procedures and the processes they generate.
</div>
</div>
<div id="outline-container-org4c6a494" class="outline-3">
<h3 id="org4c6a494">#1: sreda, 17. 4. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org4c6a494">
<div id="outline-container-org643d31a" class="outline-3">
<h3 id="org643d31a">#1: sreda, 17. 4. 2024 18.45 v CTK</h3>
<div class="outline-text-3" id="text-org643d31a">
<p>
Beremo uvod in do poglavja 1.2, približno 30 strani.
</p>
@ -294,9 +307,9 @@ Link do <a href="https://dogodki.kompot.si/events/76672075-1c6f-48f1-bc37-05dde9
</p>
</div>
<div id="outline-container-orga35156e" class="outline-4">
<h4 id="orga35156e">zapiski</h4>
<div class="outline-text-4" id="text-orga35156e">
<div id="outline-container-org5fc5dba" class="outline-4">
<h4 id="org5fc5dba">zapiski</h4>
<div class="outline-text-4" id="text-org5fc5dba">
<ul class="org-ul">
<li><a href="zapiski/g1smo/1_1_zapiski_vaje.html">g1smo</a></li>
<li><a href="zapiski/sicp-lio.html">lio</a></li>
@ -307,4 +320,4 @@ Link do <a href="https://dogodki.kompot.si/events/76672075-1c6f-48f1-bc37-05dde9
</div>
</div>
</body>
</html>
</html>

View File

@ -7,13 +7,18 @@
Izvorna koda se nahaja na [[https://git.kompot.si/programerski-bralni-krozek/sicp][git.kompot.si]].
#+END_footer
Beremo [[https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs][Structure and Interpretation of Computer Programs]]. ([[https://web.mit.edu/6.001/6.037/sicp.pdf][prenesi pdf]])
Beremo [[https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs][Structure and Interpretation of Computer Programs]]. ([[https://web.mit.edu/6.001/6.037/sicp.pdf][prenesi pdf]] ali [[https://melpa.org/#/sicp][melpa paket za Emacs]])
Za bralni krožek se dogovarjamo prek (javne) mailing liste, na katero se lahko prijavite na https://liste.kompot.si/postorius/lists/bralni.kompot.si/
[[file:./tb.jpg]]
* Srečanja
** #5: sreda, 12. 6. 2024 ob 18.45 v CTK
[[https://dogodki.kompot.si/events/63a4ad96-7cc2-45a3-8560-18a1a28cf9b5][Povezava]] do najave dogodka.
Beremo prvo podpoglavje drugega poglavja Grajenje abstrakcij s podatki.
** #4: sreda, 29. 5. 2024 18.45 v CTK
Dogodek je [[https://dogodki.kompot.si/events/0f3df86f-abc0-4725-bb4c-3587262a672c][tukaj]].
Preglejujemo naloge iz prvega poglavja.

View File

@ -425,6 +425,80 @@ Primer vsote.
pribl-pi-next
n
))
;; excercise 1.32
;; recursive accumulate
(define (accumulate-r combiner null-val term a next b)
;; combiner is a procedure of two arguments.
(if (> a b)
null-val
(combiner (term a) (accumulate-r combiner null-val term (next a) next b))
)
)
(define (sum-combiner t acc)
(+ t acc)
)
(define (sum-a term a next b)
(accumulate-r (lambda (t acc) (+ t acc)) 0 term a next b)
)
(define (prod-a term a next b)
(accumulate-r (lambda (t acc) (* t acc)) 1 term a next b)
)
(define (accumulate-i combiner null-val term a next b)
;; Iterative accumulator.
(define (iter-a a result)
(if (> a b)
result
(iter-a (next a) (combiner (term a) result))
)
)
(iter-a a null-val)
)
(define (identity x) x)
(define (add1 x) (+ x 1))
(define (sum-ai term a next b)
(accumulate-i sum-combiner 0 term a next b)
)
(define (prod-ai term a next b)
(accumulate-i (lambda (t acc) (* t acc)) 1 term a next b))
(define (fakulteta-ai n) (prod-ai identity 2 add1 n))
;; excercise 1.33 filtered accumulate - combine only those term derived from
;; values in the range that satisfy a specified condition (predicate).
;; a) sum of squares of prime numbers - assuming prime? exists already
(define (filtered-accumulate-r combiner null-val predicate term a next b)
;; combiner 2 args - element and accumulation
;; predicate 1 arg - a condition when to apply combiner
;; term 1 arg - a function to compute the term
;; next 1 arg - compute a next step
(if (> a b)
null-val
(if (predicate a)
(combiner (term a) (filtered-accumulate-r combiner null-val predicate term (next a) next b))
;; should I call combiner with null-val instead of (term a) or can I
;; directly call filtered-accumulate-r?
(filtered-accumulate-r combiner null-val predicate term (next a) next b)
)
)
)
;; (filtered-accumulate-r sum-combiner 0 even? identity 1 add1 11)
(define (filtered-accumulate-i combiner null-val predicate term a next b)
(define (iter-fa a result)
(if (> a b)
result
(iter-fa (next a)
(if (predicate a)
(combiner (term a) result)
(combiner null-val result)
)
)
)
)
(iter-fa a null-val)
)
#+end_src
** 1.3.2 Sestavljanje procedur z ~Lambda~