Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

Understanding the Haskell as-pattern

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

From the book (Chapter 4):

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

The book explains the @ symbol thus,

"...bind the variable xs to the value that matches the right side of the @ symbol."

I'm having trouble understanding this explanation. Supposing I call

suffixes "hello"

Explicitly, what would the above line with the @ do to this (on the first iteration)? I know what the result of the function is, but cannot see how we get there from the above code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...