sicp/zapiski/sicp_sec_1_2.html

538 lines
12 KiB
HTML

<?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-04-30 tor 19:39 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>&lrm;</title>
<meta name="author" content="Yuri" />
<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">
<p>
#lang scheme
</p>
<p>
;; 1.9
</p>
<p>
;; 1. primer: rekurziven
;; (+ 4 5) = inc((inc(inc(inc(5)))) = inc((inc(inc(6))) = inc((inc(7)) = inc(8) = 9
;; 2. primer: iterativen
;; (+ 4 5):
;; (+ 3 6)
;; (+ 2 7)
;; (+ 1 8)
;; (+ 0 9)
;; 9
</p>
<p>
;; 1.10
;; Ackermannova funkcija
;; <a href="https://en.wikipedia.org/wiki/Ackermann_function">https://en.wikipedia.org/wiki/Ackermann_function</a> - tukaj malo drugacna definicija, mislim, da se indeksiranje premakne za 3
</p>
<p>
( define (A x y)
( cond ((= y 0 ) 0 )
(( = x 0 ) ( * 2 y))
((= y 1 ) 2 )
( else (A ( - x 1 )
( A x ( - y 1 ) ) ) ) ) )
</p>
<p>
(A 1 10 )
(A 2 4)
(A 3 3)
</p>
<p>
;; (f n) = 2*n
;; (g n) = 2<sup>n</sup>
;; (h n) =
</p>
<p>
;;;; iz knjige
;;;;
(define (fib n)
( cond (( = n 0 ) 0)
(( = n 1) 1 )
( else ( + ( fib ( - n 1 ) )
( fib ( - n 2 ) ) ) ) ) )
</p>
<p>
(fib 10)
;;;;
;;;;
</p>
<p>
;; 1.11
</p>
<p>
;; rekurzivno
(define (f-rec n)
( cond (( &lt; n 3 ) n)
( else ( + (f-rec ( - n 1 )) (* 2 (f-rec (- n 2))) (* 3 (f-rec (- n 3)))))))
</p>
<p>
(f-rec 6)
</p>
<p>
;;;; iz knjige
;;;;
( define (fib2 n)
( fib-iter 1 0 n) )
</p>
<p>
( define ( fib-iter a b count )
( if (= count 0 )
b
( fib-iter (+ a b) a ( - count 1 ) ) ) )
</p>
<p>
(fib2 10)
;;;;
;;;;
</p>
<p>
;; iterativno
( define (f2 n)
( f-iter n (if (&lt; n 2) n 2)) )
</p>
<p>
( define ( f-iter max-count sum)
( if (&lt; max-count 3 )
max-count
(+ (f-iter (- max-count 1) sum) (* 2 (f-iter (- max-count 2) sum )) (* 3 (f-iter (- max-count 3) sum )))))
</p>
<p>
(f2 6)
</p>
<p>
;; 1.12
(define (pascal r c)
(if (or (= r 0) (= c 1) (= c r))
1
(+ (pascal (- r 1) c) (pascal (- r 1) (- c 1)))))
</p>
<p>
(pascal 5 3)
</p>
<p>
;; 1.13 na papirju
</p>
<p>
;; 1.14 na papirju
</p>
<p>
;;;; iz knjige
;;;;
( define ( count-change amount )
( cc amount 5 ) )
</p>
<p>
( define ( cc amount kinds-of-coins )
( cond (( = amount 0) 1 )
(( or (&lt; amount 0 ) ( = kinds-of-coins 0 ) ) 0 )
( else (+ ( cc amount
( - kinds-of-coins 1 ) )
( cc ( - amount
( first-denomination kinds-of-coins ) )
kinds-of-coins ) ) ) ) )
(define ( first-denomination kinds-of-coins)
( cond (( = kinds-of-coins 1 ) 1)
(( = kinds-of-coins 2 ) 5)
(( = kinds-of-coins 3) 10 )
(( = kinds-of-coins 4) 25 )
(( = kinds-of-coins 5 ) 50) ) )
</p>
<p>
( count-change 11)
;;;;
;;;;
</p>
<p>
;;;; iz knjige
;;;;
(define (square x) (* x x))
</p>
<p>
( define ( even? n)
(= (remainder n 2 ) 0 ) )
</p>
<p>
( define ( fast-expt b n)
( cond ( (= n 0 ) 1 )
( ( even? n ) ( square ( fast-expt b ( / n 2 ) ) ) )
( else ( * b ( fast-expt b ( - n 1 ) ) ) ) ) )
</p>
<p>
(fast-expt 2 5)
;;;;
;;;;
</p>
<p>
;; 1.15
;; a) 5
;; b) O(log n) (?)
</p>
<p>
;; 1.16
( define ( fast-exp-iter n b a )
( if (= n 0 )
a
(if (even? n) (fast-exp-iter ( / n 2 ) (square b) a ) ( * b ( fast-exp-iter ( - n 1 ) (* a b) a ) ) ) ) )
</p>
<p>
(fast-exp-iter 10 2 1 )
</p>
<p>
;; 1.17
</p>
<p>
;;;; iz knjige
;;;;
(define (mult a b)
(if (= b 0)
0
(+ a (mult a (- b 1 ) ) ) ) )
</p>
<p>
(mult 3 4)
;;;;
;;;;
</p>
<p>
;; naloga
(define (double x) (+ x x))
(define (halve x) (/ x 2)) ;; a bi to moralo biti kako drugace; zakaj uporabljam deljenje pri implementaciji mnozenja s sestevanjem
</p>
<p>
( define ( fast-mult a b)
( cond ( (= b 0 ) 0 )
( ( even? b ) ( double ( fast-mult a (halve b) ) ) )
( else ( + a ( fast-mult a ( - b 1 ) ) ) ) ) )
</p>
<p>
(fast-mult 8 7)
</p>
<p>
;; 1.18
</p>
<p>
( define ( fast-mult-iter b a c)
( if (= b 0 )
0
(if (even? b) (fast-mult-iter (halve b) (double a) c ) ( + a ( fast-mult-iter ( - b 1 ) a c ) ) ) ) )
</p>
<p>
(fast-mult-iter 18 10 0 )
</p>
<p>
;; 1.21
</p>
<p>
;;;; iz knjige
;;;;
( define ( smallest-divisor n)
( find-divisor n 2 ) )
( define (find-divisor n test-divisor)
( cond (( &gt; ( square test-divisor) n) n)
(( divides? test-divisor n) test-divisor)
(else ( find-divisor n (+ test-divisor 1 ) ) ) ) )
( define (divides? a b )
(= (remainder b a) 0 ) )
</p>
<p>
(smallest-divisor 19999)
</p>
<p>
( define (prime? n)
(= n ( smallest-divisor n) ) )
;;;;
;;;;
</p>
<p>
;; 1.22
</p>
<p>
(define (runtime) (current-milliseconds)) ;; brez tega error: runtime: unbound identifier in: runtime
</p>
<p>
;;;; iz knjige
;;;;
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time)) -1))
(define (report-prime elapsed-time)
(display "<b>*</b>" )
(display elapsed-time))
;;;;
;;;;
</p>
<p>
(timed-prime-test 87178291199) ;; 35742549198872617291353508656626642567
</p>
<p>
;; <a href="https://en.wikipedia.org/wiki/List_of_prime_numbers">https://en.wikipedia.org/wiki/List_of_prime_numbers</a>
</p>
<p>
;; 1.26
;; ker se s klicanjem (/ exp 2) v navadnem mnozenju parameter exp ne razpolovi v naslednjem koraku ?</p>
</div>
<div id="postamble" class="status">
<p class="author">Author: Yuri</p>
<p class="date">Created: 2024-04-30 tor 19:39</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>