algebraic-data-types

Why are Haskell algebraic data types "closed"?

Correct me if I'm wrong, but it seems like algebraic data types in Haskell are useful in many of the cases where you would use classes and inheritance in OO languages. But there is a big difference: once an algebraic data type is declared, it can not be extended elsewhere. It is "closed". In OO, you can extend already defined classes. Fo...

Choosing among alternatives in a Haskell algebraic datatype

When type X is defined as: data X = X { sVal :: String } | I { iVal :: Int } | B { bVal :: Bool } and I want the Int inside an X value, if there is one, otherwise zero. returnInt :: X -> Int How can I determine which type of X the argument to returnInt is? ...

Defining recursive algebraic data types in XML XSD

Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr = Zero | One | Add Expr Expr | Mul Expr Expr I'd like to represent this in XML, and I'd like an XSD schema for it. I have figured out how to achieve this syntax: <Expr> <Add> <Expr> <Zero/> </Expr> <...

"Pattern matching" of algebraic type data constructors

Let's consider a data type with many constructors: data T = Alpha Int | Beta Int | Gamma Int Int | Delta Int I want to write a function to check if two values are produced with the same constructor: sameK (Alpha _) (Alpha _) = True sameK (Beta _) (Beta _) = True sameK (Gamma _ _) (Gamma _ _) = True sameK _ _ = False Maintaining sam...

ADTs in F# and Scala

What are the key differences between ADTs in F# and Scala? Is there anything that F#'s ADTs can do but Scala's ADTs cannot (and vice versa)? ...

Memory footprint of Haskell data types

How to find the actual amount of memory required to store a value of some data type in Haskell (mostly with GHC)? Is it possible to evaluate it in runtime (e.g. in GHCi) or is it possible to estimate memory requirements of a compound data type from its components? In general, if memory requirements of types a and b are known, what is me...

Is there any algebraic structures used in functional programming other then monoid?

I recently getting to know about functional programming (in Haskell and Scala). It's capabilities and elegance is quite charming. But when I met Monads, which makes use of an algebraic structure named Monoid, I was surprised and glad to see the theoretic knowledge I have been learning from Mathematics is made use of in programming. T...

F# / SIlverlight Binding to algebraic datatypes...

Given a data structure of: type Candidate = SalesRep of SalesRep | Analyst of Analyst type ScorableCandidate = { candidate: Candidate ; mutable comments: string ; mutable score: int ; } and a data grid that wants to be able to display either of the candidates, is it possible to bind (using the WPF binding) to the Scorab...

Algebraic data types outside of functional languages?

Mostly out of curiosity: Which languages that are not solely functional (I'm also interested in multi-paradigm languages - I know that Ocaml and F# are ML dialects with OO added, so they inherit the algebraic data types from ML) have algebraic data types (or something similar) and pattern matching? They can be kind-of emulated using enu...

Is there a Haskell equivalent of OOP's abstract classes, using algebraic data types or polymorphism?

In Haskell, is it possible to write a function with a signature that can accept two different (although similar) data types, and operate differently depending on what type is passed in? An example might make my question clearer. If I have a function named myFunction, and two types named MyTypeA and MyTypeB, can I define myFunction so th...