I'm talking mostly about functional programming languages. For example, the wikipedia article for map has this example in a Haskell-ish language:
map square [1,2,3,4,5]
How does the parser/compiler know that we want to pass the function square to map as a higher-order function, and not trying to invoke the function itself? For languages with static typing, the expression square [1,2,3,4,5] obviously wouldn't compile, but would a compiler really use that to determine that's not what I meant?
Or, is this just a bad example from Wikipedia, and a better example might look like map &square [1,2,3,4,5] (using C-style function referencing)?
First of all function application has the highest precedence. so when the parser encounters "map" it will take the first thing to its right as the first argument. map type expects a function as first argument, and "square" is defined as a function, so types are compatible. You can think of map square as a function that is expecting a list of numbers.
The key is to examine types, and verify arguments types, following precedence rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With