Quick actions

cmd+k|ctrl+k

Navigation

Languages

List Concatenation, Associativity Matters 

Snippet info

Language

Haskell

Visibility

public

Author

homam

Created

2016-09-26T18:15:39Z

Updated

2016-09-26T19:55:17Z

import Debug.Trace (trace)

data List a  = Empty | a ::: List a
infixr 5 :::

instance (Show a) => Show (List a) where
  show Empty = "Empty"
  show (h ::: t) = show h ++ " ::: " ++ show t


(.++.) :: (Show a) => List a -> List a -> List a
Empty .++. r  = r
(h ::: t) .++. r = trace (show h) h ::: (t .++. r)

x = (9 ::: 8 ::: 7 ::: Empty) :: List Int
y = (6 ::: 5 ::: 4 ::: Empty) :: List Int
z = (3 ::: 2 ::: 1 ::: Empty) :: List Int

main = do

  -- time: 2 |x| + |y|
  print $ (x .++. y) .++. z
  
  trace (take 10 $ repeat '-') (return ())
  
  -- time: |x| + |y|
  print $ x .++. (y .++. z)
INFO