Common Lisp,縮寫為CL(不是組合邏輯的縮寫)是Lisp編程語言的一種方言,由ANSI INCITS 226-1994(R2004)(前身為ANSI X3.226-1994(R1999)),所定義的語言規範標準。Common Lisp HyperSpec是源自於ANSI Common Lisp標準的網頁超連結版本。
CL藉由標準功能進行擴展,例如Lisp宏(编译时期程序自身完成的代码重排(compile-time code rearrangement accomplished by the program itself))和阅读器宏(赋予用户自定义的語法以扩展具特殊意义的符号(extension of syntax to give special meaning to characters reserved for users for this purpose))。
在1981年,ARPA管理者Bob Engelmore最初发起了关于Common Lisp的工作,开发一个单一的社群标准Lisp方言[1]。大多数最初的语言设计是通过电子邮件完成的[2][3]。在1982年,Guy L. Steele Jr.于1982年度LISP和函数式编程研讨会上首次给出了Common Lisp的概述[4]。
在1984年,首个语言文档被出版为《Common Lisp语言(英语:Common Lisp the Language)》,第一版(也叫做CLtL1)。在1990年,第二版(也叫做CLtL2)出版了,它结合了在ANSI Common Lisp标准化过程中对语言做的很多变更:扩展的LOOP语法、Common Lisp对象系统、用于错误处理的条件系统、到精美打印的接口等等。但是CLtL2不描述最终的ANSI Common Lisp标准,因而不是ANSI Common Lisp的文档。在1994年,最终的ANSI Common Lisp标准出版了。自从出版之后标准就没有更新。对Common Lisp的各种扩展和改进(例如Unicode、并发、基于CLOS的IO)都由实现和函数库来提供。
语法
Common Lisp是Lisp編程語族的一種方言; 它使用S-表达式來表示源碼和資料結構。函数调用、宏形式和基本形式都以列表來編寫,列表的第一项是函数名稱,如以下範例:
;; 'let'構造為區域變量創建一個作用域。這裡變量'a' 被綁定到 6,變量'b'被綁定到 4。;; 'let'的內部是一個函式體,對它求值後會返回最後一個計算值。這個'let'表達式將;; a 和 b 相加的結果返回。變量 a 和 b 只存在於詞法作用域中,除非它們已先被標記;; 成特殊變量(例如上述的 DEFVAR)。(let((a6)(b4))(+ab)); 返回數值 10
Common Lisp支援頭等函數(亦即函數可當成数据類型來處理)。例如編寫以其它函數當作一個函數的參數,或函數的傳回值也是函數,利用函數的結合來描述常用的操作。CL函式庫高度依賴於這樣的高階函數變換。舉例而言,sort函數可將關係運算子作為參數,並選用如何取鍵的函數作為參數。如此一來不但能對任何型別的数据排序,還能根據取用的鍵值對数据結構作排序。
Common Lisp支援多值的概念,任何表達式經過評估之後必定會有一個主要值,但它也可能擁有任何數量的次要值,讓感興趣的呼叫者接收和檢查。這個概念與回傳列表值不同,因為次要值是備選用的,並通過專用的側面通道來傳遞。也就是說如果不需要次要值,則呼叫者完全不需要知道它們的存在,這是偶爾需使用額外而非必要的資訊,一個方便的機制。
(let((x1266778)(y458))(multiple-value-bind(quotientremainder)(truncatexy)(formatnil"~A divided by ~A is ~A remainder ~A"xyquotientremainder)));;;; => "1266778 divided by 458 is 2765 remainder 408"
(defunget-answer(library)(gethash'answerlibrary42))(defunthe-answer-1(library)(formatnil"The answer is ~A"(get-answerlibrary)));;;; Returns "The answer is 42" if ANSWER not present in LIBRARY(defunthe-answer-2(library)(multiple-value-bind(answersure-p)(get-answerlibrary)(if(notsure-p)"I don't know"(formatnil"The answer is ~A"answer))));;;; Returns "I don't know" if ANSWER not present in LIBRARY
(defunmagic-eight-ball()"Return an outlook prediction, with the probability as a secondary value"(values"Outlook good"(random1.0)));;;; => "Outlook good";;;; => 0.3187
依參照如何出現在表達式中,例如(go x)表示將控制跳轉到x標籤的位置,而(print x)表示x變量。這兩個x的作用域可以在程序的相同區域處於活動狀態,因為tagbody標籤的x與x變量名稱位於分開的命名空間中。基本運算子或宏形式可完全控制其語法中所有符號的含義。例如在(defclass x (a b) ())表達式中,類別定義(a b)是基本類別的列表,因此會在類別的命名空間中搜尋這些名稱;x並非參照到現有的綁定,而是源自於a和b的新類別名稱。這些事實純粹由defclass的語義表示得出。這表達式的唯一事實是defclass引用一個宏綁定;其中的一切都由defclass決定作用域。
Common Lisp支援動態作用域的變量,也稱為特殊變量。有些其它類型的綁定也必須是動態作用域的,例如重新啟動和捕獲標籤。函數綁定不能以flet(僅提供詞法範圍的函數綁定)進行動態作用域,但可以將函數物件(Common Lisp中的第一類物件)分配給動態作用域的變量,在動態作用域內使用let綁定,然後再以funcall或APPLY調用。
(defvar*stashed*);; will hold a function(tagbody(setf*stashed*(lambda()(gosome-label)))(goend-label);; skip the (print "Hello")some-label(print"Hello")end-label)->NIL
這種狀況是錯誤的。一個實作的錯誤回應該包含錯誤條件訊息,例如“GO: tagbody for tag SOME-LABEL has already been left”。該函數嘗試評估(go some-label),它是詞法地嵌入到TAGBODY中並解析為標籤。然而TAGBODY被跳過了而沒有執行(其作用域已經結束),故無法再轉移控制。
Common Lisp對於變量的預設模式是詞法綁定。對於個別符號可用區域聲明,或全局的聲明,來切換成動態作用域。而後者可能隱含地透過如DEFVAR或DEFPARAMETER,這樣的構造使符號成為全局可見的。Common Lisp編程中慣例以開頭和結尾星號*,將特殊變量(即處於動態作用域的)包括起來,這稱為“耳罩慣例”。遵循此慣例的效果,即為特殊變量創建了一個單獨的命名空間,則應該處於詞法作用域的變量不會被意外地特殊化。
Common Lisp中的巨集是独一无二的,和C语言中的巨集的机制相同,但是在巨集扩展的过程中由于可以使用所有现有的Common Lisp功能,因此巨集的功能就不再仅限于C语言中简单的文本替换,而是更高级的代码生成功能。巨集的使用形式和函数一致,但是巨集的参数在传递时不进行求值,而是以字面形式传递给巨集的参数。巨集的参数一旦传递完毕,就进行展开。展开巨集的过程将一直进行到这段代码中的所有巨集都展开完毕为止。巨集完全展开完毕后,就和当初直接手写在此处的代码没有区别,也就是嵌入了这段代码上下文中,然后Lisp系统就对完整的代码上下文进行求值。
;; expansion of UNTIL makes liberal use of DO(defmacrountil(expression&bodybody)`(do()(,expression),@body));; macrolet establishes lexical operator binding for DO(macrolet((do(...)...somethingelse...))(until(=(random10)0)(write-line"Hello")))
Command: (test ">zippy>lispm-int.lisp")
Error: The file was not found.
For lispm:>zippy>lispm-int.lisp.newest
LMFS:OPEN-LOCAL-LMFS-1
Arg 0: #P"lispm:>zippy>lispm-int.lisp.newest"
s-A, <Resume>: Retry OPEN of lispm:>zippy>lispm-int.lisp.newest
s-B: Retry OPEN using a different pathname
s-C, <Abort>: Return to Lisp Top Level in a TELNET server
s-D: Restart process TELNET terminal
-> Retry OPEN using a different pathname
Use what pathname instead [default lispm:>zippy>lispm-int.lisp.newest]:
lispm:>zippy>lispm-init.lisp.newest
...the program continues
Common Lisp 物件系統(CLOS)
Common Lisp包含了物件導向編程的工具包,Common Lisp物件系統或簡稱為CLOS,它是最強大的物件系統之一。Peter Norvig 解釋了在具備CLOS的動態語言中,如何使用其功能(多重繼承,混合,多方法,元類,方法組合等),以達成設計模式更簡單的實現。曾經有幾個擴展被提出來作為Common Lisp ANSI標準的物件導向編程應用,而最終採用了CLOS作為Common Lisp的標準物件系統。
CLOS是個具有多分派和多重繼承的動態物件系統,並且與靜態語言(如C++ 或Java)中的OOP設施截然不同。作為動態物件系統,CLOS允許在執行時期對泛化函數和類別進行更改。方法可以添加和刪除,類別可以添加和重新定義,物件可依照類別的變動更新,而物件所屬的類別也可以更改。CLOS已經整合到ANSI Common Lisp中。通过函數可以像普通函數一樣使用,並且是第一類資料類型。每個CLOS類別都已被整合到Common Lisp類別系統中。
Common Lisp中許多型別都有一個相對應的類別。規範中沒有說明CLOS實作的條件,CLOS進階用法的可能性並不是Common Lisp的ANSI標準,CLOS的用處有更多的潛能。一般Common Lisp實作將CLOS用於路徑名稱、流、輸入/輸出、條件,CLOS本身等等。
;; By convention, constants in Common Lisp are enclosed with + characters.(defconstant+year-size+365)(defunbirthday-paradox(probabilitynumber-of-people)(let((new-probability(*(/(-+year-size+number-of-people)+year-size+)probability)))(if(<new-probability0.5)(1+number-of-people)(birthday-paradoxnew-probability(1+number-of-people)))))
(defclassperson()((name:initarg:name:accessorperson-name)(age:initarg:age:accessorperson-age))(:documentation"The class PERSON with slots NAME and AGE."))(defmethoddisplay((objectperson)stream)"Displaying a PERSON object to an output stream."(with-slots(nameage)object(formatstream"~a (~a)"nameage)))(defparameter*group*(list(make-instance'person:name"Bob":age33)(make-instance'person:name"Chris":age16)(make-instance'person:name"Ash":age23))"A list of PERSON objects.")(dolist(person(sort(copy-list*group*)#'>:key#'person-age))(displayperson*standard-output*)(terpri))
(defunlist-matching-lines(filepredicate)"Returns a list of lines in file, for which the predicate applied to the line returns T."(with-open-file(streamfile)(loopforline=(read-linestreamnilnil)whilelinewhen(funcallpredicateline)collectit)))
Common Lisp有時被稱為Lisp-2,而Scheme被稱為Lisp-1。它指的是CL對函數和變量使用個別的命名空間(實際上CL有許多命名空間,例如go標籤,block名稱和loop關鍵字)。在涉及多個命名空間的權衡之間,CL與Scheme倡導者之間存在著長期的爭議。在Scheme中(廣義地)必須避免與函數名稱互相衝突的變量名稱;Scheme函數通常擁有名稱為lis,lst或lyst的參數,以免與系統內建的list函數衝突。然而在CL中,在傳遞函數作為參數時一定要顯式地引用函數的名稱空間,這也是一個常見的事件,如前面小節中的排序編程範例。
Common Lisp是由一份技术规范定义而不是被某一种具体实现定义(前者的例子有Ada语言和C语言,后者有Perl语言)。存在很多种实现,语言标准详细阐明了可能导致合理歧义的内容。
另外,各种实现试图引入套件或函式库来提供标准没有提及的功能,可能的擴充功能如下所列:
互動式頂層(REPL)
垃圾收集
除錯器,步進器和檢查器
弱資料結構(雜湊表)
可擴展的序列
可擴展的LOOP
環境存取
CLOS元物件協定(meta-object protocol)
基於CLOS的可擴展流
基於CLOS的條件系統
網絡流
固定性CLOS(persistent)
Unicode支援
外語編程介面(經常到C)
作業系統介面
Java介面
多緒和多重處理
應用交付(應用程序,動態函式庫)
儲存映像檔
可移植的自由软件库提供了各种特性,著名的有Common-Lisp.net[6]和Common Lisp Open Code Collection[7]项目。
Common Lisp设计为由增量编译器实现。优化编译的标准声明(例如内联函数)已进入语言规范的计划。大多数Lisp实现将函数编译成原生的机器语言。其他的编译器编译为中间码,有损速度但是容易实现二进制代码的可移植。由于Lisp提供了交互式的提示符以及函数增量式的依次编译,很多人误会为Lisp是纯解释语言。
Mirai[22],Izware LLC[23]'s fully integrated 2d/3d computer graphics content creation suite that features what is almost universally regarded as the best polygonal modeler in the industry, an advanced IK/FK and non-linear animation system (later popularized by such products as Sega's Animanium and Softimage XSI, respectively), and advanced 2d and 3d painting. It is used in major motion pictures(most famously in New Line Cinema's Lord of the Rings), video games and military simulations.
American TV series or program The Cheetah GirlsPromotional posterBased onThe Cheetah Girlsby Deborah GregoryWritten byAlison TaylorDirected byOz ScottStarringRavenAdrienne BailonKiely WilliamsSabrina BryanMusic byJohn Van TongerenCountry of originUnited StatesOriginal languageEnglishProductionProducersWhitney HoustonDebra Martin ChaseJacqueline GeorgeCinematographyDerick V. UnderschultzEditorsTerry StokesDebra LightRunning time93 minutesProduction companiesBrownhouse ProductionsMartin Ch...
Canal JDiluncurkan23 Desember 1985PemilikM6 GroupNegara PrancisSitus webwww.canalj.net Canal J merupakan sebuah jaringan televisi Prancis yang ditujukan kepada program anak-anak. Tersedia melalui layanan televisi terrestrial digital 'TNT' dan ditujukan pada anak berusia antara 4 dan 16 tahun. Program Code Lyoko Dragon Booster Kid Paddle My Life Me Totally Spies Marsupilami Martin Mystery (animated series) Oggy And The Cockroaches Sabrina, the Animated Series Skyland Trollz Viva Pinata Yu...
Botanical garden in Italy Botanical Garden of Villa Beuca in CogoletoThe Orto Botanico di Villa Beuca (34,000 m²) is a botanical garden situated in Beuca, on a terrace nearly 100 meters above the sea located around the Villa Beuca not far from the slopes of the Regional Natural Park of Beigua, in Cogoleto, Province of Genoa, Liguria, Italy. The garden is currently open to the public by appointment on Saturday: for groups and schools, guided tours may be organized; an admission fee is charged...
Montaña Blanca Montaña Blanca in der Bildmitte, links der Pico del Teide Höhe 2748 msnm Lage Teide-Nationalpark, Teneriffa Dominanz 2,79 km → Pico del Teide Schartenhöhe 28 m Koordinaten 28° 16′ 6″ N, 16° 36′ 46″ W28.2682-16.6126722222222748Koordinaten: 28° 16′ 6″ N, 16° 36′ 46″ W Montaña Blanca (Kanarische Inseln) Typ Flankenvulkan, Lavadom Gestein Phonolithischer Bimsstein Alter des Ge...
Chimpanzee falsely promoted as a chimpanzee-human hybrid OliverEarly media photograph of OliverSpeciesChimpanzeeSexMaleBornc. 1957Democratic Republic of the CongoDiedJune 2, 2012 (aged 55)San Antonio, Texas, U.S.Known forRumors of being a missing link or humanzeeOwnerVariousMate(s)Raisin Oliver (c. 1957 – 2 June 2012)[1] was a former performing chimpanzee once promoted as a missing link or humanzee due to his somewhat human-like appearance and a tendency to walk upright. Despit...
Geographical features of England Geography of EnglandSatellite image including EnglandContinentEuropeRegionBritish IslesArea • Total132,930 km2 (51,320 sq mi)Coastline3,200 km (2,000 mi)Borders Scotland96 km (60 mi) Wales 257 km (160 mi) Highest pointScafell Pike978 m (3,209 ft)Lowest pointHolme Fen, −2.75 m (−9 ft)Longest riverRiver Severn (shared with Wales)354 km (220 mi) Longest river entirely withi...
1978 single by BostonDon't Look BackSingle by Bostonfrom the album Don't Look Back B-sideThe JourneyReleasedAugust 2, 1978Recorded1978StudioTom Scholz's Hideaway StudioNorthern Studio (Maynard, Massachusetts)GenreHard rockLength5:58 (Album Version);4:05 (Radio Edit)LabelEpicSongwriter(s)Tom ScholzProducer(s)Tom ScholzBoston singles chronology Peace of Mind (1977) Don't Look Back (1978) A Man I'll Never Be (1978) Music videoDon't Look Back (Radio edit) on YouTube Don't Look Back is a song by A...
American animated TV series This article relies excessively on references to primary sources. Please improve this article by adding secondary or tertiary sources. Find sources: Wallykazam! – news · newspapers · books · scholar · JSTOR (May 2023) (Learn how and when to remove this template message) Wallykazam!GenreFantasyMusicalEducationalComedyCreated byAdam PeltzmanVoices ofThomas LangstonDan BittnerAria CapriaTaliyah WhitakerJorge VegaJenna IaconoJ.R...
Combatant organizations opposed to Nazi Germany This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Bulgarian resistance movement during World War II – news · newspapers · books · scholar · JSTOR (December 2012) (Learn how and when to remove this template message) Part of a series onAnti-fascism InterwarEthiopia...
Consumer electronics brand PlextorIndustryComputer hardwareFounded1985; 38 years ago (1985) Chiyoda, Tokyo, JapanHeadquartersTaipei, TaiwanProductsSSDs, optical disc drives, networking toolsWebsitewww.goplextor.com Plextor (styled PLEXTOR) (Chinese: 浦科特; Japanese: プレクスター) is a Taiwanese (formerly Japanese) consumer electronics brand, best known for solid-state drives and optical disc drives. Company The brand name Plextor was used for all products manuf...
Die Villa Sonnenhof ist eine stattliche Villa in der Eduard-Bilz-Straße 46 (ehem. Sophienstraße 18) im Stadtteil Oberlößnitz der sächsischen Stadt Radebeul, unmittelbar am Eduard-Bilz-Platz bzw. dem Augustusweg gelegen. Villa Sonnenhof Park der Villa Sonnenhof Villa Sonnenhof (re.), Villa Falkenstein (li.), Lutherkirche (mi.), vom Eduard-Bilz-Platz aus Inhaltsverzeichnis 1 Beschreibung 2 Geschichte 3 Literatur 4 Weblinks 5 Einzelnachweise Beschreibung Die zweigeschossige, mitsamt der Ein...
This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: List of coffee varieties – news · newspapers · books · scholar · JSTOR (March 2008) (Learn how and when to remove this template message) Main article: Coffee List and origin of arabica varieties TIF Coffee varieties are the diverse subspecies derived through se...
Place in Centre-Est Region, Burkina FasoZigla-KoulpeleCountry Burkina FasoRegionCentre-Est RegionProvinceBoulgou ProvinceDepartmentGarango DepartmentPopulation (2005 est.) • Total4,476 Zigla-Koulpele is a town in the Garango Department of Boulgou Province in south-eastern Burkina Faso. As of 2005, the town has a population of 4,476.[1] References ^ Burkinabé government inforoute communale vte Boulgou ProvinceCapital: TenkodogoBagré Department Bagré Boakla Dirl...
سفارة بنما في اليابان بنما اليابان الإحداثيات 35°39′23″N 139°44′30″E / 35.656257°N 139.741716°E / 35.656257; 139.741716 البلد اليابان المكان Higashi-Azabu [الإنجليزية] تعديل مصدري - تعديل سفارة بنما في اليابان هي أرفع تمثيل دبلوماسي[1] لدولة بنما لدى اليابان.[2][3] �...
This biography of a living person needs additional citations for verification. Please help by adding reliable sources. Contentious material about living persons that is unsourced or poorly sourced must be removed immediately from the article and its talk page, especially if potentially libelous.Find sources: John McDermott singer – news · newspapers · books · scholar · JSTOR (November 2021) (Learn how and when to remove this template message) John...
Canadian basketball player Mike SmrekMike Smrek in 2007 at a high school graduation ceremonyPersonal informationBorn (1962-08-31) 31 August 1962 (age 61)Welland, Ontario, CanadaListed height7 ft 0 in (2.13 m)Listed weight250 lb (113 kg)Career informationHigh schoolEastdale (Welland, Ontario)CollegeCanisius (1981–1985)NBA draft1985: 2nd round, 25th overall pickSelected by the Portland Trail BlazersPlaying career1985–1997PositionCenterNumber52, 55, 11Career his...