9

How can I write in Erlang an equivalent of this Haskell snippet?

name@(x:xs)

ciembor
  • 7,189
  • 13
  • 59
  • 100

1 Answers1

11

You can do it with syntax like Name=[X|Xs]. An example usage is

headlist([H|T]=L) -> io:format("List (~p) with head ~p ~n",[L,H]).
Satvik
  • 11,238
  • 1
  • 38
  • 46
  • Note that it's often more readable to put the more specific pattern to the left and the variable to the right, as in headlist([H|T]=L) -> .... This way, multiple clauses align better, and you focus first on what is being matched rather than on the extra variable. – RichardC Jan 17 '13 at 22:58
  • I agree with @RichardC but just want to say that both are legal and fully equivalent with each other. – rvirding Jan 18 '13 at 08:08
  • @RichardC I don't code much in erlang so I am not very familiar about the coding practices. Thanks for pointing that out. – Satvik Jan 18 '13 at 09:17