list - Why can't ++ be used in pattern matching? -


From LearnYouAHaskell:

There is one more thing ???? You can not use ++ in pattern matching if you try to match the pattern against (xs ++ ys), what will happen in the first place and what will happen in the second list? It does not matter what it would be wise to match the stuff against (xs ++ [x, y, z]) or bus (xs + [x]), but due to the nature of the lists, you do not Can.

I am struggling to find out the reason for what it means by the nature of the lists, why can not it be so.

You match patterns against the creators, and there are two constructors for the list, empty list < Code> [] and 'cons' : , where the two arguments of the opposition are the main and tail of the list. ++ is a function to attach two lists, so you can not match it.

If you can get against it, then xs and ys in pattern xs ++ ys . For example, if you have also taken a small list like [1] then there are two possibilities

  xs == [] and ys == [1] xs = = [1] and ys == []   

Therefore the match is ambiguous, which is about the quote.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -