프로그래밍 Programming
Haskell_ 002 Type variables
문장전달자
2018. 3. 9. 14:52
728x90
Type of the head function
head takes a list of any type and returns the first element. The type declaration of head states that it takes a list of any type and returns one element of that type.
- ghci> :t head
- head :: [a] -> a
What is this a? Types are written in capital case, so it can't exactly be a type. It's actually a type variable. That means that a can be of any type.
polymorphic functions : Functions that have type variables.
Type of the fst function
- ghci> :t fst
- fst :: (a, b) -> a
fst takes a tuple which contains two types and returns an element which is of the same type as the pair's first component.
Because a and b are different type variables, they don't have to be different types. It just states that the first component's type and the return value's type are the same.
본 카테고리의 내용은 Learn You a Haskell for Great Good! 의 내용을 학습을 위해 요약한 것입니다.
728x90