Notes: Lisp Example

Math 61 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 61 Homepage | Return to Notes Listing Page]

Contents


Example of a LISP Iterative Factorial Routine

Old Style Loop, using prog and go

(DEFUN FACT (X)
     (PROG (ANS)
	     (SETQ ANS 1)
	LOOP (COND ((ZEROP X) (RETURN ANS))
		    (T      (SETQ ANS (* ANS X))
			    (SETQ X (1- X))
			    (GO LOOP))
	      )
      )
 )

New Style Loop, using DO

(DEFUN FACT (X)
     (DO ((TEMP X (SETQ X (1- X)))  ;initialize TEMP and prescribe step
	  (ANS 1)                   ;initialize ANS
          )
	 ((ZEROP X) ANS)            ;end test and return value
	 (SETQ ANS  (* ANS X))      ;loop body action
       )
 )

New Style Loop, using DOTIMES

(DEFUN FACT (X)
     (SETQ ANS 1)
     (DOTIMES (I X ANS)
	  (SETQ ANS (* (1+ I) ANS))
       )
 )

New Style Loop, using LOOP

(DEFUN FACT (X)
     (SETQ ANS 1)
     (LOOP 
	   (SETQ ANS (* X ANS))
	   (SETQ X (1- X))
	   (WHEN (ZEROP X) (RETURN ANS))
      )
)

Helpful Unix Commands

"script" Command

When running interactive LISP sessions it is helpful to make use of the standard Unix script command. This command opens a log file and copies everything that appears on the screen into the file. By default, the name of the logfile is typescript but any filename can be added after the command by the user.

To end a script session, one types exit at the system prompt.

"fold" Command

When computing large integer numbers using LISP, that is numbers that are displayed on several lines on the screen, one should note that such a number actually is on only one electronic line. In other words, the line breaks that one sees on the screen which causes the number to be printed on several lines are not actually saved in any script file and, thus, the number would be printed only on one line on a printer (i.e., the first line would appear and digits on other lines would be lost!).

To force carriage returns into such a file, one can make use of the Unix fold command. After the command fold one adds the -w switch (for "Width") and adds a number indicating the number of characters per line in the output. This is followed by the input file to be "folded," followed, in turn, by the > (redirection symbol) and an output file. As an example, if file1 had long lines in it and you wished to create a new file, e.g., file2 with identically the same information but lines no longer than 60 characters per line, at the system prompt you would enter the following command:

     math 47:fold -w 60 file1 > file2

Execution of the Iterative Factorial Routine

math 54: script logfile1
Script started on Wed Apr  7 16:33:39 1999
math 21: lisp
AKCL (Austin Kyoto Common Lisp)  Version(1.619) Thu Oct  7 15:46:49 PDT 1993
Contains Enhancements by W. Schelter

>(load "fact.lsp")
Loading fact.lsp
Finished loading fact.lsp
T

>(fact 100)
9332621544394415268169923885626670049071596826438162146859296389521759
9993229915608941463976156518286253697920827223758251185210916864000000
000000000000000000

