Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pattern matching
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Haskell==== In Haskell, the following line defines an algebraic data type <code>Color</code> that has a single data constructor <code>ColorConstructor</code> that wraps an integer and a string. <syntaxhighlight lang="haskell"> data Color = ColorConstructor Integer String </syntaxhighlight> The constructor is a node in a tree and the integer and string are leaves in branches. When we want to write [[function (programming)|functions]] to make <code>Color</code> an [[abstract data type]], we wish to write functions to [[Interface (computer science)|interface]] with the data type, and thus we want to extract some data from the data type, for example, just the string or just the integer part of <code>Color</code>. If we pass a variable that is of type Color, how can we get the data out of this variable? For example, for a function to get the integer part of <code>Color</code>, we can use a simple tree pattern and write: <syntaxhighlight lang="haskell"> integerPart (ColorConstructor theInteger _) = theInteger </syntaxhighlight> As well: <syntaxhighlight lang="haskell"> stringPart (ColorConstructor _ theString) = theString </syntaxhighlight> The creations of these functions can be automated by Haskell's data [[Record (computer science)|record]] syntax.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)