Understanding Haskell's type system -
i've got 3 functions
boombangs xs = [if x < 10 "boom" else "bang" | x <- xs odd x] length' xs = sum [1 | _ <- xs] removenonuppercase st = [x | x <- st, x `elem` ['a'..'z']] here's type(type signature?) of each function
*main> :t boombangs boombangs :: integral => [a] -> [[char]] *main> :t length' length' :: num => [t] -> *main> :t removenonuppercase removenonuppercase :: [char] -> [char] haskell should give me error when pass array of ints removenonuppercase or string boombangs. too, how know it's wrong when haven't specified type anywhere.
also why types of boombangs , removenonuppercase different though work on same input.
i apologize if question seems vague or downright ignorant. i've started on learn haskell , i'm wrapping head around paradigm coming programming in c , python.
any resources learning more haskell , type system appreciated.
ghc uses type inference try guess type function can be.
in boombangs, can tell you're passing in list because of x <- xs in list comprehension, , can tell list elements must have type integral => a because you're applying odd each element. knows you're returning list of strings because of literal "boom" , "bang" inside list comprehension.
in length', type constrained sum, requires num => [a]. input type list of type because don't perform operations on elements themselves, indicate parameter list _ <- xs.
in removenonuppercase, sees you're passing in list same reasons first 2 functions, , since you're checking if each element in list ['a'..'z'], each element must char, you're taking list of chars , returning list of chars, since no other transformation performed on elements. since string alias [char], knows type fully.
Comments
Post a Comment