how to feel free of the Implementation of Data structure

1. To use the Data.List is so easy as it's in the 'prelude'. but when I want to get some elements by index, I need to use Array, or sometime I want to use ByteString for some good reason. We all know It's some 'linear ordered things'. Is there some method for me to use just one form of 'list' with every possible function on it. 2. f1 :: (Integral a) => a -> String f2 :: (Integral a) => String -> a pipe = f1 . f2 It is clear that pipe is String -> String, but.(1)How can I know what instance that 'a' is. (2)How can I determine what instance of 'a' is. -- ---------------- 吴兴博

1. To use the Data.List is so easy as it's in the 'prelude'. but when I want to get some elements by index, I need to use Array, or sometime I want to use ByteString for some good reason. We all know It's some 'linear ordered things'. Is there some method for me to use just one form of 'list' with every possible function on it.
Edward Kmett has recently released the "keys" [1] package which abstracts away data structures that can be indexed by providing an Indexable typeclass. It's quite simple to use. Here is example usage: import Data.Key import Data.Array hiding ((!)) import Data.ByteString.UTF8 testList = [1,2,3,4,5] testArray = listArray (0,4) [1..] testString = "12345" main = do byteString <- return $ fromString testString string <- return testString print $ testList ! 2 print $ testArray ! 2 print $ string ! 2 print $ (toString byteString) ! 2 *Main> main 3 3 '3' '3' In the example I have to import Data.Array with the "hiding" constraint because (!) is exported by both Data.Array and Data.Key.
2. f1 :: (Integral a) => a -> String f2 :: (Integral a) => String -> a
pipe = f1 . f2 It is clear that pipe is String -> String, but.(1)How can I know what instance that 'a' is. (2)How can I determine what instance of 'a' is.
Withing f1 and f2 you cannot, all these functions know about is that 'a' implements functions in the typeclass Integral. There might be some reflection magic that can get this information for you but I don't think it is intended usage. -deech

On Thu, Mar 24, 2011 at 06:34:57PM +0800, 吴兴博 wrote:
2. f1 :: (Integral a) => a -> String f2 :: (Integral a) => String -> a
pipe = f1 . f2 It is clear that pipe is String -> String,
Actually, this code will give an "ambiguous type" error, since the compiler cannot figure out what type 'a' should be. And the type that is chosen may make a difference for the behavior.
but.(1)How can I know what instance that 'a' is. (2)How can I determine what instance of 'a' is.
If you know what type you want to use for 'a' you can specify it with a type annotation, like this: pipe = f1 . (f2 :: String -> Int) -Brent

:) Thanks.
2011/3/24 Brent Yorgey
On Thu, Mar 24, 2011 at 06:34:57PM +0800, 吴兴博 wrote:
2. f1 :: (Integral a) => a -> String f2 :: (Integral a) => String -> a
pipe = f1 . f2 It is clear that pipe is String -> String,
Actually, this code will give an "ambiguous type" error, since the compiler cannot figure out what type 'a' should be. And the type that is chosen may make a difference for the behavior.
but.(1)How can I know what instance that 'a' is. (2)How can I determine what instance of 'a' is.
If you know what type you want to use for 'a' you can specify it with a type annotation, like this:
pipe = f1 . (f2 :: String -> Int)
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ---------------- 吴兴博

There's also ListLike: http://hackage.haskell.org/package/ListLike
--
Markus Läll
On Fri, Mar 25, 2011 at 6:32 AM, 吴兴博
:) Thanks.
2011/3/24 Brent Yorgey
: On Thu, Mar 24, 2011 at 06:34:57PM +0800, 吴兴博 wrote:
2. f1 :: (Integral a) => a -> String f2 :: (Integral a) => String -> a
pipe = f1 . f2 It is clear that pipe is String -> String,
Actually, this code will give an "ambiguous type" error, since the compiler cannot figure out what type 'a' should be. And the type that is chosen may make a difference for the behavior.
but.(1)How can I know what instance that 'a' is. (2)How can I determine what instance of 'a' is.
If you know what type you want to use for 'a' you can specify it with a type annotation, like this:
pipe = f1 . (f2 :: String -> Int)
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ---------------- 吴兴博
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
aditya siram
-
Brent Yorgey
-
Markus Läll
-
吴兴博