Haskell :: The Use of Brackets in Recursion -
i'm wondering, recursion example:
squaresrec :: [double] -> [double] squaresrec [] = [] squaresrec (x:xs) = x*x : squaresrec xs why on recursive case, there no bracket? shouldn't suppose this:
squaresrec :: [double] -> [double] squaresrec [] = [] squaresrec [x:xs] = x*x : squaresrec xs i know not work. wondering explanation behind it.
[] matches empty list.
[1] matches list containing 1 element, , must number equal one. note [1] syntactic sugar (1:[]), i.e. matches is: list beginning number 1, followed list empty... complicated way of saying “a list containing single element 1”.
(x:xs) matches list begins x, followed xs (and may contain number of elements, possibly zero). i.e. pattern matches list at least 1 element.
[x:xs] matches again list contains exactly 1 element, , element should match pattern (x:xs). (which doesn't make sense type-wise, because lists contain double-numbers, not lists.)
Comments
Post a Comment