[Haskell-cafe] Can this be improved?
Chris Kuklewicz
haskell at list.mightyreason.com
Tue Dec 27 11:39:34 EST 2005
Happy Holidays,
I was wondering if it this small snippet of code could be altered to
require fewer OPTIONS -fallow-... switches.
It creates a show-what-i-mean function called "swim" that takes a
variable number of arguments, and treats strings as-is, calling show on
the other arguments.
> {-# OPTIONS -fglasgow-exts #-}
> {-# OPTIONS -fallow-undecidable-instances #-}
> {-# OPTIONS -fallow-incoherent-instances #-}
> {-# OPTIONS -fallow-overlapping-instances #-}
>
> -- Want to show strings "as-is"
>
> class (Show a) => MyShow a where
> myShow :: a -> String
> myShow = show
>
> instance MyShow String where
> myShow = id
>
> instance (Show b) => MyShow b
>
> -- Want to take variable number of arguments
>
> class PVShow a r where
> pvShow :: String -> a -> r
>
> instance (MyShow b) => PVShow b String where
> pvShow oldString b = oldString ++ (myShow b)
>
> instance (MyShow b,PVShow a r) => PVShow b (a->r) where
> pvShow oldString b = pvShow (oldString ++ (myShow b))
>
> -- swim is short for Show What I Mean
>
> swim :: (PVShow a r) => a -> r
> swim = pvShow ""
>
> -- Demonstates of polymorphic and first class nature of swim
>
> test foo = foo " and " 7 " are "
>
> -- Tests
>
> main = do putStrLn $ swim "Hello" " " "World #" [17,18,19] "!"
> putStrLn $ test (swim "I also think " [4,5,6]) "cool" "."
GHCI session:
Main> main
Hello World #[17,18,19]!
I also think [4,5,6] and 7 are cool.
*Main>
Could this be improved by TypeEq machinery? Is there a Haskell 98 way
to make myShow work this way?
--
Chris Kuklewicz
More information about the Haskell-Cafe
mailing list