Append operation in haskell might be costlier ,if done this way

let x = “hello” ++ ” haskell”

let y = x ++ ” well done ”

to append this way have to traverse whole list each time ,a better alternative is :

(str1 ++) .(str2 ++ ).(str 3 ++) ……….

to do so in practice :

data DL a = DL {unwrap :: [a]->[a]}

createDL = DL ( [] ++ )

toList (DL f ) = f []

toDList :: DL a -> [a]-> DL a

toDList (DL f) a =DL $ f . (a ++)

instance Functor DL where

fmap f xs = DL (fmap f (toList xs) ++ )

Advertisements