The factorial function could be written using Z as follows: var factorial = number => factorial(6) // returns 720 In JavaScript, an external library needs to be used - for example, Z (pattern matching for Javascript). Some languages have pattern matching built in. The second is called if the value passed in is nonzero - in which case, the result of the expression n * factorial(n-1) is returned. The first is called if the value passed in is zero, in which case 1 is returned. Pattern matching could be thought of as dynamic dispatch (see above) based on a value rather than a type.Ī classic example is implementing a factorial function using two functions: factorial(0) ::= 1 factorial(n) ::= n * factorial(n-1) It’s generally popular with those who favour a more functional programming style. Pattern matching is the new kid on the block when it comes to branching.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |