Task

Demonstrate the different syntax and semantics provided for calling a function.



This may include:

   Calling a function that requires no arguments
   Calling a function with a fixed number of arguments
   Calling a function with optional arguments
   Calling a function with a variable number of arguments
   Calling a function with named arguments
   Using a function in statement context
   Using a function in first-class context within an expression
   Obtaining the return value of a function
   Distinguishing built-in functions and user-defined functions
   Distinguishing subroutines and functions
   Stating whether arguments are passed by value or by reference
   Is partial application possible and how


This task is not about defining functions.




 
 
The command
 
replace   :a0 :a1 ... an-1
     in   expression containing some occurences of :ai
     by   v0 v1 ... vp-1
 
is rewritten in a prefixed parenthesized form
 
{{lambda {:a0 :a1 ... an-1}
          expression containing some occurences of :ai}
          v0 v1 ... vp-1}
 
so called IIFE (Immediately Invoked Function Expression), and defines an anonymous function containing a sequence of n arguments :ai, immediately invoked on a sequence of p values vi, and returning the expression in its body as so modified:
 
1) if p < n     (partial application)
 
• the occurrences of the p first arguments are replaced in the function's body by the corresponding p given values,
• a function waiting for missing n-p values is created,
• and its reference is returned.
• example:
{{lambda {:x :y} ... :y ... :x ...} hello} 
-> {lambda {:y} ... :y ... hello ...}    // replaces :x by hello
-> LAMB_123                              // the new functions's reference
• called with the value world this function will return ... world ... hello ...
 
2) if p = n     (normal application)
 
• the occurences of the n arguments are replaced in the function's body by the corresponding p given values,
• the body is evaluated and the result is returned.
• example
{{lambda {:x :y} ... :y ... :x ...} hello world}
-> {{lambda {:y} ... :y ... hello ...} world}  // replaces :x by hello
-> {{lambda {} ... world ... hello ...} }      // replaces :y by world
->             ... world ... hello ...         // the value
 
3) if p > n    (variadicity)
 
• the occurrences of the n-1 first arguments are replaced in the function's body by the corresponding n-1 given values,
• the occurrences of the last argument are replaced in the body by the sequence of p-n supernumerary values,
• the body is evaluated and the result is returned.
• example:
{{lambda {:x :y} ... :y ... :x ...} hello world good morning}
-> {{lambda {:y} ... :y ... hello ...} world good morning}   
-> {{lambda {} ... world good morning ... hello ...}}   
->             ... world good morning ... hello ...        // the value
 
More can be seen in http://lambdaway.free.fr/lambdawalks/?view=lambda
 