[Haskell-beginners] how to feel free of the Implementation of Data structure

aditya siram aditya.siram at gmail.com
Thu Mar 24 16:22:20 CET 2011

> 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

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.


More information about the Beginners mailing list