Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Char
- -- stdi (string to decimal integer) takes a string of digits and turns
- -- it into a decimal integer, sort of like atoi in C.
- stdi :: [Char] -> Int
- -- The function turns a string into a list (stl), goes through the
- -- list multiplying its items by ten to a power of a decrementing
- -- value and accumulating the results (lti); it calculates the maximum
- -- power value (mpv) by calculating the length of the list and
- -- subtracting 1.
- stdi str = lti mpv stl
- -- mpv - maximum power value: e.g. numbers from 1000 to 9999 consist
- -- of 4 digits therefore the maximum power is 4 - 1 = 3
- where mpv = length stl - 1
- -- stl - string to list: applies the function digitToInt to every
- -- character in string and returns a list of integers.
- stl = map digitToInt str
- -- lti goes through the list of integers multiplying them by
- -- ten to a power of a decrementing value and accumulating the results
- lti _ [] = 0
- lti n (x:xs) = x * 10 ^ n + lti (n - 1) xs
- main :: IO ()
- main = print $ stdi "1000000" - 1
- -- 999999
Add Comment
Please, Sign In to add comment