Haskell
Let f x y z = x : ( [ y ] : [ z ] ). What is the type of f?
The syntax provided in the question represents a Haskell function 'f' which takes three arguments 'x','y' and 'z' as input to the function at the time of function call.
By seeing the definition of function 'f', we deduce that the variable 'z' is of 'list' type since it is enclosed within square brackets ( [ ] ). Now, the type of ' [ y ] ' must be 'list' since it has to be combined with the list 'z' to form a resultant list ( Here, the operator ' : ' combines the two lists on either side to form a resultant list containing the two lists ). Therefore, to make the type of '[ y ]' to be list, the type of variable 'y' must be any basic type such as Char, Num, Fractional, etc since the square brackets will automatically convert it into a list. Accordingly, the variable 'x' must be of 'list' type since it has to be combined with the other two lists 'y' and 'z' by the ' : ' operator. The resultant would be a list containing the three lists.
Now, the operation of function 'f' is to return a list that contains the list x, list y and list z serially. This implies that the function 'f' is combining the three lists. Hence, one should keep in mind that the elements of list 'x' and list 'z' should be of the same type as the type of the variable 'y', otherwise, the lists would not be combined and the compiler would throw an error.
Hence, the return type of function 'f' is list of lists ( that is, a list containing other lists ) and the elements of the inner lists are of the type of the variable 'y' ( which can be any basic type , that is Char, Num, Fractional, etc. ).
GHCi, version 8.6.5 > let fx y z = x : ([y] : [ z ]) ?:tf f:: [a] -> a -> [a] -> [[a]] If [1,2] 5 [7,9] => [[1,2],[5],[7,9]] f['c', 'h'] 'e' ['g','g'] => ["ch", "e", "gg"]
Get Answers For Free
Most questions answered within 1 hours.