Drugo poglavje 2.1 - lio.

main
Lio Novelli 2024-06-10 01:04:10 +02:00
parent 93c1532164
commit b21ded727b
2 changed files with 797 additions and 108 deletions

View File

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <head>
<!-- 2024-05-29 Wed 21:50 --> <!-- 2024-06-10 Mon 01:02 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Structure and Interpretation of Computer Programs</title> <title>Structure and Interpretation of Computer Programs</title>
@ -201,47 +201,58 @@
<h2>Table of Contents</h2> <h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc"> <div id="text-table-of-contents" role="doc-toc">
<ul> <ul>
<li><a href="#orgc29ca22">1. Foreword and Preface</a></li> <li><a href="#org1ab68a2">Foreword and Preface</a></li>
<li><a href="#org81536d5">2. 1. Grajenje abstrakcij s procedurami</a> <li><a href="#org6693cbf">1. Grajenje abstrakcij s procedurami</a>
<ul> <ul>
<li><a href="#org09ba581">2.1. Elementi programiranja</a></li> <li><a href="#org6b0ed2e">1.1. Elementi programiranja</a></li>
<li><a href="#org79ae90c">2.2. Izvajanje kombinacij(e)</a></li> <li><a href="#orgebcf1d6">1.2. Izvajanje kombinacij(e)</a></li>
<li><a href="#org4bf06e2">2.3. 1.1.4 Sestavljene procedure</a></li> <li><a href="#org8937279">1.3. 1.1.4 Sestavljene procedure</a></li>
<li><a href="#orgf96a231">2.4. 1.1.5 Substitucijski model za izvajanje procedur</a></li> <li><a href="#orgcb5c684">1.4. 1.1.5 Substitucijski model za izvajanje procedur</a></li>
<li><a href="#org2356eae">2.5. meta</a> <li><a href="#org0a42e4c">1.5. meta</a>
<ul> <ul>
<li><a href="#org1a875a1">2.5.1. video lekcije</a></li> <li><a href="#orgb0f9514">1.5.1. video lekcije</a></li>
</ul> </ul>
</li> </li>
<li><a href="#orge24b089">2.6. vaje</a> <li><a href="#orgd2a78f0">1.6. vaje</a>
<ul> <ul>
<li><a href="#org9124e61">2.6.1. 1.3</a></li> <li><a href="#org7790bc5">1.6.1. 1.3</a></li>
<li><a href="#orgfb06a20">2.6.2. 1.5</a></li> <li><a href="#org3ccc9ff">1.6.2. 1.5</a></li>
<li><a href="#orgb4abc50">2.6.3. 1.6</a></li> <li><a href="#orge03b4b9">1.6.3. 1.6</a></li>
<li><a href="#org19c25b3">2.6.4. 1.7</a></li> <li><a href="#org62edd3c">1.6.4. 1.7</a></li>
<li><a href="#orgd223612">2.6.5. 1.8</a></li> <li><a href="#org42d25fe">1.6.5. 1.8</a></li>
</ul>
</li>
<li><a href="#org670694e">1.7. 1.1.8 Procedure kot crne skatle abstrakcij</a></li>
<li><a href="#org058363a">1.8. 1.2.2 Drevesna rekurzija</a></li>
<li><a href="#orga2d0d00">1.9. 1.2.3 Redi rasti</a></li>
<li><a href="#orgdb2bb3d">1.10. 1.2.4 Eksponentna funkcija</a></li>
<li><a href="#org867a680">1.11. 1.2.5 Najvecji skupni deljitel</a></li>
<li><a href="#org29472e2">1.12. 1.2.6 Primer: Iskanje prastevil</a></li>
<li><a href="#org84f4b2d">1.13. 1.3 Sestavljanje abstrakcij s procedurami visjega reda</a></li>
<li><a href="#org43a7808">1.14. 1.3.1 Procedure kot argumenti</a></li>
<li><a href="#orga384969">1.15. 1.3.2 Sestavljanje procedur z <code>Lambda</code></a></li>
<li><a href="#org590844a">1.16. 1.3.3 Procedure kot splosne metode</a></li>
<li><a href="#org8a3aa7d">1.17. 1.3.4 Procedure kot vrnjene vrednosti</a></li>
</ul>
</li>
<li><a href="#org66fd37e">2. Grajenje absrakcij s podatki</a>
<ul>
<li><a href="#org501bf16">2.1. Uvod v podatkovne abstrakcije</a>
<ul>
<li><a href="#orga2f86a5">2.1.1. Aritmeticne operacije z racionalnimi stevili</a></li>
<li><a href="#org9d926fd">2.1.2. Pregrade abstrakcij</a></li>
<li><a href="#org518e1e2">2.1.3. Kaj so podatki?</a></li>
</ul> </ul>
</li> </li>
<li><a href="#org73c888b">2.7. 1.1.8 Procedure kot crne skatle abstrakcij</a></li>
<li><a href="#org95ea2ee">2.8. 1.2.2 Drevesna rekurzija</a></li>
<li><a href="#org1358990">2.9. 1.2.3 Redi rasti</a></li>
<li><a href="#org18b4f25">2.10. 1.2.4 Eksponentna funkcija</a></li>
<li><a href="#orga719e3c">2.11. 1.2.5 Najvecji skupni deljitel</a></li>
<li><a href="#org303b7e7">2.12. 1.2.6 Primer: Iskanje prastevil</a></li>
<li><a href="#org308cc01">2.13. 1.3 Sestavljanje abstrakcij s procedurami visjega reda</a></li>
<li><a href="#orgc7ad7a4">2.14. 1.3.1 Procedure kot argumenti</a></li>
<li><a href="#org920469a">2.15. 1.3.2 Sestavljanje procedur z <code>Lambda</code></a></li>
<li><a href="#orgb365f70">2.16. 1.3.3 Procedure kot splosne metode</a></li>
<li><a href="#orge72480c">2.17. 1.3.4 Procedure kot vrnjene vrednosti</a></li>
</ul> </ul>
</li> </li>
</ul> </ul>
</div> </div>
</div> </div>
<div id="outline-container-orgc29ca22" class="outline-2"> <div id="outline-container-org1ab68a2" class="outline-2">
<h2 id="orgc29ca22"><span class="section-number-2">1.</span> Foreword and Preface</h2> <h2 id="org1ab68a2">Foreword and Preface</h2>
<div class="outline-text-2" id="text-1"> <div class="outline-text-2" id="text-org1ab68a2">
<blockquote> <blockquote>
<p> <p>
Lisp je preživeli, v uporabi je že "polovico stoletja". Lisp je preživeli, v uporabi je že "polovico stoletja".
@ -282,13 +293,13 @@ ukvarjanje s pojmovanjem "kako".
</div> </div>
</div> </div>
<div id="outline-container-org81536d5" class="outline-2"> <div id="outline-container-org6693cbf" class="outline-2">
<h2 id="org81536d5"><span class="section-number-2">2.</span> 1. Grajenje abstrakcij s procedurami</h2> <h2 id="org6693cbf"><span class="section-number-2">1.</span> Grajenje abstrakcij s procedurami</h2>
<div class="outline-text-2" id="text-2"> <div class="outline-text-2" id="text-1">
</div> </div>
<div id="outline-container-org09ba581" class="outline-3"> <div id="outline-container-org6b0ed2e" class="outline-3">
<h3 id="org09ba581"><span class="section-number-3">2.1.</span> Elementi programiranja</h3> <h3 id="org6b0ed2e"><span class="section-number-3">1.1.</span> Elementi programiranja</h3>
<div class="outline-text-3" id="text-2-1"> <div class="outline-text-3" id="text-1-1">
<dl class="org-dl"> <dl class="org-dl">
<dt>Primitivni izrazi</dt><dd>predstavtljajo najpreprostejše gradnike (entitete) <dt>Primitivni izrazi</dt><dd>predstavtljajo najpreprostejše gradnike (entitete)
programskega jezika</dd> programskega jezika</dd>
@ -300,9 +311,9 @@ omogočajo upravljanje z njimii kot enotami</dd>
</div> </div>
</div> </div>
<div id="outline-container-org79ae90c" class="outline-3"> <div id="outline-container-orgebcf1d6" class="outline-3">
<h3 id="org79ae90c"><span class="section-number-3">2.2.</span> Izvajanje kombinacij(e)</h3> <h3 id="orgebcf1d6"><span class="section-number-3">1.2.</span> Izvajanje kombinacij(e)</h3>
<div class="outline-text-3" id="text-2-2"> <div class="outline-text-3" id="text-1-2">
<p> <p>
Postopek za izvajanje kombinacij: Postopek za izvajanje kombinacij:
</p> </p>
@ -343,9 +354,9 @@ oblika.
</div> </div>
<div id="outline-container-org4bf06e2" class="outline-3"> <div id="outline-container-org8937279" class="outline-3">
<h3 id="org4bf06e2"><span class="section-number-3">2.3.</span> 1.1.4 Sestavljene procedure</h3> <h3 id="org8937279"><span class="section-number-3">1.3.</span> 1.1.4 Sestavljene procedure</h3>
<div class="outline-text-3" id="text-2-3"> <div class="outline-text-3" id="text-1-3">
<ul class="org-ul"> <ul class="org-ul">
<li>Številke in aritmetične operacije so primitivni podatki in procedure.</li> <li>Številke in aritmetične operacije so primitivni podatki in procedure.</li>
<li>Gnezdenje kombinacij omogoča način za združevanje operacij.</li> <li>Gnezdenje kombinacij omogoča način za združevanje operacij.</li>
@ -363,9 +374,9 @@ abstrakcije.</li>
</div> </div>
</div> </div>
<div id="outline-container-orgf96a231" class="outline-3"> <div id="outline-container-orgcb5c684" class="outline-3">
<h3 id="orgf96a231"><span class="section-number-3">2.4.</span> 1.1.5 Substitucijski model za izvajanje procedur</h3> <h3 id="orgcb5c684"><span class="section-number-3">1.4.</span> 1.1.5 Substitucijski model za izvajanje procedur</h3>
<div class="outline-text-3" id="text-2-4"> <div class="outline-text-3" id="text-1-4">
<p> <p>
Za izvajanje sestavljenih procedur z argumenti, izvedeš telo procedure z vsakim Za izvajanje sestavljenih procedur z argumenti, izvedeš telo procedure z vsakim
formalnim parametrom, ki ga nadomestiš s pripadajočim argumentom. formalnim parametrom, ki ga nadomestiš s pripadajočim argumentom.
@ -393,9 +404,9 @@ izraza, ki vsebuje zgolj primitivne izraze in potem izvedi (vso) evalvacijo.</dd
</div> </div>
<div id="outline-container-org2356eae" class="outline-3"> <div id="outline-container-org0a42e4c" class="outline-3">
<h3 id="org2356eae"><span class="section-number-3">2.5.</span> meta</h3> <h3 id="org0a42e4c"><span class="section-number-3">1.5.</span> meta</h3>
<div class="outline-text-3" id="text-2-5"> <div class="outline-text-3" id="text-1-5">
<p> <p>
Linki: Linki:
<a href="https://develop.spacemacs.org/layers/+lang/scheme/README.html">https://develop.spacemacs.org/layers/+lang/scheme/README.html</a> <a href="https://develop.spacemacs.org/layers/+lang/scheme/README.html">https://develop.spacemacs.org/layers/+lang/scheme/README.html</a>
@ -410,9 +421,9 @@ Kako nastavit spacemacs, in malo o guile-u.
</div> </div>
<div id="outline-container-org1a875a1" class="outline-4"> <div id="outline-container-orgb0f9514" class="outline-4">
<h4 id="org1a875a1"><span class="section-number-4">2.5.1.</span> video lekcije</h4> <h4 id="orgb0f9514"><span class="section-number-4">1.5.1.</span> video lekcije</h4>
<div class="outline-text-4" id="text-2-5-1"> <div class="outline-text-4" id="text-1-5-1">
<p> <p>
<a href="https://yewtu.be/channel/UCEBb1b_L6zDS3xTUrIALZOw">https://yewtu.be/channel/UCEBb1b_L6zDS3xTUrIALZOw</a> (6.001 SICP: Structure and Interpretation of Computer Programs (2004)) <a href="https://yewtu.be/channel/UCEBb1b_L6zDS3xTUrIALZOw">https://yewtu.be/channel/UCEBb1b_L6zDS3xTUrIALZOw</a> (6.001 SICP: Structure and Interpretation of Computer Programs (2004))
<a href="https://yewtu.be/playlist?list=PL7BcsI5ueSNFPCEisbaoQ0kXIDX9rR5FF">https://yewtu.be/playlist?list=PL7BcsI5ueSNFPCEisbaoQ0kXIDX9rR5FF</a> (MIT 6.001 Structure and Interpretation, 1986) <a href="https://yewtu.be/playlist?list=PL7BcsI5ueSNFPCEisbaoQ0kXIDX9rR5FF">https://yewtu.be/playlist?list=PL7BcsI5ueSNFPCEisbaoQ0kXIDX9rR5FF</a> (MIT 6.001 Structure and Interpretation, 1986)
@ -421,17 +432,17 @@ Kako nastavit spacemacs, in malo o guile-u.
</div> </div>
</div> </div>
<div id="outline-container-orge24b089" class="outline-3"> <div id="outline-container-orgd2a78f0" class="outline-3">
<h3 id="orge24b089"><span class="section-number-3">2.6.</span> vaje</h3> <h3 id="orgd2a78f0"><span class="section-number-3">1.6.</span> vaje</h3>
<div class="outline-text-3" id="text-2-6"> <div class="outline-text-3" id="text-1-6">
</div> </div>
<div id="outline-container-org9124e61" class="outline-4"> <div id="outline-container-org7790bc5" class="outline-4">
<h4 id="org9124e61"><span class="section-number-4">2.6.1.</span> 1.3</h4> <h4 id="org7790bc5"><span class="section-number-4">1.6.1.</span> 1.3</h4>
<div class="outline-text-4" id="text-2-6-1"> <div class="outline-text-4" id="text-1-6-1">
</div> </div>
<ol class="org-ol"> <ol class="org-ol">
<li><a id="org101bff2"></a>najprej narobe<br /> <li><a id="orga11bbd9"></a>najprej narobe<br />
<div class="outline-text-5" id="text-2-6-1-1"> <div class="outline-text-5" id="text-1-6-1-1">
<p> <p>
Define a procedure that takes three numbers as arguments and returns the sum of Define a procedure that takes three numbers as arguments and returns the sum of
the squares of the two larger numbers. the squares of the two larger numbers.
@ -461,8 +472,8 @@ the squares of the two larger numbers.
</div> </div>
</li> </li>
<li><a id="orge6e316f"></a>pravilno<br /> <li><a id="orga97ce26"></a>pravilno<br />
<div class="outline-text-5" id="text-2-6-1-2"> <div class="outline-text-5" id="text-1-6-1-2">
<div class="org-src-container"> <div class="org-src-container">
<pre class="src src-scheme"><span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">sum-squares-of-larger</span> x y z<span style="color: #bc6ec5;">)</span> <pre class="src src-scheme"><span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">sum-squares-of-larger</span> x y z<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #2d9574;">(</span>&gt; x y<span style="color: #2d9574;">)</span> <span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #2d9574;">(</span>&gt; x y<span style="color: #2d9574;">)</span>
@ -484,9 +495,9 @@ the squares of the two larger numbers.
</ol> </ol>
</div> </div>
<div id="outline-container-orgfb06a20" class="outline-4"> <div id="outline-container-org3ccc9ff" class="outline-4">
<h4 id="orgfb06a20"><span class="section-number-4">2.6.2.</span> 1.5</h4> <h4 id="org3ccc9ff"><span class="section-number-4">1.6.2.</span> 1.5</h4>
<div class="outline-text-4" id="text-2-6-2"> <div class="outline-text-4" id="text-1-6-2">
<p> <p>
Aplikativni vrstni red: pade takoj v neskoncno zanko. Aplikativni vrstni red: pade takoj v neskoncno zanko.
Normalni vrstni red: izvrsi test in pride v if, ki ne izvrsi drugega dela. Normalni vrstni red: izvrsi test in pride v if, ki ne izvrsi drugega dela.
@ -494,18 +505,18 @@ Normalni vrstni red: izvrsi test in pride v if, ki ne izvrsi drugega dela.
</div> </div>
</div> </div>
<div id="outline-container-orgb4abc50" class="outline-4"> <div id="outline-container-orge03b4b9" class="outline-4">
<h4 id="orgb4abc50"><span class="section-number-4">2.6.3.</span> 1.6</h4> <h4 id="orge03b4b9"><span class="section-number-4">1.6.3.</span> 1.6</h4>
<div class="outline-text-4" id="text-2-6-3"> <div class="outline-text-4" id="text-1-6-3">
<p> <p>
<a href="sqrt-newton.scm">sqrt-newton.scm</a> <a href="sqrt-newton.scm">sqrt-newton.scm</a>
</p> </p>
</div> </div>
</div> </div>
<div id="outline-container-org19c25b3" class="outline-4"> <div id="outline-container-org62edd3c" class="outline-4">
<h4 id="org19c25b3"><span class="section-number-4">2.6.4.</span> 1.7</h4> <h4 id="org62edd3c"><span class="section-number-4">1.6.4.</span> 1.7</h4>
<div class="outline-text-4" id="text-2-6-4"> <div class="outline-text-4" id="text-1-6-4">
<ul class="org-ul"> <ul class="org-ul">
<li><code>good-enough?</code> ni vredu za iskanje korenov majhnih stevil.</li> <li><code>good-enough?</code> ni vredu za iskanje korenov majhnih stevil.</li>
<li>pravtako za zelo velika stevila</li> <li>pravtako za zelo velika stevila</li>
@ -519,9 +530,9 @@ dovolj majhne in takrat prekini funkcijo.</li>
</div> </div>
</div> </div>
<div id="outline-container-orgd223612" class="outline-4"> <div id="outline-container-org42d25fe" class="outline-4">
<h4 id="orgd223612"><span class="section-number-4">2.6.5.</span> 1.8</h4> <h4 id="org42d25fe"><span class="section-number-4">1.6.5.</span> 1.8</h4>
<div class="outline-text-4" id="text-2-6-5"> <div class="outline-text-4" id="text-1-6-5">
<p> <p>
// Glej v sqrt-newton.sqm // Glej v sqrt-newton.sqm
</p> </p>
@ -529,9 +540,9 @@ dovolj majhne in takrat prekini funkcijo.</li>
</div> </div>
</div> </div>
<div id="outline-container-org73c888b" class="outline-3"> <div id="outline-container-org670694e" class="outline-3">
<h3 id="org73c888b"><span class="section-number-3">2.7.</span> 1.1.8 Procedure kot crne skatle abstrakcij</h3> <h3 id="org670694e"><span class="section-number-3">1.7.</span> 1.1.8 Procedure kot crne skatle abstrakcij</h3>
<div class="outline-text-3" id="text-2-7"> <div class="outline-text-3" id="text-1-7">
<ul class="org-ul"> <ul class="org-ul">
<li>block structure</li> <li>block structure</li>
<li>lexical scoping</li> <li>lexical scoping</li>
@ -539,17 +550,21 @@ dovolj majhne in takrat prekini funkcijo.</li>
</div> </div>
</div> </div>
<div id="outline-container-org95ea2ee" class="outline-3"> <div id="outline-container-org058363a" class="outline-3">
<h3 id="org95ea2ee"><span class="section-number-3">2.8.</span> 1.2.2 Drevesna rekurzija</h3> <h3 id="org058363a"><span class="section-number-3">1.8.</span> 1.2.2 Drevesna rekurzija</h3>
</div> </div>
<div id="outline-container-org1358990" class="outline-3"> <div id="outline-container-orga2d0d00" class="outline-3">
<h3 id="org1358990"><span class="section-number-3">2.9.</span> 1.2.3 Redi rasti</h3> <h3 id="orga2d0d00"><span class="section-number-3">1.9.</span> 1.2.3 Redi rasti</h3>
</div> </div>
<div id="outline-container-org18b4f25" class="outline-3"> <div id="outline-container-orgdb2bb3d" class="outline-3">
<h3 id="org18b4f25"><span class="section-number-3">2.10.</span> 1.2.4 Eksponentna funkcija</h3> <h3 id="orgdb2bb3d"><span class="section-number-3">1.10.</span> 1.2.4 Eksponentna funkcija</h3>
<div class="outline-text-3" id="text-2-10"> <div class="outline-text-3" id="text-1-10">
<p>
Tukaj se naucimu successive squaring, ki potem se veckrat prav pride.
</p>
<p> <p>
#name: exponent #name: exponent
</p> </p>
@ -633,31 +648,68 @@ dovolj majhne in takrat prekini funkcijo.</li>
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">else</span> <span style="color: #67b11d;">(</span>fast-multi-iter a <span style="color: #b1951d;">(</span>- b <span style="color: #a45bad;">1</span><span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>+ s a<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span> <span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">else</span> <span style="color: #67b11d;">(</span>fast-multi-iter a <span style="color: #b1951d;">(</span>- b <span style="color: #a45bad;">1</span><span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>+ s a<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span> <span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">excercise 1.19 - fast fibnonachi</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">fast-fibo</span> n<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>fast-fibo-iter <span style="color: #a45bad;">1</span> <span style="color: #a45bad;">0</span> <span style="color: #a45bad;">0</span> <span style="color: #a45bad;">1</span> n<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">fast-fibo-iter</span> a b p q count<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">cond</span> <span style="color: #2d9574;">(</span><span style="color: #67b11d;">(</span>= count <span style="color: #a45bad;">0</span><span style="color: #67b11d;">)</span> b<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #67b11d;">(</span>even? count<span style="color: #67b11d;">)</span>
<span style="color: #67b11d;">(</span>fast-fibo-iter
a
b
<span style="color: #b1951d;">(</span>+ <span style="color: #4f97d7;">(</span>* p p<span style="color: #4f97d7;">)</span> <span style="color: #4f97d7;">(</span>* q q<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span>
<span style="color: #b1951d;">(</span>+ <span style="color: #4f97d7;">(</span>* q q<span style="color: #4f97d7;">)</span> <span style="color: #4f97d7;">(</span>* <span style="color: #a45bad;">2</span> p q<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span>
<span style="color: #b1951d;">(</span>/ count <span style="color: #a45bad;">2</span><span style="color: #b1951d;">)</span>
<span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">else</span> <span style="color: #67b11d;">(</span>fast-fibo-iter
<span style="color: #b1951d;">(</span>+ <span style="color: #4f97d7;">(</span>* b q<span style="color: #4f97d7;">)</span> <span style="color: #4f97d7;">(</span>* a q<span style="color: #4f97d7;">)</span> <span style="color: #4f97d7;">(</span>* a p<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span>
<span style="color: #b1951d;">(</span>+ <span style="color: #4f97d7;">(</span>* b p<span style="color: #4f97d7;">)</span> <span style="color: #4f97d7;">(</span>* a q<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span>
p
q
<span style="color: #b1951d;">(</span>- count <span style="color: #a45bad;">1</span><span style="color: #b1951d;">)</span>
<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">p' = q^2 + 2pq</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">p' = p^2 + q^2</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">slow-fibo</span> n<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">cond</span> <span style="color: #2d9574;">(</span><span style="color: #67b11d;">(</span>= n <span style="color: #a45bad;">0</span><span style="color: #67b11d;">)</span> <span style="color: #a45bad;">0</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #67b11d;">(</span>= n <span style="color: #a45bad;">1</span><span style="color: #67b11d;">)</span> <span style="color: #a45bad;">1</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">else</span> <span style="color: #67b11d;">(</span>+
<span style="color: #b1951d;">(</span>slow-fibo <span style="color: #4f97d7;">(</span>- n <span style="color: #a45bad;">1</span><span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span>
<span style="color: #b1951d;">(</span>slow-fibo <span style="color: #4f97d7;">(</span>- n <span style="color: #a45bad;">2</span><span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span>
<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">melje melje in melje . fast-fibo iypljune takoj</span>
</pre> </pre>
</div> </div>
</div> </div>
</div> </div>
<div id="outline-container-orga719e3c" class="outline-3"> <div id="outline-container-org867a680" class="outline-3">
<h3 id="orga719e3c"><span class="section-number-3">2.11.</span> 1.2.5 Najvecji skupni deljitel</h3> <h3 id="org867a680"><span class="section-number-3">1.11.</span> 1.2.5 Najvecji skupni deljitel</h3>
</div> </div>
<div id="outline-container-org303b7e7" class="outline-3"> <div id="outline-container-org29472e2" class="outline-3">
<h3 id="org303b7e7"><span class="section-number-3">2.12.</span> 1.2.6 Primer: Iskanje prastevil</h3> <h3 id="org29472e2"><span class="section-number-3">1.12.</span> 1.2.6 Primer: Iskanje prastevil</h3>
</div> </div>
<div id="outline-container-org308cc01" class="outline-3"> <div id="outline-container-org84f4b2d" class="outline-3">
<h3 id="org308cc01"><span class="section-number-3">2.13.</span> 1.3 Sestavljanje abstrakcij s procedurami visjega reda</h3> <h3 id="org84f4b2d"><span class="section-number-3">1.13.</span> 1.3 Sestavljanje abstrakcij s procedurami visjega reda</h3>
<div class="outline-text-3" id="text-2-13"> <div class="outline-text-3" id="text-1-13">
<p> <p>
Procedure, ki spreminjajo druge procedure se imenujejo <b>procedure višjega reda</b>. Procedure, ki spreminjajo druge procedure se imenujejo <b>procedure višjega reda</b>.
</p> </p>
</div> </div>
</div> </div>
<div id="outline-container-orgc7ad7a4" class="outline-3"> <div id="outline-container-org43a7808" class="outline-3">
<h3 id="orgc7ad7a4"><span class="section-number-3">2.14.</span> 1.3.1 Procedure kot argumenti</h3> <h3 id="org43a7808"><span class="section-number-3">1.14.</span> 1.3.1 Procedure kot argumenti</h3>
<div class="outline-text-3" id="text-2-14"> <div class="outline-text-3" id="text-1-14">
<p> <p>
Primer vsote. Primer vsote.
</p> </p>
@ -796,19 +848,93 @@ Primer vsote.
pribl-pi-next pribl-pi-next
n n
<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span> <span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">excercise 1.32</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">recursive accumulate</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">accumulate-r</span> combiner null-val term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">combiner is a procedure of two arguments.</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #2d9574;">(</span>&gt; a b<span style="color: #2d9574;">)</span>
null-val
<span style="color: #2d9574;">(</span>combiner <span style="color: #67b11d;">(</span>term a<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>accumulate-r combiner null-val term <span style="color: #b1951d;">(</span>next a<span style="color: #b1951d;">)</span> next b<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">sum-combiner</span> t acc<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>+ t acc<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">sum-a</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>accumulate-r <span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">lambda</span> <span style="color: #67b11d;">(</span>t acc<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>+ t acc<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span> <span style="color: #a45bad;">0</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">prod-a</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>accumulate-r <span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">lambda</span> <span style="color: #67b11d;">(</span>t acc<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>* t acc<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span> <span style="color: #a45bad;">1</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">accumulate-i</span> combiner null-val term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">Iterative accumulator.</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #2d9574;">(</span><span style="color: #bc6ec5; font-weight: bold;">iter-a</span> a result<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #67b11d;">(</span>&gt; a b<span style="color: #67b11d;">)</span>
result
<span style="color: #67b11d;">(</span>iter-a <span style="color: #b1951d;">(</span>next a<span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>combiner <span style="color: #4f97d7;">(</span>term a<span style="color: #4f97d7;">)</span> result<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>iter-a a null-val<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">identity</span> x<span style="color: #bc6ec5;">)</span> x<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">add1</span> x<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>+ x <span style="color: #a45bad;">1</span><span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">sum-ai</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>accumulate-i sum-combiner <span style="color: #a45bad;">0</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">prod-ai</span> term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>accumulate-i <span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">lambda</span> <span style="color: #67b11d;">(</span>t acc<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>* t acc<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span> <span style="color: #a45bad;">1</span> term a next b<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">fakulteta-ai</span> n<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>prod-ai identity <span style="color: #a45bad;">2</span> add1 n<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">excercise 1.33 filtered accumulate - combine only those term derived from</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">values in the range that satisfy a specified condition (predicate).</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">a) sum of squares of prime numbers - assuming prime? exists already</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">filtered-accumulate-r</span> combiner null-val predicate term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">combiner 2 args - element and accumulation</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">predicate 1 arg - a condition when to apply combiner</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">term 1 arg - a function to compute the term</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">next 1 arg - compute a next step</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #2d9574;">(</span>&gt; a b<span style="color: #2d9574;">)</span>
null-val
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #67b11d;">(</span>predicate a<span style="color: #67b11d;">)</span>
<span style="color: #67b11d;">(</span>combiner <span style="color: #b1951d;">(</span>term a<span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>filtered-accumulate-r combiner null-val predicate term <span style="color: #4f97d7;">(</span>next a<span style="color: #4f97d7;">)</span> next b<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">should I call combiner with null-val instead of (term a) or can I</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">directly call filtered-accumulate-r?</span>
<span style="color: #67b11d;">(</span>filtered-accumulate-r combiner null-val predicate term <span style="color: #b1951d;">(</span>next a<span style="color: #b1951d;">)</span> next b<span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">(filtered-accumulate-r sum-combiner 0 even? identity 1 add1 11)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">filtered-accumulate-i</span> combiner null-val predicate term a next b<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #2d9574;">(</span><span style="color: #bc6ec5; font-weight: bold;">iter-fa</span> a result<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #67b11d;">(</span>&gt; a b<span style="color: #67b11d;">)</span>
result
<span style="color: #67b11d;">(</span>iter-fa <span style="color: #b1951d;">(</span>next a<span style="color: #b1951d;">)</span>
<span style="color: #b1951d;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #4f97d7;">(</span>predicate a<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span>combiner <span style="color: #bc6ec5;">(</span>term a<span style="color: #bc6ec5;">)</span> result<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span>combiner null-val result<span style="color: #4f97d7;">)</span>
<span style="color: #b1951d;">)</span>
<span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>iter-fa a null-val<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
</pre> </pre>
</div> </div>
</div> </div>
</div> </div>
<div id="outline-container-org920469a" class="outline-3"> <div id="outline-container-orga384969" class="outline-3">
<h3 id="org920469a"><span class="section-number-3">2.15.</span> 1.3.2 Sestavljanje procedur z <code>Lambda</code></h3> <h3 id="orga384969"><span class="section-number-3">1.15.</span> 1.3.2 Sestavljanje procedur z <code>Lambda</code></h3>
<div class="outline-text-3" id="text-2-15"> <div class="outline-text-3" id="text-1-15">
<p> <p>
Splosna forma <code>let</code> izraza Splosna forma <code>let</code> izraza
</p> </p>
<pre class="example" id="orgc6db3ea"> <pre class="example" id="org861d813">
(let ((&lt;var1&gt; &lt;exp1&gt;) (let ((&lt;var1&gt; &lt;exp1&gt;)
(&lt;var2&gt; &lt;exp2&gt;) (&lt;var2&gt; &lt;exp2&gt;)
... ...
@ -818,7 +944,7 @@ Splosna forma <code>let</code> izraza
<p> <p>
To je okrajsava za To je okrajsava za
</p> </p>
<pre class="example" id="org4ff7fd6"> <pre class="example" id="orge922f38">
((lambda (&lt;var1&gt; ... &lt;varn&gt;) ((lambda (&lt;var1&gt; ... &lt;varn&gt;)
&lt;body&gt;) &lt;body&gt;)
&lt;exp1&gt; &lt;exp1&gt;
@ -830,9 +956,9 @@ To je okrajsava za
</div> </div>
<div id="outline-container-orgb365f70" class="outline-3"> <div id="outline-container-org590844a" class="outline-3">
<h3 id="orgb365f70"><span class="section-number-3">2.16.</span> 1.3.3 Procedure kot splosne metode</h3> <h3 id="org590844a"><span class="section-number-3">1.16.</span> 1.3.3 Procedure kot splosne metode</h3>
<div class="outline-text-3" id="text-2-16"> <div class="outline-text-3" id="text-1-16">
<p> <p>
Ce pogledamo proceduro za integral, vidimo mocnejse abstrakcije: procedure, ki Ce pogledamo proceduro za integral, vidimo mocnejse abstrakcije: procedure, ki
izrazajo splosne racunske metode, neodvisne od posameznih vkljucenih funkcij. izrazajo splosne racunske metode, neodvisne od posameznih vkljucenih funkcij.
@ -840,9 +966,9 @@ izrazajo splosne racunske metode, neodvisne od posameznih vkljucenih funkcij.
</div> </div>
</div> </div>
<div id="outline-container-orge72480c" class="outline-3"> <div id="outline-container-org8a3aa7d" class="outline-3">
<h3 id="orge72480c"><span class="section-number-3">2.17.</span> 1.3.4 Procedure kot vrnjene vrednosti</h3> <h3 id="org8a3aa7d"><span class="section-number-3">1.17.</span> 1.3.4 Procedure kot vrnjene vrednosti</h3>
<div class="outline-text-3" id="text-2-17"> <div class="outline-text-3" id="text-1-17">
<p> <p>
V splošnem programski jeziki omejujo, kateri komputacijski elemente lahko (koda) V splošnem programski jeziki omejujo, kateri komputacijski elemente lahko (koda)
spreminja. Elementi z najmanj omejitvami imajo <i>prvorazredni</i> status. Pravice in spreminja. Elementi z najmanj omejitvami imajo <i>prvorazredni</i> status. Pravice in
@ -866,11 +992,302 @@ spremenljivke shranjene v procedurino okolje (poglavje 4.1).
</div> </div>
</div> </div>
</div> </div>
<div id="outline-container-org66fd37e" class="outline-2">
<h2 id="org66fd37e"><span class="section-number-2">2.</span> Grajenje absrakcij s podatki</h2>
<div class="outline-text-2" id="text-2">
<p>
Poglavje bo govorilo o kompleksnih podatkih. Poglavje 1 govori o grajenju
abstrakcij z zdruzevanjem procedur, ki tvorijo sestavljene procedure (compound).
V poglavju 2 pa bo fokus na grajenju abstrakcij z zdruzevanjem podatkovnih
objektov v sestavljene podatke (compound).
</p>
<p>
Z zdruzenimi podatkovnimi objekti lahko procedure delajo nad njimi ne da bi bile
odvisne od njihove natancne strukture.
</p>
<p>
Podobno kot pri sestavljenih procedurah gre tudi pri sestavljenih podatkovnih
objektih za nacin spoprijemanja s kompleksnostjo - podatkovne abstrakcije
omogocijo postavitev primernih abstrakcijskih pregrad med razlicnimi deli
programa.
</p>
<p>
Napoved, kaj se bo pregledalo v 2. poglavju (bi bilo smiselno povzet).
</p>
</div>
<div id="outline-container-org501bf16" class="outline-3">
<h3 id="org501bf16"><span class="section-number-3">2.1.</span> Uvod v podatkovne abstrakcije</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Podatkovna abstrakcija je metodologija, ki nam omogoci, da locimo kako so
sestavljeni podatki uporabljeni od detajlov o tem, kako so izgrejeni iz
primitivnih podatkovnih objektov. (To je analogno grajenju produceur, ki imajo
vgrajene druge procedue iz poglavja 1.1.8)
</p>
<p>
Skratka programe hocemo graditi tako, da uporabljajo podatke na nacin, da nimajo
nobenih predpostavk o tem, kaksni naj so ti podatki (oziroma cim manj), ravno
dovolj za izvajanje potrebnih operacij. Hkrati so konkretne reprezentacije
podatkov definirane neodvisno od programov, ki podatke uporabljajo.
</p>
<p>
Selektorji in konstruktorji.
</p>
</div>
<div id="outline-container-orga2f86a5" class="outline-4">
<h4 id="orga2f86a5"><span class="section-number-4">2.1.1.</span> Aritmeticne operacije z racionalnimi stevili</h4>
<div class="outline-text-4" id="text-2-1-1">
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">add-rat</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>make-rat <span style="color: #2d9574;">(</span>+ <span style="color: #67b11d;">(</span>* <span style="color: #b1951d;">(</span>numer x<span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>denom y<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span>
<span style="color: #67b11d;">(</span>* <span style="color: #b1951d;">(</span>numer y<span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>denom x<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>denom x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">sub-rat</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>make-rat <span style="color: #2d9574;">(</span>- <span style="color: #67b11d;">(</span>numer x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom y<span style="color: #67b11d;">)</span>
<span style="color: #67b11d;">(</span>numer y<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom x<span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>denom x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">mul-rat</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>make-rat <span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>numer x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>numer y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>denom x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">div-rat</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>make-rat <span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>numer x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>denom x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>numer y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">equal-rat?</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>= <span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>numer x<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom y<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>* <span style="color: #67b11d;">(</span>numer y<span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>denom x<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">make-rat</span> n d<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>cons n d<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">numer</span> x<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>car x<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">denom</span> x<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>cdr x<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">print-rat</span> x<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">(</span>numer x<span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">"/"</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">(</span>denom x<span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>newline<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5; font-weight: bold;">one-half</span> <span style="color: #bc6ec5;">(</span>make-rat <span style="color: #a45bad;">1</span> <span style="color: #a45bad;">2</span><span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5; font-weight: bold;">one-third</span> <span style="color: #bc6ec5;">(</span>make-rat <span style="color: #a45bad;">1</span> <span style="color: #a45bad;">3</span><span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">excercise 2.1</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">make-rat-norm</span> n d<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">if</span> <span style="color: #2d9574;">(</span>&lt; d <span style="color: #a45bad;">0</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>make-rat <span style="color: #67b11d;">(</span>* n <span style="color: #a45bad;">-1</span><span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>* d <span style="color: #a45bad;">-1</span><span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>make-rat n d<span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
</pre>
</div>
<p>
Sestavljena strkutura <code>par</code>, ki je konstruirana s primitivno proceduro <code>cons</code>. S
primitivnimi procedurami <code>car</code> in <code>cdr</code> lahko dobimo prvi in ostale elemente
para.
</p>
</div>
<ul class="org-ul">
<li><a id="org06ac49c"></a>Predstavljanje racionalnih stevil<br />
<div class="outline-text-5" id="text-org06ac49c">
<p>
<i>Glej zgornji codeblock.</i>
</p>
</div>
</li>
</ul>
</div>
<div id="outline-container-org9d926fd" class="outline-4">
<h4 id="org9d926fd"><span class="section-number-4">2.1.2.</span> Pregrade abstrakcij</h4>
<div class="outline-text-4" id="text-2-1-2">
<p>
Splosna ideja podatkovnih abstrakcij je, da se identificira za vsak tip podatka
osnovni set opraracij, s katerimi bodo vse procedure, ki bodo manipulirale
podatke operirale, oziroma bodo iz njih sestavljene. Nato se uporabljamo samo te
operacije pri delu s podatki.
</p>
<p>
Pregrade:
</p>
<ul class="org-ul">
<li>programi, ki uporabljajo racionalna stevila</li>
<li>racionalna stevila v problemskem polju
<ul class="org-ul">
<li><code>add-rat</code>, <code>sub-rat</code> &#x2026;</li>
</ul></li>
<li>racionalna stevila kot stevci in imenovalci
<ul class="org-ul">
<li><code>make-rat</code>, <code>number</code>, <code>denom</code></li>
</ul></li>
<li>racionalna stevila kot pari
<ul class="org-ul">
<li><code>cons</code>, <code>car</code>, <code>cdr</code></li>
</ul></li>
<li>kakor so pac pari implementirani</li>
</ul>
<p>
Procedure na vsakem nivoju so vmesniki, ki definirajo abstrakcijski nivo in med
sabo povezujejo razlicne nivoje.
</p>
<p>
Ena od prednosti razdelitve na nivoje je, da je programe lazje vzdrzevati in
spreminjati, ker lahko delas spremembe na posameznem nivoju, ki ne vplivajo
izven svojega nivoja.
</p>
<p>
<i>vaja 2.2</i>
</p>
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">crte v prostoru</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">make-segment</span> startp endp<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>cons startp endp<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">make-line</span> x1 y1 x2 y2<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>make-segment <span style="color: #2d9574;">(</span>make-point x1 y1<span style="color: #2d9574;">)</span> <span style="color: #2d9574;">(</span>make-point x2 y2<span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">start-segment</span> segment<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>car segment<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">end-segment</span> segment<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>cdr segment<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">make-point</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>cons x y<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">x-point</span> p<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>car p<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">y-point</span> p<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>cdr p<span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">mid-point</span> segment<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>make-point
<span style="color: #2d9574;">(</span>/ <span style="color: #67b11d;">(</span>+ <span style="color: #b1951d;">(</span>x-point <span style="color: #4f97d7;">(</span>start-segment segment<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>x-point <span style="color: #4f97d7;">(</span>end-segment segment<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span> <span style="color: #a45bad;">2</span><span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span>/ <span style="color: #67b11d;">(</span>+ <span style="color: #b1951d;">(</span>y-point <span style="color: #4f97d7;">(</span>start-segment segment<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span> <span style="color: #b1951d;">(</span>y-point <span style="color: #4f97d7;">(</span>end-segment segment<span style="color: #4f97d7;">)</span><span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span> <span style="color: #a45bad;">2</span><span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">print-point</span> p<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">"("</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">(</span>x-point p<span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">","</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">(</span>y-point p<span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>display <span style="color: #2d9574;">")"</span><span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>newline<span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">vaja 2.3 :: segment je lahko tudi pravokotnik, ce nimamo rotacije in je crta</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">vedno diagonala. Delal bom brez rotacije, zato ker potem ni dovolj imeti</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">konstruktorja, ki je samo kons, ampak rabim 3 parametre, segment in rotacija</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">in potem nvm kako delat selektorje in pa se vse se mi zakomplicira in se mi</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">ne da, ker je nedelja zvecer.</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">brez rotacije - 2a + 2b</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">perimeter</span> rectangle<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>+ <span style="color: #2d9574;">(</span>* <span style="color: #a45bad;">2</span> <span style="color: #67b11d;">(</span>side-a rectangle<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span> <span style="color: #2d9574;">(</span>* <span style="color: #a45bad;">2</span> <span style="color: #67b11d;">(</span>side-b rectangle<span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">area</span> rectangle<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>* <span style="color: #2d9574;">(</span>side-a rectangle<span style="color: #2d9574;">)</span> <span style="color: #2d9574;">(</span>side-b rectangle<span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">selektor (brez rotacije)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">side-a</span> rectangle<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>abs <span style="color: #2d9574;">(</span>- <span style="color: #67b11d;">(</span>x-point <span style="color: #b1951d;">(</span>start-segment rectangle<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>x-point <span style="color: #b1951d;">(</span>end-segment rectangle<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">side-b</span> rectangle<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span>abs <span style="color: #2d9574;">(</span>- <span style="color: #67b11d;">(</span>y-point <span style="color: #b1951d;">(</span>start-segment rectangle<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span> <span style="color: #67b11d;">(</span>y-point <span style="color: #b1951d;">(</span>end-segment rectangle<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span><span style="color: #2d9574;">)</span><span style="color: #bc6ec5;">)</span>
<span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">sedaj vpeljemo drugo reprezentacijo pravokotnikov (nic vec s segmentom)</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">ali lahko obseg in ploscina se vedno delujeta?</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">odvisna sta od side-a in side-b. Ce to zemanjam, bosta obseg in ploscina se</span>
<span style="color: #2aa1ae; background-color: #292e34;">;; </span><span style="color: #2aa1ae; background-color: #292e34;">vedno delovali.</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org518e1e2" class="outline-4">
<h4 id="org518e1e2"><span class="section-number-4">2.1.3.</span> Kaj so podatki?</h4>
<div class="outline-text-4" id="text-2-1-3">
<p>
Pri racionalnih stevilih imamo se en pogoj:
</p>
<p>
<code>(/ (numer x) (denom x)) = n/d</code>
</p>
<p>
Selektorji konstruktorji in pogoji tvorijo veljavno reprezentacijo.
</p>
<p>
Vsaka trojica procedur, ki ustreza pogoju, da ce zdruzis dva objekta, in potem z
eno proceduro dobis iz zdruzenih prvi objekt in z drugo drugi objekt, je potem
trojica procedu za delanje s pari.
</p>
<p>
Trojico procedur (cons, car, cdr) se da implementirati brez podatkov:
</p>
<div class="org-src-container">
<pre class="src src-scheme"><span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">cons-p</span> x y<span style="color: #bc6ec5;">)</span>
<span style="color: #bc6ec5;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #2d9574;">(</span><span style="color: #bc6ec5; font-weight: bold;">dispatch</span> m<span style="color: #2d9574;">)</span>
<span style="color: #2d9574;">(</span><span style="color: #4f97d7; font-weight: bold;">cond</span>
<span style="color: #67b11d;">(</span><span style="color: #b1951d;">(</span>= m <span style="color: #a45bad;">0</span><span style="color: #b1951d;">)</span> x<span style="color: #67b11d;">)</span>
<span style="color: #67b11d;">(</span><span style="color: #b1951d;">(</span>= m <span style="color: #a45bad;">1</span><span style="color: #b1951d;">)</span> y<span style="color: #67b11d;">)</span>
<span style="color: #67b11d;">(</span><span style="color: #4f97d7; font-weight: bold;">else</span> <span style="color: #b1951d;">(</span>error <span style="color: #2d9574;">"Argument not 0 or 1 -- CONS"</span> m<span style="color: #b1951d;">)</span><span style="color: #67b11d;">)</span>
<span style="color: #2d9574;">)</span>
<span style="color: #bc6ec5;">)</span>
dispatch<span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">car-p</span> z<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>z <span style="color: #a45bad;">0</span><span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">define</span> <span style="color: #bc6ec5;">(</span><span style="color: #bc6ec5; font-weight: bold;">cdr-p</span> z<span style="color: #bc6ec5;">)</span> <span style="color: #bc6ec5;">(</span>z <span style="color: #a45bad;">1</span><span style="color: #bc6ec5;">)</span><span style="color: #4f97d7;">)</span>
</pre>
</div>
<p>
Zdaj imamo procedure za delanje s pari, ki so definirane brez podatkov.
Obskurno, ampak v okviru definije delanja s pari. Na tem primeru vidimo, da
zmoznost manipuliranja procedur kot objektov avtomaticno omogoci moznost za
reprezentacijo sestavljenih podatkov. (Proceduralna reprezentacija podatkov bo
igrala osrednjo vlogo v nadaljevanju - temu se rece <i>message passing</i> in bo
osnovno orodje v tretjem poglavju o problemih modeliranja in simulacije).
</p>
</div>
</div>
</div>
</div> </div>
<div id="postamble" class="status">
<p class="author">Author: Lio Novelli</p>
<p class="date">Created: 2024-05-29 Wed 21:50</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div> </div>
</body> </body>
</html> </html>

View File

@ -2,6 +2,10 @@
#+AUTHOR: Lio Novelli #+AUTHOR: Lio Novelli
* Foreword and Preface * Foreword and Preface
:PROPERTIES:
:UNNUMBERED: t
:END:
#+begin_quote #+begin_quote
Lisp je preživeli, v uporabi je že "polovico stoletja". Lisp je preživeli, v uporabi je že "polovico stoletja".
@ -31,7 +35,7 @@ natančno spoprijemanje s pojmovanjem "kaj je". Računanje pa ponudi okvir za na
ukvarjanje s pojmovanjem "kako". ukvarjanje s pojmovanjem "kako".
#+end_quote #+end_quote
* 1. Grajenje abstrakcij s procedurami * Grajenje abstrakcij s procedurami
** Elementi programiranja ** Elementi programiranja
@ -202,6 +206,8 @@ Normalni vrstni red: izvrsi test in pride v if, ki ne izvrsi drugega dela.
** 1.2.4 Eksponentna funkcija ** 1.2.4 Eksponentna funkcija
Tukaj se naucimu successive squaring, ki potem se veckrat prav pride.
#name: exponent #name: exponent
#+begin_src scheme #+begin_src scheme
;; O(n) korakov in O(n) prostora ;; O(n) korakov in O(n) prostora
@ -283,6 +289,43 @@ Normalni vrstni red: izvrsi test in pride v if, ki ne izvrsi drugega dela.
(else (fast-multi-iter a (- b 1) (+ s a))) (else (fast-multi-iter a (- b 1) (+ s a)))
) )
) )
;; excercise 1.19 - fast fibnonachi
(define (fast-fibo n)
(fast-fibo-iter 1 0 0 1 n)
)
(define (fast-fibo-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fast-fibo-iter
a
b
(+ (* p p) (* q q))
(+ (* q q) (* 2 p q))
(/ count 2)
)
)
(else (fast-fibo-iter
(+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)
))
)
)
;; p' = q^2 + 2pq
;; p' = p^2 + q^2
(define (slow-fibo n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+
(slow-fibo (- n 1))
(slow-fibo (- n 2))
))
)
)
;; melje melje in melje . fast-fibo iypljune takoj
#+end_src #+end_src
** 1.2.5 Najvecji skupni deljitel ** 1.2.5 Najvecji skupni deljitel
@ -544,3 +587,232 @@ moč programskega jezika. Najvišja cena pri implementaciji procedur s
prvorazrednim statusom je, da je potrebno rezervirati prostor za procedurine prvorazrednim statusom je, da je potrebno rezervirati prostor za procedurine
proste spremenljivke tudi, ko se procedura ne izvaja. V scheme-u so te proste spremenljivke tudi, ko se procedura ne izvaja. V scheme-u so te
spremenljivke shranjene v procedurino okolje (poglavje 4.1). spremenljivke shranjene v procedurino okolje (poglavje 4.1).
* Grajenje absrakcij s podatki
Poglavje bo govorilo o kompleksnih podatkih. Poglavje 1 govori o grajenju
abstrakcij z zdruzevanjem procedur, ki tvorijo sestavljene procedure (compound).
V poglavju 2 pa bo fokus na grajenju abstrakcij z zdruzevanjem podatkovnih
objektov v sestavljene podatke (compound).
Z zdruzenimi podatkovnimi objekti lahko procedure delajo nad njimi ne da bi bile
odvisne od njihove natancne strukture.
Podobno kot pri sestavljenih procedurah gre tudi pri sestavljenih podatkovnih
objektih za nacin spoprijemanja s kompleksnostjo - podatkovne abstrakcije
omogocijo postavitev primernih abstrakcijskih pregrad med razlicnimi deli
programa.
Napoved, kaj se bo pregledalo v 2. poglavju (bi bilo smiselno povzet).
** Uvod v podatkovne abstrakcije
Podatkovna abstrakcija je metodologija, ki nam omogoci, da locimo kako so
sestavljeni podatki uporabljeni od detajlov o tem, kako so izgrejeni iz
primitivnih podatkovnih objektov. (To je analogno grajenju produceur, ki imajo
vgrajene druge procedue iz poglavja 1.1.8)
Skratka programe hocemo graditi tako, da uporabljajo podatke na nacin, da nimajo
nobenih predpostavk o tem, kaksni naj so ti podatki (oziroma cim manj), ravno
dovolj za izvajanje potrebnih operacij. Hkrati so konkretne reprezentacije
podatkov definirane neodvisno od programov, ki podatke uporabljajo.
Selektorji in konstruktorji.
*** Aritmeticne operacije z racionalnimi stevili
#+begin_src scheme
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x))
)
(* (denom x) (denom y))
)
)
(define (sub-rat x y)
(make-rat (- (numer x) (denom y)
(numer y) (denom x)
)
(* (denom x) (denom y))
)
)
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))
)
)
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y)))
)
(define (equal-rat? x y)
(= (* (numer x) (denom y))
(* (numer y) (denom x))
)
)
(define (make-rat n d) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x))
(newline)
)
(define one-half (make-rat 1 2))
(define one-third (make-rat 1 3))
;; excercise 2.1
(define (make-rat-norm n d)
(if (< d 0)
(make-rat (* n -1) (* d -1))
(make-rat n d)
)
)
#+end_src
Sestavljena strkutura ~par~, ki je konstruirana s primitivno proceduro ~cons~. S
primitivnimi procedurami ~car~ in ~cdr~ lahko dobimo prvi in ostale elemente
para.
**** Predstavljanje racionalnih stevil
:PROPERTIES:
:UNNUMBERED: t
:END:
/Glej zgornji codeblock./
*** Pregrade abstrakcij
Splosna ideja podatkovnih abstrakcij je, da se identificira za vsak tip podatka
osnovni set opraracij, s katerimi bodo vse procedure, ki bodo manipulirale
podatke operirale, oziroma bodo iz njih sestavljene. Nato se uporabljamo samo te
operacije pri delu s podatki.
Pregrade:
- programi, ki uporabljajo racionalna stevila
- racionalna stevila v problemskem polju
- ~add-rat~, ~sub-rat~ ...
- racionalna stevila kot stevci in imenovalci
- ~make-rat~, ~number~, ~denom~
- racionalna stevila kot pari
- ~cons~, ~car~, ~cdr~
- kakor so pac pari implementirani
Procedure na vsakem nivoju so vmesniki, ki definirajo abstrakcijski nivo in med
sabo povezujejo razlicne nivoje.
Ena od prednosti razdelitve na nivoje je, da je programe lazje vzdrzevati in
spreminjati, ker lahko delas spremembe na posameznem nivoju, ki ne vplivajo
izven svojega nivoja.
/vaja 2.2/
#+begin_src scheme
;; crte v prostoru
(define (make-segment startp endp)
(cons startp endp)
)
(define (make-line x1 y1 x2 y2)
(make-segment (make-point x1 y1) (make-point x2 y2))
)
(define (start-segment segment)
(car segment)
)
(define (end-segment segment)
(cdr segment)
)
(define (make-point x y)
(cons x y)
)
(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (mid-point segment)
(make-point
(/ (+ (x-point (start-segment segment)) (x-point (end-segment segment))) 2)
(/ (+ (y-point (start-segment segment)) (y-point (end-segment segment))) 2)
)
)
(define (print-point p)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")")
(newline)
)
;; vaja 2.3 :: segment je lahko tudi pravokotnik, ce nimamo rotacije in je crta
;; vedno diagonala. Delal bom brez rotacije, zato ker potem ni dovolj imeti
;; konstruktorja, ki je samo kons, ampak rabim 3 parametre, segment in rotacija
;; in potem nvm kako delat selektorje in pa se vse se mi zakomplicira in se mi
;; ne da, ker je nedelja zvecer.
;; brez rotacije - 2a + 2b
(define (perimeter rectangle)
(+ (* 2 (side-a rectangle)) (* 2 (side-b rectangle)))
)
(define (area rectangle)
(* (side-a rectangle) (side-b rectangle))
)
;; selektor (brez rotacije)
(define (side-a rectangle)
(abs (- (x-point (start-segment rectangle)) (x-point (end-segment rectangle))))
)
(define (side-b rectangle)
(abs (- (y-point (start-segment rectangle)) (y-point (end-segment rectangle))))
)
;; sedaj vpeljemo drugo reprezentacijo pravokotnikov (nic vec s segmentom)
;; ali lahko obseg in ploscina se vedno delujeta?
;; odvisna sta od side-a in side-b. Ce to zemanjam, bosta obseg in ploscina se
;; vedno delovali.
#+end_src
*** Kaj so podatki?
Pri racionalnih stevilih imamo se en pogoj:
~(/ (numer x) (denom x)) = n/d~
Selektorji konstruktorji in pogoji tvorijo veljavno reprezentacijo.
Vsaka trojica procedur, ki ustreza pogoju, da ce zdruzis dva objekta, in potem z
eno proceduro dobis iz zdruzenih prvi objekt in z drugo drugi objekt, je potem
trojica procedu za delanje s pari.
Trojico procedur (cons, car, cdr) se da implementirati brez podatkov:
#+begin_src scheme
(define (cons-p x y)
(define (dispatch m)
(cond
((= m 0) x)
((= m 1) y)
(else (error "Argument not 0 or 1 -- CONS" m))
)
)
dispatch)
(define (car-p z) (z 0))
(define (cdr-p z) (z 1))
#+end_src
Zdaj imamo procedure za delanje s pari, ki so definirane brez podatkov.
Obskurno, ampak v okviru definije delanja s pari. Na tem primeru vidimo, da
zmoznost manipuliranja procedur kot objektov avtomaticno omogoci moznost za
reprezentacijo sestavljenih podatkov. (Proceduralna reprezentacija podatkov bo
igrala osrednjo vlogo v nadaljevanju - temu se rece /message passing/ in bo
osnovno orodje v tretjem poglavju o problemih modeliranja in simulacije).