갈루아의 반서재

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.
  1. ghci> :t head  
  2. head :: [a] -> a  
What is this aTypes 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
  1. ghci> :t fst  
  2. 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