drkcore

2010/01/21 21:44:23

Write Yourself a Scheme in 48 HoursComments (4章)

効率のよいエラーの仕組みを導入。

type ThrowsError = Either LispError

みたいなのが慣れない。Either a bがaとbの両方をとるみたいに感じちゃうからか。実際に下のようにしてみると納得出来るんだけど。

ここにあった例を

int_sqrt :: Int -> (Either Int Double)
int_sqrt x | fsdx * fsdx == x  = Left fsdx  
           | otherwise = Right sdx
 where sdx = sqrt $ fromIntegral x
       fsdx = floor sdx

このように変えてみた

type Leftint = Either Int

int_sqrt :: Int -> (Leftint Double)
int_sqrt x | fsdx * fsdx == x  = Left fsdx  
           | otherwise = Right sdx
 where sdx = sqrt $ fromIntegral x
       fsdx = floor sdx

まぁそうだよなと思う。

Comments