noritsuguの日記

2009-04-13``Algorithms'' (38) Exercise 2.9

import Data.Char

string2int xs = string2int' (reverse xs)

string2int' (x:xs) = c2int x + 10 * string2int' xs
string2int' []     = 0

c2int c | isDigit   c = digitToInt c
        | otherwise   = 0

で、

*Main> string2int "3454"
3454
*Main> string2int "76"
76

import Data.Char

string2int = foldl (\r x -> c2int x + 10 * r) 0

c2int c | isDigit   c = digitToInt c
        | otherwise   = 0
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090413