kyoo0000

formatInput.hs

Nov 22nd, 2020 (edited)
454
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Read a number from stdin in the form "xxx.xx"
  2. -- and return a tuple (xxx,xx)
  3. -- Example:
  4. -- input>: "123.45"
  5. -- output>: (123,45)
  6. import Control.Monad
  7. import Text.Printf
  8. import Data.List
  9.  
  10. formatInput :: String -> (Int,Int)
  11. formatInput str = do
  12.    let (s1,s2) = splitInput str
  13.    let x1 = fmtFst s1
  14.    let x2 = fmtSnd s2
  15.    return (x1,x2)
  16.    where splitInput s = splitAt (('.' `elemIndices` s) !! 0) s
  17.          fmtFst s = read s :: Int
  18.          fmtSnd s = read (delete '.' s) :: Int
  19.  
  20. Error:
  21. uri1021.hs:29:4: error:
  22.     • Couldn't match type ‘(Int, Int)’ with ‘Int’
  23.      Expected type: (Int, Int)
  24.        Actual type: (Int, (Int, Int))
  25.    • In a stmt of a 'do' block: return (x1, x2)
  26.      In the expression:
  27.        do let (s1, s2) = splitInput str
  28.           let x1 = fmtFst s1
  29.           let x2 = fmtSnd s2
  30.           return (x1, x2)
  31.      In an equation for ‘formatInput’:
  32.          formatInput str
  33.            = do let (s1, s2) = ...
  34.                 let x1 = ...
  35.                 let x2 = ...
  36.                 ....
  37.            where
  38.                splitInput s = splitAt (('.' `elemIndices` s) !! 0) s
  39.                fmtFst s = read s :: Int
  40.                fmtSnd s = read (delete '.' s) :: Int
  41.   |
  42. 29 |    return (x1,x2)
  43.   |    ^^^^^^^^^^^^^^
  44. Failed, no modules loaded.
  45.  
  46.  
RAW Paste Data