Scheme golf tips
あなごるç¨
Haskell golf の教科書ãèªãã§ã俺ãããããã®æ¸ãããï¼ãã¨æã£ã¦æ¸ãã¦ã¿ãï¼(æã£ã¦ããæ¸ãã¾ã§ã«1å¹´ãããçµã£ã¦ããã©)
ã¢ããã¥ã¢ã´ã«ãã¡ã¼ãªã®ã§è³ãã¬æãããããã ãã©ï¼ä»å¾ã®Schemeçã¨ããªããçã®çãä¸ãããé¡ã£ã¦ãã®æç« ãå
¬éããï¼
(教科書ãåèã«ããã¦ããã ãã¾ããï¼)
å±éºãªã®ã§ golf 以å¤ã§ã¯çä¼¼ããªãã§ä¸ããï¼
ããªããã®åºæ¬
- ä½è¨ãªç©ºç½ã¯é¤å»
- æ¹è¡ã¯LF
- ã¨ã©ã¼ã§æ¢ãã¦ãã
- åºåã®æ«å°¾ã«ä½è¨ãªç©ºç½ï¼æ¹è¡ããã£ã¦ãæ§ããªã
Scheme(Gauche) golfã®åºæ¬
- ã¨ã«ããã¾ãã¯é©å½ã«æ¸ãã¦ããã°ã©ã éç®ãã¦ãã
- ä»ã®è¨èªããgcdãå®ãã®ã§ç´ æ°ã»æ´æ°ç³»ã®è§£ãæ¹ãå°ãéããã¨ããã
- use ã¯ããªãæ¹ãçããã¨ãå¤ã
ãã使ãã®ã¯ srfi-1ï¼ç¹ã« iotaï¼
srfi-1 ã§ç°¡åã«æ¸ãã¦ãæ¸ãä¸ããæ¹ãçããã¨ãããã®ã§ï¼ã¨ã«ããããããæ¸ãã¦ã¿ããã¨ãéè¦
Så¼ã®ç縮è¨æ³ãæ´»ç¨ãã
(quote x) 'x
(list x y) `(,x,y)
(dotimes(i 10)(print'hoge) (dotimes,10(print'hoge))
(dotimes(i 10)(print(+ i 1)) (dotimes,10(print(+ . ,1))) (dotimes,10(print(+ .,1))) ; 0.9.2以é
(let((f proc))(f x)(f y)) (let('proc)'x'y)
Gauche0.9.1ã§ã¯unquoteãæç¸ãã¦ãquasiquoteä¸ã®unquoteã¯åãã®ã§å®å¿ãã¦ä½¿ããï¼
(let(,print)`(,(list 1 2 3))) ; => ((1 2 3))
(ä¿®æ£ 12/26 8:29)
å®å¿ãã¦ä½¿ããªããã°ãå¤æï¼
(let (,1 (x 2)) `(,x)) ; => 2
ããããä¸è¨ã®ããã«unquoteã使ãã¦ãã¾ãã®ããã°ãªã®ã§ï¼ææ°ã®éçºçã§ã¯quasiquoteèªä½ãå¥å ¨ãªããã«ä¿®æ£ãããï¼
(let (,1) `(,(+ 1 2))) ; => (,(+ 1 2))
å ±ææ§é (SRFI-38)
(print(read-line)(read-line)) (print #0=(read-line)#0#)
generic function ãç縮åãæ´»ç¨ãã
^ ~ .$
㯠Gauche 0.9.1 ããï¼
^
㯠lambda
ã®ç縮åï¼
(lambda(x y)...) (rec(_ x y)...) (^(x y)...)
å ´åã«ãã£ã¦ã¯ããã«
(^'y ...)
(lambda(x)...) (rec,x ...) (^x ...)
let1 ãããçããªãï¼
(let1 x(foo)(bar x)) ((^x(bar x))(foo))
~
㯠ref*
ã®ç縮åã§ï¼ ref*
㯠ref
ã®ãã¹ããã¾ã¨ãããã®ï¼ ref
㯠list-ref
ã vector-ref
ã®genericçï¼
(list-ref l i) (ref l i) (~ l i)
(ref(ref l i)j) (~ l i j)
.$
㯠compose
ã®ç縮åï¼
(map f(map g l)) (map(.$ f g)l)
ãã®ä»
(string->number s) (x->number s)
æååã¯ã·ã³ãã«ã§ä»£ç¨ã§ãããã¨ããã
(print"hoge") (print'hoge)
'("foo""bar""baz")
ãã·ã³ãã«ã§ãããã°
'(foo bar baz)
string-interpolation
æååã®æ§æã¯ããã format
ã§ãããã¨ãå¤ãï¼
(x->string x) #`",x"
å ±ææ§é ã¯readåä½ã§ããå ±æãããªãããï¼
#`",#0=x,#0#"
ã¨ã¯ã§ããªãï¼
æ£è¦è¡¨ç¾ã¨ object apply
æååå¦çæç¶ã㯠string- ãã¤ãã¦é«ãã®ã§ï¼ãªãã¹ãæ£è¦è¡¨ç¾+object applyã§æ¸ã¾ããï¼
æååã®å解ã¯å¤§ä½ããã§ããï¼
(substring x 1(-(string-size x)1)) ((#/.(.*)./x)1)
(string-copy x 1) ((#/.(.*)/x)1)
set!ã®è¿ãå¤
(ç¾å¨ã®)Gaucheã§ã¯(å¤å)ï¼R5RSå½¢å¼ã® set!
ã¯ä»£å
¥ãããå¤ãè¿ãï¼SRFI-17å½¢å¼ã® set!
㯠setter ãè¿ãå¤ãè¿ãï¼ vector-set!
ã¨ã㯠#
ãè¿ããããªã®ã§é½åãã縮ãã ãããªãï¼æåãã使ããä¸æåã®èå¥åã«ã¯ ^
ã ~
ãããï¼ ^x
ãã¨ã¦ãå®ãã®ã§ä½¿ãæ©ä¼ã¯æ¸ã£ããï¼ç©ºç½ã®é¢ä¿ã§ç¸®ããã¨ãããï¼
anarchy golf - NABEATSU of the world
é 次å®è¡
é 次å®è¡ããæã¯å¤§ä½ print
ã¨ããªã®ã§ ï¼ begin
㯠if
ã and
ã§ä»£ç¨ã§ããï¼
(begin(print x)x) (if(print x)x)
(begin(print x)(set! y z)x) (and(print x)(set! y z)x) ; z ã #f ã§ãªãã¨ã
åå²
ä»è¨èªã¨éã£ã¦æ®éã« if or and ãå®ãï¼cond ã case ããã¾ã«ä½¿ãï¼
æåã¯(Gaucheã§ã¯) eq? ã§æ¯è¼ã§ãããï¼æ°ã§æ±ãã° = ã§æ¯è¼ã§ããï¼
æåååãã¦æ£è¦è¡¨ç¾ã§ããããããã©ãã調ã¹ãã¨ç¸®ãå ´åãããï¼
null?
ãã㯠eq?()
ï¼Gaucheã§ã¯ ()
ã¯quoteããªãã¦ãåãï¼
(null? l) (eq?()l)
å ¥å
read
ã§æ¸ããªã read
ï¼æååä½ã®å¦ç㯠read-char
ã read-byte
ï¼
ãã¾ã« read-block
ã使ããï¼ãã㯠incomplete string ãè¿ãã®ã§ä½¿ãã«ãã(æ£è¦è¡¨ç¾ãref
ã使ããªã)ã®ã§æ°ãã¤ããï¼
åºå
åºæ¬ã¯ print
ï¼ãªã¹ããåºåããå ´åï¼(apply print ...)
ã®å½¢ãæå¹ï¼
æååã®æ§æ㯠#`"..."
ã format
ãå¤ãï¼
å帰/ã«ã¼ã
(for-each(^x ...)l) (map(^x ...)l)
for-each
ã¯å¿è«ä½¿ããªãï¼
(while(read-line)=> x ...) (port-map(^x ...)read-line)
(while(read-line)...)
ã¯ã¨ã©ã¼æ¢ããå¿
è¦ãªã®ã§ï¼ã§ããªãå ´å㯠port-map
ã使ãï¼
æç¸ããå¿
è¦ããªããã°
(while(print(~(read-line)0)))
çï¼
å
¥åã«å¯¾ããã«ã¼ãã¯do dotimes rec while until port-map port-fold
çï¼é¸æè¢ãå¤ãï¼
ç®å®ã¯ (ä¿®æ£ 1/17 17:58)
- ãã ã®å復ã¯
while
ãport-map
- å
¥åã«å¯¾ããå復ã§ç¶æ
å¤æ°ãä¸ã¤æã¤å ´åã¯
rec
- å
¥åã«å¯¾ããå復ã§ç¶æ
å¤æ°ãä¸ã¤æã¡ã¤ã¤ã¨ã©ã¼æ¢ãã§ããªãå ´åã¯
port-fold
- 綺éºã«å½ã¦ã¯ã¾ããã®ããªããã°
do
do
ã¯
(do('car,cdr`print(x 1)(i 0(+ i 1)))(#f)...)
ã¨ããããã§ããã®ã§å¼·ãï¼
æ´æ°ã«å¯¾ããç¹°ãè¿ã㯠dotimes
ã iota
ï¼srfi-42 ã使ãå ´åã¨ï¼ä»ã®ã«ã¼ã㨠inc! dec! ãåããã¦ä½¿ãå ´åãããï¼
å ¥åãä¸åº¦ãªã¹ãã«ãããæã¯
(port-map(^x x)read-line)
ã¨ãªããï¼+
ã *
ãä¸å¼æ°ã®æã«å¼æ°ããã§ãã¯ãããã®ã¾ã¾è¿ãã®ã§
(port-map + read-line)
ã¨æ¸ããï¼