Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do @ and ! mean?

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

I only know that @ here is called "AsPattern". But how does it actually work here ? Is there anyone can give me a analysis?

And how does ! work in haskell. I can not give an example right now. But I am pretty sure it does occur in the haskell programming .

like image 953
Anthony Avatar asked Dec 04 '22 03:12

Anthony


2 Answers

The @ symbol allows you to pattern match and refer to the entire value you are pattern matching against. In your example you can refer to the whole list argument as xs (using the @ symbol) and the tail of the list as xs' (using pattern matching).

The ! symbol can be used to force a value to be evaluated strictly instead of lazily (using bang patterns or strictness annotations).

like image 92
bwroga Avatar answered Dec 31 '22 10:12

bwroga


In Haskell, you can use an As Pattern to give an argument an alternative name by which you can refer to it. In your case,

xs@(_:xs')

allows you to use xs as an alternative name for the argument (_:xs'). Otherwise, you wouldn't be able to do this because _ matches any argument, but is unusable otherwise.

like image 31
zcleghern Avatar answered Dec 31 '22 11:12

zcleghern