sicp/zapiski/g1smo/1_3_zapiski_vaje.html

545 lines
41 KiB
HTML
Raw Normal View History

2024-05-29 21:56:50 +02:00
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"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-06-12 19:35:40 +02:00
<!-- 2024-06-12 sre 19:35 -->
2024-05-29 21:56:50 +02:00
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Zapiski #3 srečanja programerskega bralnega krožka SICP</title>
<meta name="author" content="Jurij" />
<meta name="generator" content="Org Mode" />
<style>
#content { max-width: 60em; margin: auto; }
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #e6e6e6;
border-radius: 3px;
background-color: #f2f2f2;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: auto;
}
pre.src:before {
display: none;
position: absolute;
top: -8px;
right: 12px;
padding: 3px;
color: #555;
background-color: #f2f2f299;
}
pre.src:hover:before { display: inline; margin-top: 14px;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-authinfo::before { content: 'Authinfo'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { }
</style>
</head>
<body>
<div id="content" class="content">
<h1 class="title">Zapiski #3 srečanja programerskega bralnega krožka SICP</h1>
2024-06-12 19:35:40 +02:00
<div id="outline-container-org5e882c4" class="outline-2">
<h2 id="org5e882c4">Teme</h2>
<div class="outline-text-2" id="text-org5e882c4">
2024-05-29 21:56:50 +02:00
</div>
2024-06-12 19:35:40 +02:00
<div id="outline-container-orgdaeae88" class="outline-3">
<h3 id="orgdaeae88">Funkcije višjega reda</h3>
2024-05-29 21:56:50 +02:00
</div>
</div>
2024-06-12 19:35:40 +02:00
<div id="outline-container-org401da87" class="outline-2">
<h2 id="org401da87">Vaje</h2>
<div class="outline-text-2" id="text-org401da87">
2024-05-29 21:56:50 +02:00
</div>
2024-06-12 19:35:40 +02:00
<div id="outline-container-org91ad66e" class="outline-3">
<h3 id="org91ad66e">1.29 Simpsonovo pravilo</h3>
<div class="outline-text-3" id="text-org91ad66e">
2024-05-29 21:56:50 +02:00
<div class="org-src-container">
2024-06-12 19:35:40 +02:00
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span><span style="color: #b16286;">(</span><span style="color: #fabd2f;">cube</span> x<span style="color: #b16286;">)</span>
2024-05-29 21:56:50 +02:00
<span style="color: #b16286;">(</span>* x x x<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span><span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-integers</span> a b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span> &gt; a b<span style="color: #8ec07c;">)</span>
2024-05-29 21:56:50 +02:00
0
<span style="color: #8ec07c;">(</span>+ a <span style="color: #d65d0e;">(</span>sum-integers <span style="color: #458588;">(</span>+ a 1<span style="color: #458588;">)</span> b<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span><span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-cubes</span> a b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span>&gt; a b<span style="color: #8ec07c;">)</span>
2024-05-29 21:56:50 +02:00
0
<span style="color: #8ec07c;">(</span>+ <span style="color: #d65d0e;">(</span>cube a<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>sum-cubes<span style="color: #458588;">(</span>+ a 1<span style="color: #458588;">)</span> b<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span>&gt; a b<span style="color: #8ec07c;">)</span>
2024-05-29 21:56:50 +02:00
0
<span style="color: #8ec07c;">(</span>+ <span style="color: #d65d0e;">(</span>term a<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>sum term <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> next b<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">integral</span> f a b dx<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">add-dx</span> x<span style="color: #8ec07c;">)</span> <span style="color: #8ec07c;">(</span>+ x dx<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
2024-05-29 21:56:50 +02:00
<span style="color: #b16286;">(</span>* <span style="color: #8ec07c;">(</span>sum f <span style="color: #d65d0e;">(</span>+ a <span style="color: #458588;">(</span>/ dx 2.0<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span> add-dx b<span style="color: #8ec07c;">)</span>
dx<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">simpson</span> f a b n<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #fabd2f;">h</span> <span style="color: #8ec07c;">(</span>/ <span style="color: #d65d0e;">(</span>- b a<span style="color: #d65d0e;">)</span> n<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">add-hh</span> a<span style="color: #8ec07c;">)</span> <span style="color: #8ec07c;">(</span>+ a h h<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
2024-05-29 21:56:50 +02:00
<span style="color: #b16286;">(</span>* <span style="color: #8ec07c;">(</span>+ <span style="color: #d65d0e;">(</span>- <span style="color: #458588;">(</span>f a<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>* 2 <span style="color: #458588;">(</span>sum f a add-hh b<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>* 4 <span style="color: #458588;">(</span>sum f <span style="color: #b16286;">(</span>+ a h<span style="color: #b16286;">)</span> add-hh b<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>- <span style="color: #458588;">(</span>f b<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span>/ h 3.0<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>list <span style="color: #b16286;">(</span>simpson cube 1 2 100<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>simpson cube 1 2 1000<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">3.7500000000000004</td>
<td class="org-right">3.75</td>
</tr>
</tbody>
</table>
</div>
</div>
2024-06-12 19:35:40 +02:00
<div id="outline-container-org710ddb9" class="outline-3">
<h3 id="org710ddb9">1.30 linearen sum</h3>
<div class="outline-text-3" id="text-org710ddb9">
2024-05-29 21:56:50 +02:00
<p>
Rekurzivne procedure:
</p>
<div class="org-src-container">
2024-06-12 19:35:40 +02:00
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span>&gt; a b<span style="color: #8ec07c;">)</span>
2024-05-29 21:56:50 +02:00
0
<span style="color: #8ec07c;">(</span>+ <span style="color: #d65d0e;">(</span>term a<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>sum term <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> next b<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">cube</span> n<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* n n n<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">inc</span> n<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>+ n 1<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-05-29 21:56:50 +02:00
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-cubes</span> a b<span style="color: #b16286;">)</span>
2024-05-29 21:56:50 +02:00
<span style="color: #b16286;">(</span>sum cube a inc b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>sum-cubes 1 10<span style="color: #458588;">)</span>
</pre>
</div>
<pre class="example">
3025
</pre>
<p>
Iterativne procedure:
</p>
<div class="org-src-container">
2024-06-12 19:35:40 +02:00
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-iter</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">iter</span> a result<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span><span style="color: #fb4933;">if</span> <span style="color: #d65d0e;">(</span>&gt; a b<span style="color: #d65d0e;">)</span>
2024-05-29 21:56:50 +02:00
result
<span style="color: #d65d0e;">(</span>iter <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> <span style="color: #458588;">(</span>+ result <span style="color: #b16286;">(</span>term a<span style="color: #b16286;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>iter a 0<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">cube</span> n<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* n n n<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">inc</span> n<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>+ n 1<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-05-29 21:56:50 +02:00
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-cubes-iter</span> a b<span style="color: #b16286;">)</span>
2024-05-29 21:56:50 +02:00
<span style="color: #b16286;">(</span>sum-iter cube a inc b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>sum-cubes-iter 1 10<span style="color: #458588;">)</span>
</pre>
</div>
<pre class="example">
3025
</pre>
</div>
</div>
2024-06-12 19:35:40 +02:00
<div id="outline-container-org7e0d0cf" class="outline-3">
<h3 id="org7e0d0cf">1.31 Produkt višjega reda</h3>
<div class="outline-text-3" id="text-org7e0d0cf">
2024-05-29 21:56:50 +02:00
<div class="org-src-container">
2024-06-12 19:35:40 +02:00
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">prod-iter</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">iter</span> a result<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span><span style="color: #fb4933;">if</span> <span style="color: #d65d0e;">(</span>&gt; a b<span style="color: #d65d0e;">)</span>
2024-05-29 21:56:50 +02:00
result
<span style="color: #d65d0e;">(</span>iter <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> <span style="color: #458588;">(</span>* result <span style="color: #b16286;">(</span>term a<span style="color: #b16286;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #b16286;">(</span>iter a 1<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-05-29 21:56:50 +02:00
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">piblizek</span> n<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">stevec</span> m<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span>+ m 2<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>* 4 <span style="color: #8ec07c;">(</span>/ <span style="color: #d65d0e;">(</span>prod-iter <span style="color: #458588;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #b16286;">(</span>a<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* <span style="color: #8ec07c;">(</span>- a 1<span style="color: #8ec07c;">)</span> <span style="color: #8ec07c;">(</span>+ a 1<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-05-29 21:56:50 +02:00
3
stevec
n<span style="color: #d65d0e;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #d65d0e;">(</span>prod-iter <span style="color: #458588;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #b16286;">(</span>b<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* b b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-05-29 21:56:50 +02:00
3
stevec
n<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
2024-06-12 19:35:40 +02:00
<span style="color: #458588;">(</span>+ <span style="color: #b16286;">(</span>piblizek 10000<span style="color: #b16286;">)</span> 0.0<span style="color: #458588;">)</span>
</pre>
</div>
<pre class="example">
3.1417497371492673
</pre>
</div>
</div>
<div id="outline-container-orgd135cab" class="outline-3">
<h3 id="orgd135cab">1.32 Akumulator (reduce funkcija)</h3>
<div class="outline-text-3" id="text-orgd135cab">
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">accumulate</span> combiner null-value term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span>&gt; a b<span style="color: #8ec07c;">)</span>
null-value
<span style="color: #8ec07c;">(</span>combiner <span style="color: #d65d0e;">(</span>term a<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>accumulate combiner null-value term <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> next b<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>accumulate + 0 term a next b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">cube</span> n<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* n n n<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-cubes</span> a b<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>sum cube a <span style="color: #8ec07c;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #d65d0e;">(</span>n<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>+ n 1<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span> b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">prod</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>accumulate * 1 term a next b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">piblizek</span> n<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">stevec</span> m<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span>+ m 2<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>* 4 <span style="color: #8ec07c;">(</span>/ <span style="color: #d65d0e;">(</span>prod <span style="color: #458588;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #b16286;">(</span>a<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* <span style="color: #8ec07c;">(</span>- a 1<span style="color: #8ec07c;">)</span> <span style="color: #8ec07c;">(</span>+ a 1<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
3
stevec
n<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>prod <span style="color: #458588;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #b16286;">(</span>b<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* b b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
3
stevec
n<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>list <span style="color: #b16286;">(</span>sum-cubes 1 10<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>+ <span style="color: #8ec07c;">(</span>piblizek 10000<span style="color: #8ec07c;">)</span> 0.0<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">3025</td>
<td class="org-right">3.1417497371492673</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-orgacc9e22" class="outline-3">
<h3 id="orgacc9e22">1.32b Akumulator iterativno (reduce funkcija)</h3>
<div class="outline-text-3" id="text-orgacc9e22">
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">accumulate-iter</span> combiner null-value term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">combine</span> a combination<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span><span style="color: #fb4933;">if</span> <span style="color: #d65d0e;">(</span>&gt; a b<span style="color: #d65d0e;">)</span>
combination
<span style="color: #d65d0e;">(</span>combine <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> <span style="color: #458588;">(</span>combiner combination <span style="color: #b16286;">(</span>term a<span style="color: #b16286;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>combine a null-value<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>accumulate-iter + 0 term a next b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">cube</span> n<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* n n n<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">sum-cubes</span> a b<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>sum cube a <span style="color: #8ec07c;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #d65d0e;">(</span>n<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>+ n 1<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span> b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">prod</span> term a next b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>accumulate-iter * 1 term a next b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">piblizek</span> n<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">stevec</span> m<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span>+ m 2<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>* 4 <span style="color: #8ec07c;">(</span>/ <span style="color: #d65d0e;">(</span>prod <span style="color: #458588;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #b16286;">(</span>a<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* <span style="color: #8ec07c;">(</span>- a 1<span style="color: #8ec07c;">)</span> <span style="color: #8ec07c;">(</span>+ a 1<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
3
stevec
n<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>prod <span style="color: #458588;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #b16286;">(</span>b<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>* b b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
3
stevec
n<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>list <span style="color: #b16286;">(</span>sum-cubes 1 10<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>+ <span style="color: #8ec07c;">(</span>piblizek 10000<span style="color: #8ec07c;">)</span> 0.0<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">3025</td>
<td class="org-right">3.1417497371492673</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-org8780c88" class="outline-3">
<h3 id="org8780c88">1.33 filtriran akumulator</h3>
<div class="outline-text-3" id="text-org8780c88">
</div>
<div id="outline-container-orgd3eb7fb" class="outline-4">
<h4 id="orgd3eb7fb">a) vsota kvadratov praštevil med a in b</h4>
<div class="outline-text-4" id="text-orgd3eb7fb">
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">accumulate-filter</span> combiner null-value term a next b predikat?<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span>&gt; a b<span style="color: #8ec07c;">)</span>
null-value
<span style="color: #8ec07c;">(</span><span style="color: #fb4933;">if</span> <span style="color: #d65d0e;">(</span>predikat? a<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>combiner <span style="color: #458588;">(</span>term a<span style="color: #458588;">)</span> <span style="color: #458588;">(</span>accumulate-filter combiner null-value term <span style="color: #b16286;">(</span>next a<span style="color: #b16286;">)</span> next b predikat?<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>accumulate-filter combiner null-value term <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> next b predikat?<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">je-prastevilo?</span> a<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">define</span> <span style="color: #8ec07c;">(</span><span style="color: #fabd2f;">preveri-prastevilo?</span> x a<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span><span style="color: #fb4933;">if</span> <span style="color: #d65d0e;">(</span>&gt; x <span style="color: #458588;">(</span>/ a 2<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
#t
<span style="color: #d65d0e;">(</span><span style="color: #fb4933;">and</span> <span style="color: #458588;">(</span>not <span style="color: #b16286;">(</span>integer? <span style="color: #8ec07c;">(</span>/ a x<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>preveri-prastevilo? <span style="color: #b16286;">(</span>+ x 1<span style="color: #b16286;">)</span> a<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>preveri-prastevilo? 2 a<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #7c6f64;">;; </span><span style="color: #7c6f64;">(vsota kvadratov pra&#353;tevil med 1 in 12 je 209)</span>
<span style="color: #458588;">(</span>= <span style="color: #b16286;">(</span>accumulate-filter + 0 <span style="color: #8ec07c;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #d65d0e;">(</span>n<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>* n n<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span> 1 <span style="color: #8ec07c;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #d65d0e;">(</span>n<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>+ n 1<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span> 12 je-prastevilo?<span style="color: #b16286;">)</span>
209<span style="color: #458588;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orga117f13" class="outline-4">
<h4 id="orga117f13">a) produkt vseh naravnih števil manjših od n ki so "relativno pra-n" (torej si z n ne delijo skupnega delitelja razen n)</h4>
<div class="outline-text-4" id="text-orga117f13">
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">accumulate-filter</span> combiner null-value term a next b predikat?<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #fb4933;">if</span> <span style="color: #8ec07c;">(</span>&gt; a b<span style="color: #8ec07c;">)</span>
null-value
<span style="color: #8ec07c;">(</span><span style="color: #fb4933;">if</span> <span style="color: #d65d0e;">(</span>predikat? a<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>combiner <span style="color: #458588;">(</span>term a<span style="color: #458588;">)</span> <span style="color: #458588;">(</span>accumulate-filter combiner null-value term <span style="color: #b16286;">(</span>next a<span style="color: #b16286;">)</span> next b predikat?<span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>accumulate-filter combiner null-value term <span style="color: #458588;">(</span>next a<span style="color: #458588;">)</span> next b predikat?<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">id</span> a<span style="color: #b16286;">)</span> a<span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">inc</span> a<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span>+ a 1<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">IN</span> a b<span style="color: #b16286;">)</span> <span style="color: #b16286;">(</span><span style="color: #fb4933;">and</span> a b<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">relativni-prastevili?</span> a b<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>accumulate-filter IN #t <span style="color: #8ec07c;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #d65d0e;">(</span>x<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span><span style="color: #fb4933;">and</span> <span style="color: #458588;">(</span>not <span style="color: #b16286;">(</span>integer? <span style="color: #8ec07c;">(</span>/ a x<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>not <span style="color: #b16286;">(</span>integer? <span style="color: #8ec07c;">(</span>/ b x<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span>
2 inc a id<span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #fb4933;">define</span> <span style="color: #b16286;">(</span><span style="color: #fabd2f;">produkt-relativnih-prastevil</span> n<span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>accumulate-filter * 1 id 1 inc n <span style="color: #8ec07c;">(</span><span style="color: #fb4933;">lambda</span> <span style="color: #d65d0e;">(</span>x<span style="color: #d65d0e;">)</span> <span style="color: #d65d0e;">(</span>relativni-prastevili? x n<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>produkt-relativnih-prastevil 9<span style="color: #458588;">)</span>
2024-05-29 21:56:50 +02:00
</pre>
</div>
<p>
2024-06-12 19:35:40 +02:00
TODO popravi!! ^
2024-05-29 21:56:50 +02:00
</p>
</div>
</div>
</div>
</div>
2024-06-12 19:35:40 +02:00
</div>
<div id="postamble" class="status">
<p class="author">Author: Jurij</p>
<p class="date">Created: 2024-06-12 sre 19:35</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
2024-05-29 21:56:50 +02:00
</body>
</html>