ここみた。

NodeBoxも楽しいなぁ
20012010 Haskell
Write Yourself a Scheme in 48 Hoursを3章まで読んだ。RWHのParsecの章を読んでたのと、やさしい Lisp の作り方をMooseでトレースしたことがあるので割とすんなりと進んだ。
Real World Haskell―実戦で学ぶ関数型言語プログラミングapply func args = maybe (Bool False) ($ args) $ lookup func primitives
ってのがあって、僕はちょっと前まで$を()の構文糖衣だと勘違いしてたのでなんか違和感が。
($ [1,2,3]) (map (1+)) :: map (1+) [1,2,3]
は見慣れれば楽なのかもしれないが。
Prelude> (($[1,2,3]).map) (2-)
[1,0,-1]
とかやるとmapがメソッドっぽく見える
19012010 Haskell
import Data.List (sortBy,sort)
import Data.Ord (comparing)
meansort xs = sortBy (comparing (abs . ((average xs)-))) (sort xs)
where
average xs = sum xs `div` (length xs)
19012010 Haskell
前回の行番号付きでファイルの出力に、さらに5行出力するごとにファイル名行番号を反転表示した後、一時停止して、リターンを押したら出力を再開するプログラムをかけという問題が丸投げされていたのでやってみた。
import System
import System.Console.Readline (readline)
showNext xs file = showNext' 1 xs file
showNext' n xs f | next == [] = do mapM_ putStrLn $ zipWith (\n x -> show n ++ ": " ++ x) [n..] this
| otherwise = do mapM_ putStrLn $ zipWith (\n x -> show n ++ ": " ++ x) [n..] this
maybeLine <- readline $ "\ESC[7m" ++ f ++ "(" ++ show (n+4) ++"):\ESC[m"
case maybeLine of
Nothing -> return ()
Just "" -> showNext' (n+5) next f
where
(this,next) = splitAt 5 xs
main = do
file:_ <- getArgs
content <- readFile file
showNext (lines content) file
showNext'が冗長なので、もう少し綺麗に書きたい。Stateモナドとか使うといいのかなぁ。
haskellのK-meansのソース読んでたら
dist a b = sqrt . sum $ zipWith (\x y-> (x-y) ^ 2) a b
ってのがあって、pythonでzipWithってあったかなーと思って検索したら、自分のwikiが引っかかった。★付けたいところ。
これをつかうと
from math import *
zipWith = lambda f, xs, ys : [f(x, y) for x,y in zip(xs, ys)]
def dist(xs, ys): return sqrt(sum(zipWith(lambda x, y: (x-y)**2, xs, ys)))
となって、任意の次元にも対応出来る
>>> dist([1,2,3,4],[2,2,2,2])
2.4494897427831779
17012010 Arduino
etherシールド買ったので早速つぶやいてみた。

義母がチケットを持っているからいこうと誘われ、のこのこついて行った。
11穀米のリゾットというコース(1.7K)。



全体的に量が少ないという周りの意見であったが、ランチだったらこのぐらいの分量でいいかなとも思う。あと、サラダのあとがなかなか出てこなくて多少持て余した。
17012010 Haskell
mixiの課題丸投げから「10000以下の完全数を求める」
[n|n <- [1..10000],sum [x | x <- [1..(n `div` 2)], n `mod` x == 0] == n]
16012010 life
Haskell勉強会の皆様お疲れ様でした。次回もよろしくお願いします。
Twitter飲みの皆様お疲れ様でした。次回もよろしくお願いします。
hydeout productions 2nd Collections16012010 Haskell
$ runhaskell mixi100116.hs mixi100116.hs
1: import System
2:
3: main = do
4: file:_ <- getArgs
5: content <- readFile file
6: mapM_ putStrLn $ withNum 1 $ lines content
7: where
8: withNum n [] = []
9: withNum n (x:xs) = ((show n) ++ ": " ++ x) : (withNum (n+1) xs)
zipWithがあるじゃないか。忘れてた。
1: import System
2:
3: main = do
4: file:_ <- getArgs
5: content <- readFile file
6: mapM_ putStrLn $ zipWith (\n x -> show n ++ ": " ++ x) [1..] (lines content)
数字の連番くっつけたいときはそういうリストを用意してまぜ合わせる。