If a function is generic and takes a parameter 'a list
, the type of 'a
could also be a list. So the function would work with lists, or lists of lists, or lists of lists of lists, ...so long as the outer type is a list, 'a
could be anything.
For example:
let isNonEmpty = function
| [] -> false
| _::_ -> true
isNonEmpty [1; 2; 3]
isNonEmpty [[1]; [2]; [3]]
If your function doesn't depend on the list elements being of a certain type it should probably be generic. If you post your code perhaps someone can help with that.
EDIT
A naive version of your flatten
function, without using built-in functions, might be:
let flatten lst =
let rec helper = function
| [] -> []
| h::tl ->
match h with
| Elem x -> x::helper tl
| List xs -> helper xs @ helper tl
helper [lst]