Understanding the Haskell as-pattern

前端 未结 2 500
你的背包
你的背包 2020-12-11 08:55

I\'m reading through Real World Haskell, and am trying to understand the as-pattern.

From the book (Chapter 4):

suffixes :: [a] -> [[a]]
suffixes          


        
相关标签:
2条回答
  • 2020-12-11 09:16

    xs' would be bound to the string "ello".

    xs would be bound to the string "hello".

    The @ pattern lets you give a name to a variable while also matching its structure and possibly giving name to the components.

    0 讨论(0)
  • 2020-12-11 09:32

    Perhaps an actual "de-sugaring" will make it easier to understand:

    suffixes xs@(_:xs') = xs : suffixes xs'
    

    is equivalent to

    suffixes xs
     | (_:xs') <- xs   = xs : suffixes xs'
    

    i.e. you're firstly binding the entire argument to the variable xs, but you also do pattern matching on the same argument (or, equivalently, on xs) to (_:xs').

    0 讨论(0)
提交回复
热议问题