Lisp: Exploring some commonly-used math operations

From CodeMinima <Code Concised>
Jump to: navigation, search
I LOVE CODEMINIMA!

Lisp: Exploring some commonly-used math operations

Contents

Introduction

Lisp has many math functions which we will explore in the code snippet/s on this page. Lisp's math capabilities extend from very basic arithmetic operators till composite functions that do complex arithmetic operations.

Tutorial context

To be able to fully understand this code sample and for approaching this topic as a full-blown tutorial, we would recommend that you have a look at the following code samples too and proceed in a sequential way till the end.

Backward & forward links

Prerequisites

  1. You need to have Lisp installed on your system. Please see topic on how to install Lisp on Windows.

Running the example code

  1. Open the GNU Common Lisp command line interpreter and directly type the code in the code sample below.
  2. The > at the beginning of the line indicates the interpreter prompt; the code has to be typed in after that.
  3. Type the code and simply press the enter key to see the output.
  4. In case of any typo you will get an error prompt; just type :q to exit.

Code

Some simple arithmetic operations are listed below:

  1. > (+ 3 2)
  2. > (+ 3 2 1)
  3. > (* 3 2)
  4. > (/ 100 4 5)
  5. > (/ 1 3)

Output

5
6
6
5
1/3

Explanation

Bear in mind that while learning Lisp we will be working with parentheses-delimited lists for the most part. So when we say (+ 3 2), think of it as so: a list with three elements: the plus sign (+ character), the integer 3, and the integer 2. The first symbol represents the operation and the rest of the elements of the list represent the arguments that will go towards that operation. So in the previous sample (Lisp: A simple "Hello world" program) in the line (print "Hello world") we had two symbols within the list, print and the string Hello world. Back to our line of code, we see that Lisp's addition function adds up the two and presents the output, 5.

Of course, the number of arguments defined for a particular operation need not be restricted to just one or two parameters, they can be more as we will see in the next line of code. In the next line (+ 3 2 1) we have the addition operation with three arguments that we pass to it, namely, 3, 2, and 1; the addition operation summarily adds up all three of them and the REPL prints the output, 6.

In the next two lines we do multiplication and division operations respectively. (* 3 2) gives us the product of 3 and 2, that is 6, while (/ 100 4 5) works in a very obvious way: it first divides 100 by 4 to yield 25, which in turn is further divided by 5 to give the result 5.

The last line of code in the series is not so obvious; instead of giving us a decimal the division operator gives us a fraction with the numerator and denominator intact, that is 1/3.

Code

Some exponential operations are listed below:

  1. > (sqrt 100)
  2. > (sqrt 3)
  3. > (expt 10 3)

Output

10.0
1.7320508075688772
1000

Explanation

Lisp can be quite handy for operations involving both exponential operations and finding roots, as we see in the next three lines of Lisp code. The sqrt (square root) function yields the square root value of the parameter. In the case of 100, the square root is 10.0, while in the case of 3 it is 1.732, both of which we can see in the output. What you may be wondering about in this case is why the sqrt function returns a real type (see data types in computing) when we have passed it integer values. The answer seems to be quite obvious: for most numbers on the number line the square root tends to be a fraction.

The last line of code is quite interesting; the expt function finds the exponential value of the number passed as the first argument. In the case of (expt 10 3) we have a number (10) as well its exponent (3). Of course, the 10 raised to the power of 3 is 1000, which we see in the output.

Code

One thing to note is that Lisp's number crunching capabilities are quite remarkable. For example, in the next code sample we compute the value of 99999 raised to 99999. Have a look at the output. (When you run this line of code in your own Lisp interpreter, please be patient as it computes this... it may take a few dozen seconds or so.)

  1. > (expt 99999 99999)

Output

3678812805793780648191685775038060842147638043522668420548699136183546050974916474660127566092315813070763129679712073171206965616916387625616924576752663698576391763813405908
96464931186466803010250862751850967995483624261794695178018992812549068218558691723333030889780863536072879153205708905262307145552386625041980366102522383333587801341640950594
22264880205145138578018123809042727136908680355970046213215031021668069261799237011514923357555601592980018936409790529903020426928338299700610767862913667508088713668742962397
17158280436900067844588759779633204980944463496872840596678630180736102733298238383204066917448779377862582101321171534259794538373349342546726482955510997254689434999907461387
99909428036614502154321585011244431264653926761816010737539586616466362775671511673902631666379696001928798164552447137602376452826248250979866029290142059450453724265220605907
20665867813086760781812959892366220826996230441273033552174193497703583367194342249891888118969278222784740146360659512579707384922370029180176869551573928759492403222326365898
31233900435294686991200584571029848854734255125175575610663889039605027777150010397171310829153379219489544353733068192762168436999484896355407050255641302796197444509540931756
40698632616675543671903970671382317350489776509668926108636571942822840054880957919224053841909102721726258511158671347263199627684869354033512372815164281474834392208910331711
71678924241653343960655632984301538969268031621982088550878392672006062291296492881111780818526152554402072074387991358455555399683077730639686686535891648151928665077938905076
09249786453405773239498918326327173404403839244497783838959155383839067990671235502547520353004535410296134864638778775628304696162643893437281176722159086553588556509260850004
69532697368789549546768574432872177201805877077299299777045726869338541557559902188834626864933326520501093454103666424986492349187259869157452581498531513503545835619103923130
18535650961289042309726520064625646487201457500005000376524777208747226179157106939623146362628340636080179004148314514553593047185168789921123731615463706815332964604206226244
29843380681052781908168780180261070763922158620020812000012502397034874709170347264376592376015849682025840155371787672139549906153900084674166854679464537811522235409921028866
62199001901233340432807377474040030099260753522643841510954651297749544922922881689313856777405336887699522179471585675367751290677945926858934736843417504066275086320245927044
19378488444061827841872788133953711791906735276781658220860832679715589622607053807972697287979617182067315307330572078502401659744635370280313080607542643483290927808483321203
02941438910163101192736162191596650434284292286107245559828914554535154651881346205352625275710087876875228900791633471485863754447075845727238421104001547264588617064245436852
95805399258155597777653751842082576661846934713727231891786005894663865687829974937457247073205286487096662811100280541150109402555570275976861757235878999350357468563027760906
96903138730015077136100730656920796883392089197999208327519811808531607788251028670744640671723263070173747159502193757063921913402154921038175006108844054089207348551616979185
97531257929186822349223285950020119247309220356693539905067540836471772744615239670704374314977129591575560225457150212590540095868994155617806551801259618398130301561746886695
38796322695082653449732874241379115833802516845623535112140355477544807794792043382740171467399575479166408387506277810128681926510022485542313156662867002519390969336984832276
79940612332415560984359418737838827763444598425379864648149301321638723733285498174524682594899955906418108662767869551088392502471224608426168550797084715354719316630924501296
50622912789899440252054839108233172714234316097566323962664004869229570226108386825153642487946281264610051498000751399954610230210353257074308262522812918889817369635896246304
67462217767732898355051013121555881562784401978520881783797107493644793310685606834218327317708803402509517596104584615157266126944759985380120415323160853898699744297984286156
05065128478941022814708103010551207130257418759747533234681739996987240795192517489883998293576272808408537548668133140737464545724142548248980246838063019156578482010629140826
15289018627770203364658499764778492040898274527046357046768709257112223629919676864604991042330664170912166905561196841187306815225642950598725787173820323584620265598896091285
50218480098758834230058783528424281595008534045154873921274962895942631094152823886401247603320227695042266302756706734881544986862185760666937242502881391159813459651047
82588772174258824625100776407275694360982789308750630989405831039266309644463550161751049147179548477680374907594143500345298517684701358811051415100039574036302831106584478979
58193387499475956243162868338326161638621532252751681856857614514157016059404460176453326899934685053944600485842272692802387710656854769936053571014574614966842173450304968616
62744596983064992374945578906238998997434696997098905272630394051297375740621908395695581526776819974351543933586472170975711995979147228685087887657053866129731321140941897249
83188921193974440410524041830229689600339416232138509692150908773658302426465583660158786782919091651290334042796922676150585659050972140087831735805704556952728849003905030576
74460568618837046674953728917440141762299166982869917935883762616730054943300755675881887452907071134429699651821357721536019173607191360432405111773316529168139383171134103407
56889735031569935971006227645680077313826862825965195427018103395969170536903876486169922217283731724127167999368560635325025270811155065073942116155263242904922511948921742483
98210275983638170795221681619621095194207902195846961022369844155407519009559860861324597231069060767968567907017707558104768928669938399896826629266659629863657641886605444186
23841397681546819744341131740329489521283530050577921075221010123268398814302290954072823389753947951980823369146587252536030011556358327906315899944488658763424215287900318566
83996112493725185471116154918355367578930420490869286104547510829839696830698279289395798287600391315446120212245812451978430043553154350590272848364344299033586314161266638300
82167780841502571816950131668529088853725264989478386067049347043145936220417308122255450942882025023805006415101968570261882340490622220586814427994133020981035376395491929092
47663971320907096699716824710980440025871514056975290643642541291002813252235812294982553105163521565490876390015251932704411245577855754173726815135115994990290517573774444681
89959606740952610501526549197171629558065760349995565195750729005180956520590856776123951358856086964904808174124844604787331308323764342723519575308242957745333275078620171033
01741322655622845033407133440512393547202788117393106042772026959504967763985776453648730280916638405884855814517680772724078285732533083426571665983062708621169130979763833976
80990757839537208238931924567953948019763011666802660986830056967836253199014873712440826653749071001453357060639398225552610625760255480935856116431115747988255341516523637875
35802831935411831589224549355506919607880069114455681177060769249254938678318946329510285099255246733114418944819211697560729083566501740551298278780184455569346558454464200831
57555809602547428706865178839687946302362258659388954294346506826803494430744236024684092060376539634833386465673932452200472704019388153887315504637364217761322779762111672928
56066618622642781226779959566896332499636170008443303019768719027449242418510060405887236595909729788894685077284381509746638062129602809024864038638996487094871399721334158601
87378114476739260704977579180383145096718972699278508950952264391955364214384747823514058536918745967856669570021999405663088534529684842615325466977161293940575627534815868253
71969607851876428821160513083707076550263736896771009020188529302297472048744160249579321790649742244371780293160507457630855022904020533495521947008859046165845874126809902989
05428521883265089092962493361208798023276979406619271609631849677091462265246945500350738305596368826495939037220803798946469884790319963447813967829249332507039519809221411220
12573774882230377355266729617203388257170752658510688518510999936319900163379733824884103939998297268807199312856091874645986054896807275201672424794741724530371223915200813396
14962851143759973272957409310261550968557974615374218828664411955657708777802643641644899663649164123592509981021030840170297111917036211669150846847364435848895417181586251843
58371407130623164802344814903584225992462602061157430228265393657546695297404319843246295023073738887621299536375644867981122684367193119447602032687478716318172363764862066086
25490502068689506990973497161538301114386729552043779277533656257969127669903324467334036564817462123453884351678699748228595650060992837002132310114830417273434416932614230851
39458685529097730047142451548248314239704762256194026707325807168748187750507100674406499005617898956479864427214357597161549786413892112556644848033243069485722134708600048871
53358905607545212012349964027601874063409865160820497750043797586013613220028198972798246762937404761215404453167610893446264346659366425423726373752739253688735813584578454485
98788661592219844389883571897381307066576677174562482507167981952831117293807555738368738761665203158263637782497957860962333016533565978862040702015735635457351749104167764861
54094772318347454728873230912700892953089306867370272275098056021642290840254959358989117588963622519940254209111473590671495394853785757449840173560508376374033724055580545954
64535612638365170333470511915852729398185238876622677074686088396286919437453728812385720595316047462094394542044082282719916843311682458984494421188993354105347929407521172142
55947006981202891033500543295811742977408286392337259509277252554572959727897263407112289083238374393085604950228420071814865857475334790748395170866755004672197917341480134290
53699953806569270375087000094836871664439340044987396078272057248781274775092754419250662967621990250832615984963301291188436821597237820144994560750155342908323004649835311956
76409370951684354859696574714889018825973693417601005051794400311614370047448226407248116574236067276897019864221611881949633323480992988775901367775707976897803132229325808314
95316557850393496348687412913183754738348536947618145035209545146313746815666050568094178420268975717403036925807495479628236512342612391947340333516547817499999166658334166700
000499999999899999

Explanation

The Lisp interpreter took a little less than 30 seconds to compute this for me! In the spirit of being minimal in my use of words, I will let the code and the output speak for themselves... I rest my case!

See also

References

Lisp article on Wikipedia

Further reading

  1. Ansi Common Lisp by Paul Graham
  2. Land of Lisp by Conrad Barski
  3. Lisp (3rd Edition) by Patrick Henry Winston
  4. Practical Common Lisp by Peter Seibel
  5. On Lisp by Paul Graham
  6. The Little Schemer - 4th Edition by Matthias Felleisen
  7. Structure and Interpretation of Computer Programs, Second Edition by Harold Abelson

External sources

  • McCarthy, John (1979-02-12). "The implementation of Lisp". History of Lisp. Stanford University. Retrieved 2008-10-17.
  • Steele, Jr., Guy L.; Richard P. Gabriel (1993). "The evolution of Lisp". The second ACM SIGPLAN conference on History of programming languages. New York, NY: ACM, ISBN 0-89791-570-4. pp. 231–270. ISBN 0-89791-570-4. Retrieved 2008-10-17.
  • Veitch, Jim (1998). "A history and description of CLOS". In Salus, Peter H. Handbook of programming languages. Volume IV, Functional and logic programming languages (first ed.). Indianapolis, IN: Macmillan Technical Publishing. pp. 107–158. ISBN 1-57870-011-6
  • Abelson, Harold; Sussman, Gerald Jay; Sussman, Julie (1996). Structure and Interpretation of Computer Programs (2nd ed.). MIT Press. ISBN 0-262-01153-0.
  • My Lisp Experiences and the Development of GNU Emacs, transcript of Richard Stallman's speech, 28 October 2002, at the International Lisp Conference
  • Graham, Paul (2004). Hackers & Painters. Big Ideas from the Computer Age. O'Reilly. ISBN 0-596-00662-4.
  • Berkeley, Edmund C.; Bobrow, Daniel G., eds. (March 1964). The Programming Language LISP: Its Operation and Applications. Cambridge, Massachusetts: MIT Press.
  • Weissman, Clark (1967). LISP 1.5 Primer. Belmont, California: Dickenson Publishing Company Inc..

Author link

Najeeb (talk)

Discussions

Please do leave your comment. Thank you!

and

Nobody voted on this yet
 You need to enable JavaScript to vote
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox
Facebook Page
Twitter