Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Delicious Lisp

Delicious Lisp

おいしいLisp
Lisp/S式とClojureの基本を簡単に紹介。
Clojureおーいしい!

Kent OHASHI

August 06, 2016
Tweet

More Decks by Kent OHASHI

Other Decks in Programming

Transcript

  1. 自己紹介 ( d e f p r o f i

    l e l a g e n o r h y n q u e [ K e n t O H A S H I ] : g i t h u b / t w i t t e r @ l a g e n o r h y n q u e : c o m p a n y 株式会社オプト : l a n g u a g e s [ C l o j u r e H a s k e l l P y t h o n S c a l a E n g l i s h f r a n ç a i s D e u t s c h р у с с к и й ] : i n t e r e s t s [ プログラミング 語学 数学] )
  2. 関数呼び出し: Python > > > s t a n d

    a r d _ d e v i a t i o n ( [ 1 , 2 , 3 , 4 , 5 ] ) 1 . 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1
  3. 関数定義: Python f r o m m a t h

    i m p o r t s q r t d e f s t a n d a r d _ d e v i a t i o n ( x s ) : n = l e n ( x s ) μ = s u m ( x s ) / n r e t u r n s q r t ( s u m ( ( x - μ ) * * 2 f o r x i n x s ) / n )
  4. 関数呼び出し: Lisp (Clojure) ( s t a n d a

    r d - d e v i a t i o n [ 1 2 3 4 5 ] ) u s e r = > 1 . 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1
  5. 関数定義: Lisp (Clojure) ( d e f n s t

    a n d a r d - d e v i a t i o n [ x s ] ( l e t [ n ( c o u n t x s ) μ ( / ( a p p l y + x s ) n ) ] ( M a t h / s q r t ( / ( a p p l y + ( f o r [ x x s ] ( M a t h / p o w ( - x μ ) 2 ) ) ) n ) ) ) )
  6. リファクタリング: Lisp (Clojure) なんとかしたい…… a p p l y +

    . . . 関数を定義しよう! ( d e f n s u m [ n s ] ( a p p l y + n s ) )
  7. 関数の利用: Lisp (Clojure) apply + ⇒ sum ( d e

    f n s t a n d a r d - d e v i a t i o n [ x s ] ( l e t [ n ( c o u n t x s ) μ ( / ( s u m x s ) n ) ] ( M a t h / s q r t ( / ( s u m ( f o r [ x x s ] ( M a t h / p o w ( - x μ ) 2 ) ) ) n ) ) ) )
  8. リファクタリング: Lisp (Clojure) なんとかしたい…… ( M a t h /

    s q r t ( / ( s u m ( f o r [ x x s ] ( M a t h / p o w ( - x μ ) 2 ) ) ) n ) ) マクロを定義しよう! ( d e f m a c r o - > [ x & f o r m s ] ( l o o p [ x x , f o r m s f o r m s ] ( i f f o r m s ( l e t [ f o r m ( f i r s t f o r m s ) t h r e a d e d ( i f ( s e q ? f o r m ) ( w i t h - m e t a ` ( ~ ( f i r s t f o r m ) ~ x ~ @ ( n e x t f o r m ) ) ( m e t a f o r m ) ) ( l i s t f o r m x ) ) ] ( r e c u r t h r e a d e d ( n e x t f o r m s ) ) ) x ) ) )
  9. マクロの利用: Lisp (Clojure) (h (g (f x))) ⇒ (-> x

    f g h) ( d e f n s t a n d a r d - d e v i a t i o n [ x s ] ( l e t [ n ( c o u n t x s ) μ ( / ( s u m x s ) n ) ] ( - > ( f o r [ x x s ] ( M a t h / p o w ( - x μ ) 2 ) ) s u m ( / n ) M a t h / s q r t ) ) )
  10. Lisp/S 式 homoiconicity ( 同図像性): code as data マクロ: 自由自在なメタプログラミング

    DSL ( ドメイン特化言語): 対象領域に合わせたミニ言語の構築 Lisp にとってメタプログラミングは黒魔術ではなく言語の基本機能
  11. DSL の例: SQL S E L E C T u

    . i d , u . n a m e F R O M u s e r u W H E R E u . a g e > = 2 0 ; : Tasty SQL for Clojure Korma ( s e l e c t u s e r ( f i e l d s : i d : n a m e ) ( w h e r e ( > = : a g e 2 0 ) ) )
  12. DSL の例: HTML < h e a d > <

    t i t l e > たのしいL i s p < / t i t l e > < / h e a d > < b o d y > < u l > < l i > S c h e m e < / l i > < l i > C o m m o n L i s p < / l i > < l i > C l o j u r e < / l i > < / u l > < / b o d y > : Fast library for rendering HTML in Clojure Hiccup ( h t m l [ : h e a d [ : t i t l e " たのしいL i s p " ] ] [ : b o d y [ : u l ( f o r [ x [ " S c h e m e " " C o m m o n L i s p " " C l o j u r e " ] ] [ : l i x ] ) ] ] )
  13. DSL の例: JavaScript l e t c o l o

    r s = [ " r e d " , " y e l l o w " , " g r e e n " ] ; c o l o r s . f o r E a c h ( c o l o r = > c o n s o l e . l o g ( c o l o r ) ) ; : Clojure to JS compiler ClojureScript ( d e f c o l o r s [ " r e d " " y e l l o w " " g r e e n " ] ) ( d o s e q [ c o l o r c o l o r s ] ( p r i n t l n c o l o r ) )
  14. Further Reading : Clojure 公式 : ClojureScript 公式 : ブラウザで試せるClojure

    REPL のひとつ : ブラウザで試せるClojureScript REPL のひとつ : Clojure の入門書( 日本語では現在唯一) : Clojure のレシピ本 : Lisp( 主にCommon Lisp) でのマクロの活用について : DSL 設計/ 実装のノウハウとRuby, Scala, Groovy, Clojure のメタプログラミングについて Clojure ClojureScript Clojure instaREPL Replumb REPL 『 プログラミングClojure』 『 おいしいClojure 入門』 『On Lisp』 『 実践プログラミングDSL』