Real World Haskell - 第4章 Part10 As-patterns

Real World HaskellHaskell を継続的に勉強中。


Chapter 4. Functional programming
Chapter 4. Functional programming

As-patterns

  • as-pattern を使うと読みやすいコードが書ける
  • as-pattern ではデータのコピーが発生しないので allocation を節約できる

The pattern xs@(_:xs') is called an as-pattern, and it means “bind the variable xs to the value that matches the right side of the @ symbol”.

suffixes :: [a] -> [[a]]
suffixes xs@(_:xs') = xs : suffixes xs'
suffixes _ = []
noAsPattern :: [a] -> [[a]]
noAsPattern (x:xs) = (x:xs) : noAsPattern xs
noAsPattern _ = []