Revision of 1.2.1 Linear Recursion and Iteration from 11 July 2009 - 10:03pm

The revisions let you track differences between multiple versions of a post.

Printer-friendly versionPrinter-friendly version

Exercises

Exercise 1.9

Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1.

(define (+ a b)
  (if (= a 0)
      b
      (inc (+ (dec a) b))))

(define (+ a b)
  (if (= a 0)
      b
      (+ (dec a) (inc b))))

Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive?

Exercise 1.10

The following procedure computes a mathematical function called Ackermann’s function.

(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))

What are the values of the following expressions?

(A 1 10)

(A 2 4)

(A 3 3)

Consider the following procedures, where A is the procedure defined above:

(define (f n) (A 0 n))

(define (g n) (A 1 n))

(define (h n) (A 2 n))

(define (k n) (* 5 n n))

Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n. For example, (k n) computes 5n2.

Comments

Clojure version of iterative factorial

Clojure doesn’t have tail recursion due to limitations with the JVM so you have to explicitly use ‘recur’:

(defn factorial [n]
  (fact-iter 1 1 n))
(defn fact-iter [product counter max-count]
  (if (> counter max-count)
    product
    (recur (* counter product)
                (+ counter 1)
                max-count)))

TXPFqcqogM

Very true! Makes a change to see soomnee spell it out like that. :)

http://www.rateamateapp.com

http://xn—ghq332m.com/bbs/forum.php?mod=viewthread&tid=151917 http://jwc.dongyueweb.com/forum/forum.php?mod=viewthread&tid=672752 http://bbs.storberry.com.cn/forum.php?mod=viewthread&tid=906494 http://www.cslife.org/forum.php?mod=viewthread&tid=597431 http://three-body.com/forum.php?mod=viewthread&tid=256136 http://mail.eachina.org.cn:8080/upload/forum.php?mod=viewthread&tid=2213906 http://longmen.6macau.com/forum.php?mod=viewthread&tid=229887 http://yoyaer.com/forum.php?mod=viewthread&tid=1043581 http://dz.aqualem.com/forum.php?mod=viewthread&tid=313319 http://bbs.xarg.com.cn/forum.php?mod=viewthread&tid=597811 http://imperiusdamian.x10host.com/wiki/index.php/User:Djvl44pg#camisetas… http://bbs.77w.net/forum.php?mod=viewthread&tid=616353 http://uctest.ucweb.com:81/discuzx2/forum.php?mod=viewthread&tid=109105 http://www.51nitian.com/thread-727899-1-1.html http://zk.duerain.com/sns/forum.php?mod=viewthread&tid=595836 http://www.345wan.com/bbs/forum.php?mod=viewthread&tid=126564 http://bbs.fzycw.net/forum.php?mod=viewthread&tid=806921 http://www.c-kf.com/bbs/forum.php?mod=viewthread&tid=186046 http://igoviva.com/forum.php?mod=viewthread&tid=50830 http://mmwk.cn:8088/1/tzb/forum.php?mod=viewthread&tid=511320 http://by78.com/forum.php?mod=viewthread&tid=399816 http://www.bjsfedu.com/forum.php?mod=viewthread&tid=353594 http://www.pingguo4.com/forum.php?mod=viewthread&tid=396585 http://xa.hengzhibs.com/forum.php?mod=viewthread&tid=116279 http://bbs.51mainet.com/forum.php?mod=viewthread&tid=26621 http://www.yoyaer.com/forum.php?mod=viewthread&tid=1043588 http://bbs.xjeep.cn/forum.php?mod=viewthread&tid=515426 http://www.yxqnlxx.cn/Review.asp?NewsID=38 http://www.roomie.com.cn/forum.php?mod=viewthread&tid=67105 http://26w.com.cn/forum.php?mod=viewthread&tid=371814 http://bbs.ymxx.mhedu.sh.cn/forum.php?mod=viewthread&tid=11788489 http://www.islesofdoom.com/wiki/index.php?title=User:Nuaa04sa#Cheap_Socc… http://h.znovels.com/space.php?uid=357314&do=blog&id=2373347 http://lnkjoy.com/bbs/forum.php?mod=viewthread&tid=947889 http://www.buji8.com/forum.php?mod=viewthread&tid=1380707 http://jetmotoboat.com/forum.php?mod=viewthread&tid=514364 http://bbs.weifangcn.com/thread-434507-1-1.html http://vibebbs.com/forum.php?mod=viewthread&tid=970446 http://118.244.203.38/thread-626701-1-1.html http://www.wx0.net/forum.php?mod=viewthread&tid=91784 http://www.face-pic.org/blog/26738/football-shirts-uk-380f6h6-41020/ http://www.islesofdoom.com/wiki/index.php?title=User:WdcoxwgN#Soccer_Jer… http://www.zoom51.com/forum.php?mod=viewthread&tid=89699 http://www.jlxshx.com/Review.asp?NewsID=433 http://ncut.me/forum.php?mod=viewthread&tid=335719 http://www.myokez.tw/forum.php?mod=viewthread&tid=1342970 http://bbs.51xingtai.com/forum.php?mod=viewthread&tid=802962 http://www.51liuxue.ca/forum.php?mod=viewthread&tid=515294 http://0769yjl.com/forum.php?mod=viewthread&tid=738016 http://raegquit.com/wiki/index.php?title=User:YpreuuBc#camiseta_barcelon… http://www.528club.com/forum.php?mod=viewthread&tid=2439474 http://club.xiangqinba.com/home/space.php?uid=406610&do=blog&id=3534105 http://110.90.120.57:88/forum.php?mod=viewthread&tid=1543730 http://xbzhaopin.com/forum.php?mod=viewthread&tid=1303794 http://www.hebidianjing.com/web/thread-237317-1-1.html http://www.njhdwj.com/read.php?tid=242469 http://jk578.com/read.php?tid=3437215 http://www.rosemeadchamber.org/index.php?do=/blog/add/ http://www.9xchem.com/bbs/home/space.php?uid=149714&do=blog&id=1051894 http://zaojiao.siai.co/forum.php?mod=viewthread&tid=65252 http://www.amiella.com/forum.php?mod=viewthread&tid=212673 http://www.dtstudyclub.com/index.php?do=/blog/add/ http://www.nss184xx.com/Review.asp?NewsID=497 http://goldfishhome.gicp.net/uchome/space.php?uid=306223&do=blog&id=1407478 http://philipsokolov.com/phpbb/viewtopic.php?p=436422#436422 http://www.nnro-pran.ru/forum/viewtopic.php?p=42262#42262 http://bbs.eeu.cn/read-htm-tid-416927.html http://humaraadab.com/wiki/index.php?title=User:IyuhghvN#Soccer_Jerseys_… http://olveczkylab.fas.harvard.edu/OpCon/index.php?title=User%3AG80bsyb0… http://shamanita.org/wiki/index.php?title=User:ArhphkxN#Cheap_Football_S…
http://www.rateamateapp.com http://www.rateamateapp.com

1.2.1 Linear Recursion and Iteration | SICP in Clojure

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my
newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you
would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy
reading your blog and I look forward to your new updates.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <hr> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options