Haskell

do 記法で整数リスト生成

More than 5 years have passed since last update.


num.hs

data MyList a r = ML {list::[a], result ::r}

instance Integral a => Monad (MyList a) where
return r = ML [] r
ML l r >>= f = let res = f r in ML (l ++ list res) (result res)

instance (Integral a, Integral b) => Num (MyList a b) where
fromInteger a = ML ([fromIntegral a]) (fromIntegral a)

main = print . list $ do
1
2
3
4
5


できるかな、と思って試行錯誤でやってみた。型クラスがまだよくわかってない。