>(fact 2000)
3316275092450633241175393380576324038281117208105780394571935437060380
7790560082240027323085973259225540235294122583410925808481741529379613
1386633526343688905634058556163940605117252571870647856393544045405243
9574670376741087229704346841583437524315808775336451274879954368592474
0803240894656150723325065279765575717967153671868935905611281587160171
7232657156110004214012420433842573712700175883547796899921283528996665
8534055798549036573663501333865504011720121526354880382681521522469209
9520603156441856548067594649705155228820523489999572645081406553667896
9532101467622671332026831552205194494461618239275204026529722631502574
7520482960647509273941658562835317795744828763145964503739913273341772
6360885249009350662161014445970941270782131373256383157230201994991495
8316470942774473870327985549674298608839376326824152478834387469595829
2577405745398375015858154681362942179499723998135994810165565638760342
2731291225038470987290962662246197107660593155020189513558316535787149
2290916779049702247094611937607785165110684432255905648736266530377384
6503907880495246007125494026145660722541363027549136715834060978310749
4528221749078134770969324155611133982805135860069059461996525731074117
7081519922564516778571458056602185654760952377463016679422488444485798
3498015480326208298909658573817518886193766928282798884535846398965942
1395298446529109200910371004614944991582858805076186792494638518087987
4512891408019340074625920057098729578599643650655895612410231018690556
0603087836291105056012459089983834107993679020520768586691834779065585
4470014869265692463193333761242809742006717284636193924969862846871999
3450393889367270487127172734561700354867477509102955523953547941107421
9133013568195410919414627664175421615876252628580898012224438902486771
8205495941575199170127176757178749586161966593187885514183578209260148
2071777331735396034304969082070589958701381980813035590160762908388574
5612882176981361824835767392183031184147191339868928423440007792466912
0976673165143349443747323563657204884447833185494169303012453167623274
5367879322847473824485092283139952509732505979127031047683601481191102
2292533726976938236700575656124002905760438528529029376064795334581796
6612383960526254910718666386935476610845504619810208405063582767652658
9492393249519685954171672419329530683673495544004586359838161043059449
8266275306054235807558941082788804278259510898806354105679179509740177
8068878286981021901090014835206168888372025031066592206860148364983053
2782088263536558043605686781284169217133047141176312175895777122637584
7531235172309905498292101346873042058980144180638753826641698977042377
5940628087725370226542653058086237930142267582118714350291863763634030
0173251818262076039747369595202642632364145446851113427202150458383851
0101369413130348562219166316238926327658153550112763078250599691588245
3345743543786368317373067329658935519969445823687350883027865770087974
9889992343555566240682834763784685183844973648873952475103224222110561
2012958296571913681086938254757641188868793467251912461921511447388362
6959164367249007165342822815266124780046392254494517036372362794075778
4542091048305461656190622174286981602973324046520201992813854882681951
0072828697010707375009276664875021747753727423515087482467202741700315
8112280589617812216074743794751095062093855667458125251837668215771280
7861499255876132352950422346387878954850885764466136290394127665978044
2020922813379871159008962648789424132104549250035666706329094415793729
8674342147050721358893201958072306478149842952259558901275482397177332
5722910325760929790733299545056388362640474650245080809469116072632087
4941439730007041114185955302788273576548191820024496977611113463181952
8276159096418979095811733862720608891043294524497853514701411244214305
5486089639578378347325323595763291438925288393986256273242862775563140
4638303891684216331134456363095719659784663385514923161963356753551384
0342580416291983782226690952177015317533873028461084188655413832917195
1332117895728541662084823682817932512931237521541926970269703299477643
8233864830088715303734056663838682940884877307217622688490230849346611
9426018027261380210800507821574100605484820134785957810277070778065551
2772540501674332396066253216415004808772403047611929032210154385353138
6855384864255707907953411765195711886837398806838957927437496834981429
2329219630977709014393684365533335930782018131299345502420604456334057
8606962471961505603394899523321800434359967256623927196435402872055475
0120798543319706747973131268135236537440856622632067688375851327828962
5233328434181297762469707954343600349234315923967476363891211528540665
7783646213911247447051255226342701239527018127045491648045932248108858
6746009523067931759677555810116799400052498063037631413444122690370349
8735579991600925924807505248554156826628176081544630830540667741263012
4441864204108373119093130001154470560277773724378067188899770851056727
2767812471988328576958442175888951604678682048100100478164623582208385
3248813427083407986848663216272020882330872781908537884546913155602172
8873121907393965209260229101477527080930865364979858554010577450279289
8146036884318215086372462169678722821693473705992862771124476909209029
8832016683017027342025976567170986331121634950217126442682711965026405
4228231759630874475301847194095524263411498469508073390080000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000

>(bye)
Bye.
math 22: exit
script done on Wed Apr  7 16:35:02 1999
math 55: fold -w 70 logfile1 > logfile170

This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 7 April 1999.