RWH 4.6 練習問題

2の一番下の例はパッと思いつかなかった。

-- 2
asInt_fold :: String -> Int
asInt_fold xs
    | null xs        = error "empty list"
    | xs == "-"      = error "minus sign only"
    | '.' `elem` xs  = error "floating point"
    | head xs == '-' = negate $ fold $ tail xs
    | otherwise      = fold xs
    where fold xs = foldl' step 0 xs
          step acc x = 10 * acc + digitToInt x