Created
June 17, 2015 21:54
-
-
Save davidallsopp/fc67b34dc8dc06f062b2 to your computer and use it in GitHub Desktop.
Constructor fields in Haskell - see https://www.haskell.org/tutorial/moretypes.html, section 6.2 Field Labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> data Point = Point {x :: Int} deriving Show | |
Prelude> Point 1 | |
Point {x = 1} | |
Prelude> :t x | |
x :: Point -> Int | |
Prelude> :t Point | |
Point :: Int -> Point | |
Prelude> let p = Point 2 | |
Prelude> x p | |
2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The field names can be used as selector functions to extract a component from a structure. In this simple case, with just one parameter, the accessor
x
is the inverse of the data ConstructorPoint
.