Haskell — How should I avoid head or tail, and adopt pattern matching?

  • A+
Category:Languages

I am a beginner in Haskell, and I have been told that I should avoid using head and tail for list operation and use pattern matching instead. I got a code snippet here, and I wonder how should this be converted so that no head or tail is used?

myzipWith :: (a->b->c) -> [a] -> [b] ->[c] myzipWith func [] [] = [] myzipWith func listA listB =     [func (head listA) (head listB) ] ++ (myzipWith func (tail listA) (tail listB)) 


myzipWith :: (a->b->c) -> [a] -> [b] ->[c] myzipWith func [] [] = [] myzipWith func (headA:restA) (headB:restB) =     [func headA headB] ++ myzipWith func restA restB 

But note the append (++) isn't necessary. This would be more idiomatic (and efficient):

    func headA headB : myzipWith func restA restB 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: