Re:Rust creator Graydon Hoare is now at Apple working on Swift
by chwind on Tuesday January 31, 2017 @03:10PM
(#13534736)
what is a function head?
[ Parent
| on HN ]
Re:Rust creator Graydon Hoare is now at Apple working on Swift
by ZephyrP on Tuesday January 31, 2017 @03:23PM
(#13534836)
In some languages, a function can be defined multiple times with different signatures (multiple 'heads'). It will try each clause until it finds one that matches, allowing you to filter or change course on the basis of the value or characteristics of the arguments.
This allows you to easily create special logic for corner cases and is generally extremely useful.
Re:Rust creator Graydon Hoare is now at Apple working on Swift
by macintux on Tuesday January 31, 2017 @06:26PM
(#13536454)
If you look at the Wikipedia article for Greatest Common Divisor, you see this:
gcd(a, 0) = a
gcd(a, b) = gcd(b, a mod b)
That, in effect, is what pattern matching + function heads gives you. You can copy that function directly into ML or Erlang, modulo a couple tiny bits of syntax. gcd(A, 0) -> A;
gcd(A, B) -> gcd(B, A rem B).
Re:Rust creator Graydon Hoare is now at Apple working on Swift
by aaron-santos on Tuesday January 31, 2017 @03:30PM
(#13534896)
Pattern matching can be used in the parameter list of function's definition.