Guest User

Untitled

a guest
Aug 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. Works in ghci but not in the file
  2. printf.hs:37:19:
  3. Ambiguous type variable `a0' in the constraints:
  4. (Format a0) arising from a use of `showManyP' at printf.hs:37:19-27
  5. (Num a0) arising from the literal `10' at printf.hs:37:34-35
  6. Probable fix: add a type signature that fixes these type variable(s)
  7. In the second argument of `($)', namely `showManyP "%d" 10'
  8. In the expression: putStrLn $ showManyP "%d" 10
  9. In an equation for `main': main = putStrLn $ showManyP "%d" 10
  10. Failed, modules loaded: none.
  11.  
  12. {-# LANGUAGE OverlappingInstances #-}
  13. {-# LANGUAGE UndecidableInstances #-}
  14. {-# LANGUAGE FlexibleInstances #-}
  15. {-# LANGUAGE TypeSynonymInstances #-}
  16. import Data.List (intercalate,isPrefixOf)
  17. class Showable a where
  18. showManyP :: String -> a
  19.  
  20. instance Showable String where
  21. showManyP str = str
  22.  
  23. instance (Showable s,Format a) => Showable (a->s) where
  24. showManyP str a = showManyP (format str a)
  25.  
  26. class Format a where
  27. format :: String -> a -> String
  28.  
  29. instance Format String where
  30. format str a = replace "%s" str a
  31.  
  32. instance Format Char where
  33. format str a = replace "%c" str [a]
  34.  
  35. instance Num a=>Format a where
  36. format str a = replace "%d" str (show a)
  37.  
  38. replace :: String -> String -> String -> String
  39. replace f str value = intercalate value $ split str f
  40.  
  41. split :: String -> String -> [String]
  42. split [] f = [[]]
  43. split str [] = [str]
  44. split str@(x:xs) f | isPrefixOf f str = [[],drop (length f) str]
  45. | otherwise = let (y:ys) = split xs f
  46. in [x:y] ++ ys
Add Comment
Please, Sign In to add comment