Haskell Golf å ¥é
ãã®çºè¨
@dekosuke ãªãã åã¯æè¿å ¨ç¶ Haskell ã´ã«ãããã¦ããªãã¦è ãè¡°ãã¦ãã¦æ²ããã®ã§ã @notogawa ãããããã解説è¨äºãæ¸ãã¦ãããªããã¨æå¾ ãã¦ãã¾ãï¼ [ç³ææµ æµ·å çè¬æ¼ä¼ 11/6(å) URL ]
2010-10-07 20:18:44 via YoruFukurou to @dekosuke
æ¸ãã¤ã¤æã£ããï¼ãããªãã®ã§ããã®ã ãããï¼ãããã®åã«ä¸æ®µãããããªæ°ãããï¼
ã¨ãããã注ææ¸ããæ¸ãã¦ãããã¼ï¼
ãã®ã¨ã³ããªã¯Haskellå
¥éã§ã¯ããã¾ããï¼æ®éã«ã³ã¼ãã£ã³ã°ããå ´åï¼ããã¾ãçä¼¼ããªãã»ããè¯ãã§ãï¼
(anarchy) golfã®åºæ¬
- ä½è¨ãªç©ºç½ã¯é¤å»(Haskellã¯é¢æ°é©ç¨ãããã®ã§å²ã¨ç©ºç½ãããã»ã)
- æ¹è¡ã¯LF
- ã¨ã©ã¼ã§æ¢ãã¦ãã
Haskell Golf ç¹æã®åºæ¬
- modulesã¨ãæ¸ããªã
- ææ§ã¨ãæãããã¾ã§Type Signatureã¨ãæ¸ããªã
- ã¨ã«ããã¾ãã¯é©å½ã«æ¸ãã¦ããã°ã©ã éç®ãã¦ãã
- åã£ã¦çããä½ããã足ãã¦ä½ãã»ããå®ããã¨ãå¤ã
- ãã¿ã¼ã³ãããããã³ã³ã¹ãã©ã¯ã
- divãã(*)
- take/dropãã(++)
- ä»ã®è¨èªããgcdãå®ãã®ã§ç´ æ°ã»æ´æ°ç³»ã®è§£ãæ¹ãå°ãéããã¨ããã
- scan/foldãå¶ãããã®ã²ã¼ã ãå¶ãâ¦ãã¨ãç¨ã«ãããã
- ã¢ããã¯(>>=)ã§ã¤ãªãããdoè¨æ³ã«ãããã¯ç¶æ³ä¾å
- é常ã¯(>>=)ãå®ã
- è¤è£½ããï¼å å 表è¨ã®ä¸ã§ä½¿ãï¼ãªã©ä¸æ¦ä½ãè¨å·ã«è½ã¨ãå¿ è¦ããããªãdoè¨æ³(ãã¹ä¿®æ£thx @1to100pen)
[1..n]>>=f;f x=[x,x] [1..n]>>= \x->[x,x] do x<-[1..n];[x,x]
- returnãæ¸ããã«æ¸ãããã«ãã¹ã
- if/caseå¼ã¯ä½¿ããªã
- ifã®æ¸ãæ¿ãã¯å¾è¿°
- é¢æ°ã®ãã¿ã¼ã³ãããã§åå²ããã
- åå¾ã«ç½®ã空ç½ã®é¢ä¿ä¸ä½¿ããªãlet-inããwhere
- importã¯é«ãã®ã§ããã ãããªã
- ããã§ãããimportãããã¨ã«ãªãã®ã¯â¦
- List (sortãå¿ è¦ã ã£ããããã¨)
- Numeric (åºæ°å¤æãå¿ è¦ã ã£ããããã¨)
- Text.Printf (æ´å½¢ãå¿ è¦ã ã£ããããã¨)
å ¥åºå
å ¥åå ¨ä½ãå¦çã«ä½¿ãåé¡
main=interact f
ä¸è¡æ¯ã«å ¥åºåãè¡ã£ã¦æ¸ãåé¡ãªã
main=interact$unlines.map f.lines
ã¨ãªããï¼ããããã¨ã©ã¼æ¢ãã§ãããªããã£ã¡ã®ã»ããçãï¼
main=getLine>>=putStrLn.f>>main
ããã«asãã¿ã¼ã³ãå©ç¨ãã¦æ¬¡ã®å½¢ã¾ã§ãããï¼
m@main=getLine>>=putStrLn.f>>m
ãã ãï¼å¦çã®ä¸ã§è¡æ«æ¹è¡ã®ä»ä¸ãå®ãå¯è½ã§ããã°ï¼
main=interact$(>>=f).lines
ã«æå©ãä»ãï¼
ä»ï¼å ¥åºåããã¿ã®ããããå¤æã¯ï¼
getLine>>=f.read readLn>>=
putStrLn.show
print
putStr.unlines
mapM putStrLn
ifã®å¤æ
ã¾ãåºæ¬
if p then a else b last$b:[a|p]
ããããã±ã¼ã¹1
if 0<x then a else b last$b:[a|0<x] [a,b]!!(0^x) -- xãIntegralã§éè² ãªãã° a+(b-a)*0^x -- a,bãNumãªãã°
ããããã±ã¼ã¹2
if p then xs else[] [x|p,x<-xs]
å帰
åºæ¬ã¯é 延è©ä¾¡ã§ç¡éã«åãã¦takeã§æ¬²ããã ãåãï¼ãããã¯ï¼(!!)ã§åãï¼
take 10z;z=0:z
è¦å®æ°å帰ãã¦æ¢ãããªãï¼ã¨ã©ã¼æ¢ãã§ãããªããã¿ã¼ã³ããã失æãããï¼
f(a:x)=print a>>f x -- []ã«ãªã£ããåæ¢ f(n+1)=print n>>f n -- 0ã«ãªã£ããåæ¢
åæ¢æ¡ä»¶ã¾ã§å ¥ãããªããã¿ã¼ã³ãããã®è¨è¿°é ã§çãããªããã¨ãããï¼
f[]=[];f(a:x)=g a:f x f(a:x)=g a:f x;f x=x -- åãå¤ãããªãå ´å
èåå¤æ
map/map fusion
map f.map g map(f.g)
foldr/map fusion
foldr f e.map g foldr(f.g)e
scanrãåæ§
scanr f e.map g scanr(f.g)e
Hylomorphism
foldr f e.b;b x|p x=[]|0<1=x:b(h x) g;g x|p x=e|0<1=f x$g(h x)
ãã®ä»ããããå¤æ
Trueã®ç½®ãæ¿ã
otherwise True 0<1
ãªã¹ãã¢ããã¯æå¹ã«ä½¿ãï¼
concat.map f concatMap f (>>=f)
xãéè² ã®ã¨ã以ä¸ã®å¤å½¢ãã§ãããxã大ããã¨timeoutã®å±éºãããï¼
[1..n]!!mod x n cycle[1..n]!!x
ä½ãã®æ¡ä»¶ã§ã½ã¼ãããæã¯sortByã¯æ¡å¤å¼±ã
sortBy(\a b->f a`compare`f b);f x=expr sortBy((.f).compare.f);f x=expr map snd.sort.map(\x->(expr,x))
æå¾ã®å½¢ã«ãã¦ããã¨ï¼ããã«åå¾ã®å¦çã¨èåã§ãããããã®ã§ãªãããè¾ãï¼
mapM print.sortBy((.f).compare.f);f x=expr mapM(print.snd).sort.map(\x->(expr,x))
filterã¯åä½ã§ã¯ãªã¹ãå å 表è¨ããæå©ã ãï¼
filter p xs [x|x<-xs,p x]
ä»ã®å¦çã¨åæããã¨å¤§æ¦ä¸å©ã«ãªãï¼
map f.filter p xs [f x|x<-xs,p x]
lengthã¯åå¦çå¦ä½ã§ã¯sumã«ç½®ãæããã»ããçããã¨ãããï¼
length xs sum[1|x<-xs] length$filter p xs sum[1|x<-xs,p x]
ãªã¹ãå å 表è¨ã¨whereã使ãå ´åå å 表è¨å ã®ã¿ã§æ¸ã¾ãããã¨ãã§ããï¼
f x=[g a|a<-xs,g a]where g a=expr x a f x=[a|a<-xs,a<-[expr x a],a] --ãªãããã¾ããªããª
replicateã¯ãã¶ããããªãåï¼
replicate n x [1..n]>>[x]
åä½ã®takeWhile/dropWhileã¯spanã§ï¼
takeWhile p fst.span p dropWhile p snd.span p
ä¸å®åæ°ä»¥ä¸ã®æååãªãã©ã«ã®ãªã¹ããå¿ è¦ãªå ´åï¼å¯è½ãªãã°ã²ã¨ã¤ã«é£çµãã¦ããã¦wordsãlinesã§åãï¼
["a","b","c"] words"a b c"