InterviewSolution
| 1. |
What are called Parameters and write a note on? |
|
Answer» 1. Parameter without Type 2. Parameter with Type Parameters (and arguments) Parameters are the variables in a function definition and arguments are the values which are passed to a function definition. (I) Parameter without Type Let us see an example of a function definition: (requires: b> = 0) (returns: a to the power of b) let rec pow a b: = if b = 0 then 1 else a * pow a (b – 1) In the above function definition variable ‘b’ is the parameter and the value which is passed to the variable ‘b’ is the argument. The precondition (requires) and postcondition (returns) of the function is given. Note we have not mentioned any types: (data types). Some language compiler solves this type (data type) inference problem algorithmically, but some require the type to be mentioned. In the above function definition if expression can return 1 in the then branch, by the typing rule the entire if expression has type int. Since the if expression has type ‘int ’ , the function’s return type also be ‘inf. ‘b ’is compared to 0 with the equality operator, so ‘b ’is also a type of ‘int. Since a is multiplied with another expression using the * operator, ‘a’ must be an int. (II) Parameter with Type Now let us write the same function definition with types for some reason: (requires: b > 0) (returns: a to the power of b) let rec pow (a: int) (b: int): int : = if b = 0 then 1 else a * pow b (a – 1) When we write the type annotations for ‘a ’ and ‘b ’ the parentheses are mandatory. Generally we can leave out these annotations, because it’s simpler to let the compiler infer them. There are times we may want to explicitly write down types. This is useful on times when you get a type error from the compiler that doesn’t make sense. Explicitly annotating the types can help with debugging such an error message. The syntax to define functions is close to the mathematical usage: the definition is introduced by the keyword let, followed by the name of the function and its arguments; then the formula that computes the image of the argument is written after an = sign. If you want to define a recursive function: use “let rec ” instead of “let Syntax: The syntax for function definitions: let rec fnal a2 … an : = k Here the fn is a variable indicating an identifier being used as a function name. The names ‘al ’ to ‘an ’ are variables indicating the identifiers used as parameters. The keyword ‘rec ’ is required if fn ’ is to be a recursive function; otherwise it may be omitted. For example: let us see an example to check whether the entered number is even or odd. (requires: x> = 0) let rec even x : = x = 0 || odd (x – 1) return ‘even’ (requires: x> = 0) let odd x : = x< >0 && even (x – 1) return ‘odd’ The syntax for function types: x → y x1 → x2 → y x1 → … → xn → y The ‘x’ and ‘y’ are variables indicating types. The type x → y is the type of a function that gets an input of type ‘x’ and returns an output of type ‘y’. Whereas x → x → y is a type of a function that takes two inputs, the first input is of type ‘x1 ’ and the second input of type ‘x2’ , and returns an output of type ‘y’. Likewise x1 → … → xn → y has type ‘x’ as input of n arguments and ‘y’ type as output. |
|