2014年1月30日木曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の1(手続きによる抽象の構築)、1.2(手続きとその生成するプロセス)、1.2.4(べき乗)、問題 1.16.を解いてみる。

その他参考書籍

問題 1.16.

コード(BBEdit, Emacs)

sample.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

(define (square x) (* x x))

(define (expt b n)
  (expt-iter b n 1))

(define (expt-iter b n a)
  (cond ((= n 0) a)
        ((even? n) (expt-iter (square b) (/ n 2) a))
        (else (expt-iter b (- n 1) (* a b)))))

(define (even? n)
  (= (remainder n 2) 0))

入出力結果(Terminal, REPL(Read, Eval, Print, Loop))

$
gosh> (load "./sample.scm")
#t
gosh> (expt 2 0)
1
gosh> (expt 2 1)
2
gosh> (expt 2 2)
4
gosh> (expt 2 3)
8
gosh> (expt 2 4)
16
gosh> (expt 2 5)
32
gosh> (expt 2 10)
1024
gosh> (expt 2 11)
2048
gosh> (expt 2 31)
2147483648
gosh> (expt 2 63)
9223372036854775808
gosh> (expt 2 10000)
19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376
gosh> (expt 2 10001)
39901262337615167697674843253671701676469936637723849097040178997058877660443893263839923368072389195798662258846418248543112982698827562235187571864192647915711460093587589053530493102532119791041100173836386623085017216921236209371018149732179249776180979789676018507883266515701243136618947805113824776130450193287748882093519743253970906445737076323388631551259281525673761521464457070183282952367912762917938927798821681921072535642129242854666788073051131299061206285360469938800671868633302918595546559331551212345164062815988396359214756491367524560074605770974503801668929162909301115859202829667843231469176278514190759538238555601653915471348888246124037515672651005456647578541420747605732786062856266482803248391343381148122839308684649277602497712294610414863984519223592500261985720483416681615211864640322536984576992511682625688123073477902974228512630222179491028406627640405863281915192929512020811691683132144089925734033030123841262008372844551817341801149212835713903822912110136502500812015039684523796118474236108889576145812790485096678443965414808946324753521693226067557412079606826394266987309245401126339874911016483561945621966582628807143755049537019714553875852866443198799753773321616737675676055286565550344547315145489568224588779467621723214846506583949626240395208356563931394951796329062516868271919725568260256370812566953298177381042095161765231647923971540244814088661166151738078639209206809946313166417344211826601807505646831079490788795430514910581020424621894643221506949651481550547972696596996681513875911293277243749138998558033144207402728866271634428623582796445967691694668880541928365702010145855496729101157269002201705975624778947857399081668692317614087918237971630291558354287239397456262918967566404162949964343716022778142456501811653634872441154951842835307431375451229809165809984922057260163071166616260203975351712468687077910818351246801689775052325287137297667038927440754586480188912493846508700801356054547675510752813453797272482074982821933437114101518196200493579760356543851906762564843908056605518816897910029353336779395993772483272626752787806746911602815273483755422110768451478998220372936439393163302970260988444739895429526138310936435365752400725554515447562730663222393622561585338963774402597287321537103279721069204595743115035894770492738893846175788531896434016102240644730992576338071478242736676787183512837467701021940543227830879181983196309308834672623313872062244499875939998453563464716046223725289150598271516350016399678472569230499762177920464488724347543236172714030936968117244659585707751246973112881073925244037927142057624723135025086676606540058195337301137114315011033455037798388259422675380299832362630343088015457301146379114901840660370609694227636630814648106638076924168072843527407823101279578001485707344392561806955949066640936737591737160475904437258240161485639102635896315248896597036923019409776054549443149376263189500819464230160996380911606833653899574282632126421372783023363548609585193418752
gosh> (exit)
$

0 コメント:

コメントを投稿