ãããã£ã±ãªã¿ã¤ãã«ãããã
Common LispãSchemeã¨ãã£ãLispæã¯ãããã¦æ§æã®æ§é ãç°¡ç´ ã§ããï¼ã¨ãã£ããã¨ã¯ãããã説æã®å¿ è¦ããªãã§ãããããªã¹ãã¨ãã¦å¼ã®ASTã表ç¾ããããã§ãã
(defun foo (bar) (+ foo bar)) ; ããããæ§æããªã¹ã
æ§é ãç°¡ç´ ã¨ãããã¨ã¯ï¼ããã表ç¾ããã®ã«å¿ è¦ãªï¼è¨ãæããã¨äºç´ããªããã°ãªããªãæåããã¾ãç¡ãï¼ã¨ãããã¨ã§ãããããã§ãã
äºå®ï¼Common Lispã§ã·ã³ãã«ã¨ãã¦ä½¿ããªãæåãæ¢ãã¦ã¿ãã¨ï¼ä»è¨èªããã¯å°ãªãã®ã§ããã¡ãã£ã¨æ¢ãã¦ã¿ã¾ãããã
使ããªãããªæå
()
è¨ãããããªï¼Lispã¯ã«ãã³ã§ãªã¹ãã表ç¾ããã®ã§ã«ãã³ã¯ä½¿ããªãã
'
ãªã¹ããã³ã¼ãã§ã¯ãªããã¼ã¿ã¨ãã¦èªã¾ãããã¨ãã«ã¯ã¯ãªã¼ãquoteã¨ãã表è¨ã§ãããæ示ããããã®è¨å·ã«ã¯ã¢ãã¹ãããã£ãå²ãå½ã¦ããã¦ããï¼ã·ã³ãã«åã«ã¯ä½¿ããªããããã¯ãã£ãã¯ãã ãããåãçç±ã§ä½¿ããªãã
空ç½
ãããããã ã
#
ããã·ã¥ãµã¤ã³ã¯Common Lispã®èªåå¨ã«æ示ãåºãããã«ï¼ãã°ãã°ã¯ãªã¼ãã¨é£æ¥ãã¦'#
ã®ããã«ä½¿ãããã'#()
ã¨è¡¨ç¾ããã¨ãã¯ã¿ã§ããã'#2A((1 2 3) (4 5 6))
ã¨æ¸ãã¨2次å
é
åã«ãªãã
ã¡ãªã¿ã«é¢æ°ã·ã³ãã«ã¯#'foo
ã¨ãã£ãããã«æ¸ããã¾ãããããã
:
ã³ãã³ã¯ããã±ã¼ã¸åã¨ã·ã³ãã«ã¨ã®åºåãã¨ãã¦ä½¿ããããä¾ãã°CL-USER:FOO
ã¯CL-USER
ããã±ã¼ã¸ã®FOO
ã¨ããã·ã³ãã«ãæã示ããPerlã®ããã±ã¼ã¸ã¯::ã¨ãããµãã«2æåéããå¿
è¦ãããã®ã ãï¼2æåãè¦ããããã¨ããææ
ãããã
ã¡ãªã¿ã«Common Lispã§ã¯ã³ãã³ãéãã¦::
ã¨æ¸ãã¨ï¼export
æä½ããã¦ããªããã©ã¤ãã¼ããªã·ã³ãã«ã«ã¢ã¯ã»ã¹ã§ãããã«ãã»ã«åãç ´ãï¼æ³¨æãå¿
è¦ãªæä½ã ã¨ãããã¨ãè¦ãç®ã§åããããã«ãªã£ã¦ããã
"
ä»ã®è¨èªãããã§ããããã«ï¼ããã«ã¯ãªã¼ãã¯æååãªãã©ã«ã®ããã«äºç´ããã¦ããã
.
ãããã¯ããã対ãªãã©ã«ã®ããã«äºç´ããã¦ããã
â¦â¦ã¨ããããã
* (defparameter foo.bar 123) FOO.BAR * (+ foo.bar foo.bar) 246
ã¯ãªã¼ãã®å¤ã§ã¯ä½¿ããããã ã
\
ããã¯ã¹ã©ãã·ã¥ã¯æåã表ç¾ããã¨ãã«ä½¿ããã¨ãããã
* (defparameter foo\bar 666) |FOObAR|
ãªããä¸ç©ãªç¶æ ã«ãªã£ããæãããããã¦ãããã
ããããªæåã§å¤æ°å®ç¾©ãã¦ã¿ã
ãã¦ï¼åè¿°ãã以å¤ã®æåã¯ï¼ãã¶ãï¼ã ããããªãã§ãå¤æ°åã¨ãã¦ï¼ããæ£ç¢ºã«ã¯ã·ã³ãã«åã¨ãã¦ä½¿ããã¨ãã§ãã¾ããå®éã«ãã£ã¦ã¿ã¾ãããã
ã¢ã¹ã¿ãªã¹ã¯ã¯SBCLï¼å¦çç³»ï¼ã®ããã³ããã§ãã
@
ã¾ãã¯Perlã©ã¤ã¯ã«ãªã¹ãã@ã§è¡¨ç¾ãã¦ã¿ãã
* (defparameter @array '(1 2 3)) @ARRAY * (mapcar #'* @array @array) (1 4 9)
æ®éã«ä½¿ãããã¨ã¯ãããªã¹ãã¯ãã¾ãã«ãã使ãã®ã§ãããã@ã§ä¿®é£¾ããã¾ã§ããªããããããªãã
ã¡ãªã¿ã«æºã¯ãªã¼ãquasiquoteã§ãªã¹ããå±éããã¨ãã«@ã使ãã®ã§ã¾ããããããããããªãã
* (defparameter foo '(1 2 3)) FOO * `(a b ,@foo) (A B 1 2 3)
%
次ã«ããã·ã¥ã%ã§è¡¨ç¾ãã¦ã¿ãã
* (defparameter %hash (make-hash-table :test #'equal)) %HASH * (setf (gethash "foo" %hash) 123) 123 * (gethash "foo" %hash) 123 T
æ®éã«ä½¿ãããã©ï¼Common Lispã§ããã·ã¥ããå¤ãåãåºãããã«ã¯gethash
é¢æ°ã¨ããå°ç¨ã®é¢æ°ã使ãããã§ãããã®ãã渡ããã¦ããå¤æ°ãããã·ã¥ãªã®ã¯èªæã¨ããã°èªæã
ã¨ããã§gethash
ã®å¼æ°ã®é åºï¼çããç´å¾ã§ãã¦ã¾ããï¼ãã¼ãæåã§ããã·ã¥ãæå¾ã§ããï¼ï¼ï¼ãããããªãã§ããï¼ï¼ï¼ï¼ï¼OOPã§å¹ããã常èãå´©ãã¦ããã¾ãã
(gethash key ; ã¾ããã¼ãæå®ãã¦... hash ; 次ã«ããã·ã¥ãã¼ãã«ã渡ã )
ã¡ãªã¿ã«é¢æ°å ã§ã«ã¼ãç¨ã®é¢æ°ãå®ç¾©ããã¨ãã«ãã¬ãã£ãã¯ã¹ã¨ãã¦%ããã使ããã¦ããæ°ããã¾ãã
ä¸çå·
ã¯ã©ã¹ãå®ç¾©ãã¦ã¿ã¾ããããCommon Lispã§ã¯ã¯ã©ã¹åãä¸çå·ã§ä¿®é£¾ããç¿æ £ããã£ãæ°ããã¾ãã
* (defclass <foo> () ((x :initarg :x :accessor x) (y :initarg :y :accessor y))) #<STANDARD-CLASS COMMON-LISP-USER::<FOO>> * (defparameter fooinstance (make-instance '<foo> :x 12 :y 34)) FOOINSTANCE * (x fooinstance) 12
æ®éã«ä½¿ãã¾ããã
defclass
ãã½ã©ã§æ¸ããããã«ãªã£ãã大人ã§ãã
ããããããdefclassããªãdefclassãã½ã©ã§æ¸ãããããã«ãªããªãããããã人éããéããããªã貧ä¹éããããªããã¡ããã©ãããããã£ã¦ã¨ããªãã ã
— windymelt (@windymelt) 2020å¹´5æ8æ¥
ã¹ã©ãã·ã¥
ã¹ã©ãã·ã¥ã使ã£ã¦ã¿ã¾ããPerlãã¯ããã¨ããè¨èªã§ã¯æ£è¦è¡¨ç¾ãªãã©ã«ã«ä½¿ããã¦ãã¾ãããæ£è¦è¡¨ç¾ã表ç¾ããå¤æ°åã¨ãã¦ä½¿ã£ã¦ã¿ã¾ãã
* (ql:quickload :cl-ppcre) To load "cl-ppcre": Load 1 ASDF system: cl-ppcre ; Loading "cl-ppcre" .. (:CL-PPCRE) * (defparameter /regex/ (ppcre:parse-string "regex")) /REGEX/ * (ppcre:scan /regex/ "abcregexdef") 3 8 #() #()
ããã¾ããã¾ã¿ãæããªãã
[]
é åç¨ã®æ§æããªãã®ã§è§ã«ãã³ãèªç±ã«ä½¿ããããã©ãã¾ã使ãéãæãä»ããªãã1次å é åã§ãããã¯ã¿ã§ä½¿ã£ã¦ã¿ã¾ããããã
* (defparameter [vec] #(1 2 3)) [VEC] * (defun dotproduct (v1 v2) (reduce #'+ (map 'vector #'* v1 v2))) DOTPRODUCT * (defparameter [vec].[vec] (dotproduct [vec] [vec])) [VEC].[VEC] * [vec].[vec] 14
å ç©ãå®ç¾©ã§ããã
ãã¬ã¼ã¹{}ã使ãããã ãã©ç¨éãã¾ã£ããæãä»ããªãã
ã¯ã¦ãªã§ãã£ããã
Common Lispã§ã¯è¿°èªï¼tãnilããè¿ãé¢æ°ï¼ã«ã¯æ«å°¾ã«pï¼predicateã®ç¥ï¼ãã¤ããæ £ä¾ããããï¼Schemeã§ã¯?ãæ«å°¾ã«ä»ãããå人çã«ã¯Schemeã®ã¹ã¿ã¤ã«ã好ããªã®ã ãã
* (defun string? (x) (typecase x ((string) t))) STRING? * (string? 123) NIL * (string? "foo") T
->
Schemeã§ã¯ä½ãããä½ãã¸ã¨å¤æãããããªé¢æ°ã¯->ã§ã¤ãªããCommon Lispã«ããããæ £ä¾ã¯ãªãããã ã
* (defun string->integer (s) (parse-integer s)) STRING->INTEGER * (string->integer "666") 666 3
å½åã«é¢ãã¦ã¯Schemeã®ã»ããããæ´ç·´ããã¦ããããã«æããã
ææ³
å½åè¦åçãªæãã§ã·ã³ãã«ã®ä¸èº«ã«å¯¾å¿ããè¨å·ã修飾ããããã«ããã¨ï¼ãã¨ãã¨Lispã¯ç¡å³ä¹¾ç¥ã¨ãã¦è¦ããã¡ãªã®ã§ï¼ã³ã¼ããèªã¿ããããªããããããªãã
ã¨ã¯ãããã³ã¬ãªã¢ã³è¨æ³ã¿ããã«ãªã£ã¦ãå«ãªæ°ãããã
ã¡ãªã¿ã«Googleã®Common Lispã¹ã¿ã¤ã«ã¬ã¤ãã¨ããã®ããã£ã¦ï¼ä¸è¬çãªæ £ä¾ã¯ç¶²ç¾ ããã¦ããã®ã§ããã«å¾ãã¨ããã ããã
ã¿ãªãã¾ãLispã§è¦æããé¢ç½ãå½åããã£ããæãã¦ãã ããã
ãªã
å®ã¯ãã¤ãï¼ãã¼ãã«ã«ãã¼ï¼ã§ãããã¨ï¼ãã®ä¸ã§ã¯ã ãããä½ã§ã使ããããã«ãªãã¾ãã
* (defparameter |foo bar buzz '() ::::| 1) |foo bar buzz '() ::::|
è
ãã§ãã