lispとrubyとpythonと(common lisp)
まずはlisp。
基本的にはこんな感じ。
;;コメントは;で始める ;;標準出力に表示 (print "こんにちは 1") ;;;改行は~% (format t "こんにちは 4~%") ;;;タブ文字はこんな感じで表示できる (format t "こんにちは~Cこんにちは~%" #\tab) ;;;フォーマット (format t "こんにちは~A~%" "世界") ;;;文字列に出力 (print (with-output-to-string (strm) (format strm "こんにちは ~A~%" "世界"))) ;;;nilにするとフォーマットした文字列が返る (format nil "こんにちは~a~%" "世界") ;;関数定義 (defun fun0(msg) (format t "「~A」を引数としてfunが呼ばれた~%" msg)) ;;条件分岐 (defun fun1(n) (if (<> n 5) (format t "5より大きいです")))) ;;繰り返し (defun fun3(n) (do ((i 0 (1+ i))) ((> i n) nil) (format t "繰り返し~A回目~%" i))) (defun fun4(n) (dotimes (i (1+ n) nil) (format t "繰り返し~A回目~%" i))) ;;;loopはもう言語内言語と思った方がよい ;;;使いこなせない。。。 (defun fun5(n) (loop for i from 0 to n collect (format t "繰り返し~A回目~%" i))) ;;;こんなのもある(series) (require :series) (series::install) (defun fun5_1(n) (collect (#M(lambda (i) (format t "繰り返し~A回目~%" i) i) (series:scan-range :upto (1- n))))) (fun5_1 3) ;;定数定義 ;;(defconstant const-value "定数") ;;変数宣言(スペシャル変数) (defvar sp-value "スペシャル変数" "変数の説明・・・") ;;レキシカル変数 (let ((s "レキシカル変数")) (format t "変数の値:~A~%" s)) ;;クラス定義 (defclass cls0 nil ((slot0) (slot1))) ;;;コンストラクタ (defmethod initialize-instance :after ((o cls0) &rest initargs) (with-slots (slot0 slot1) o (setf slot0 "初期値") (setf slot1 "初期値"))) ;;;メソッド (defmethod set-slots ((o cls0) val0 val1) (with-slots (slot0 slot1) o (setf slot0 val0) (setf slot1 val1))) (defmethod print-slots ((o cls0)) (with-slots (slot0 slot1) o (format t "slot0:~A~%" slot0) (format t "slot1:~A~%" slot1))) ;;;継承 (defclass cls1 (cls0) ((slot2 :accessor cls1-slot2 :initform "初期値" :initarg slot2))) ;;;オーバーライド (defmethod print-slots ((o cls1)) (with-slots (slot0 slot1 slot2) o (format t "cls1-slot2:~A~%" slot2))) (setf o (make-instance 'cls0)) (print-slots o) (set-slots o "スロット1の値" "スロット2の値") (print-slots o) (setf o (make-instance 'cls1)) (print-slots o) (set-slots o "スロット1の値" "スロット2の値") (print-slots o) ;;ラムダ式 (setf f #'(lambda (s) (format t "~A~%" s))) ;;;呼び出し (funcall f "こんにちは") ;;;これでもよびだせる (apply f '("こんにちは")) ;;;リテラルだからこれでもオケ(意味ないけど) ((lambda (s) (format t "~A~%" s)) "こんにちは") ;;クロージャ (defun make-decorator(prefix suffix) #'(lambda (s) (concatenate 'string prefix s suffix))) (setf decorator (make-decorator "<" ">")) (format t (funcall decorator "html"))