![haskell algebraic javascript emulator haskell algebraic javascript emulator](https://res.cloudinary.com/practicaldev/image/fetch/s--R7-XLz1R--/c_imagga_scale,f_auto,fl_progressive,h_500,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1j2ttyuiqe5jvhpkd63.png)
Parentheses are required around patterns consisting of more than just a single constructor.
![haskell algebraic javascript emulator haskell algebraic javascript emulator](https://i.stack.imgur.com/DkSMI.png)
Note how we also get to give names to the values that come along with each constructor. Previous section), we could write something like foo (Constr1 a b) =. This information can be used as the basis for deciding what to do-indeed, in Haskell, this is the only way to make a decision.įor example, to decide what to do with a value of type AlgDataType (the made-up type defined in the Fundamentally, pattern matching is about taking apart a value by finding out which constructor it was built with. We've seen pattern matching in a few specific cases, but let’s see how pattern-matching works in general. (Otherwise, Haskell parsers would have quite a difficult job figuring out which names represent variables and which represent constructors). One final note: type and data constructor names must always start with a capital letter variables (including names of functions) must always start with a lowercase letter. For example, if it was constructed using Constr1, then it comes along with two values, one of type Type11and one of type Type12. Depending on the constructor used, an AlgDataType value may contain some other values. This specifies that a value of type AlgDataType can be constructed in one of four ways: using Constr1, Constr2, Constr3, or Constr4. In general, an algebraic data type has one or more data constructors, and each data constructor can have zero or more arguments. This idiom (giving the type and data constructor of a one-constructor type the same name) is common, but can be confusing until you get used to it. Notice how the type constructor and data constructor are both named Person, but they inhabit different namespaces and are different things.
![haskell algebraic javascript emulator haskell algebraic javascript emulator](https://goodfirstissue.dev/images/meta.jpg)
Store a person's name, age, and favorite Thing Main = print (failureToZero Failure, failureToZero (OK 3.4))ĭata constructors can have more than one argument: - /show For some applications, we might consider mapping a failed computation to a value of zero: - /showįailureToZero :: FailableDouble -> Double More pattern matching! Notice how in the OK case we can give a name to the Double that comes along with it. SafeDiv :: Double -> Double -> FailableDouble Here's one way we might use our new FailableDouble type: - /show showįor any x of type Double, OK x has type FailableDouble. For example, OK 3.4 is a value of type FailableDouble. So OK by itself is not a value of type FailableDouble we need to give it a Double. The first one, OK, takes anĪrgument of type Double. This says that the FailableDouble type has two data constructors. As a first example of a data type which is not just an enumeration, consider the definition of FailableDouble: data FailableDouble = Failure However, enumerations are actually only a special case of Haskell’s more general algebraic data types. Thing is an enumeration type, similar to those provided by other languages such as Java or C++. Main = print (isSmall2 Cabbage) Beyond Enumerations Recalling how function clauses are tried in order from top to bottom, we could also make the definition of isSmall a bit shorter like so: - /show We can write functions on Things by pattern matching: - /show This declares a new type called Thing with five data constructors Shoe, Ship, etc which are the (only) values of type Thing. Here’s a simple example: data Thing = Shoe Like many programming languages, Haskell allows programmers to create their own enumeration types.