algebraic-data-types

Why can't i re-use same value constructor among different data types?

蹲街弑〆低调 提交于 2020-02-15 07:37:13
问题 i am new to Haskell and probably missing something really basic here, but i am not able to re-use same value constructor among different data types. data Colour = Red | Pink | Orange | Yellow data Fruit = Apple | Orange | Banana This produces error saying Multiple declarations of ‘Orange’ Not sure why this isn't allowed, i have been using OCaml before learning Haskell and was able to define types like this 回答1: As a quick exercise try just defining one of your data types and then opening up

How to extract a list of fields from a list of a given data type?

删除回忆录丶 提交于 2020-01-17 13:26:29
问题 If I have defined a datatype with, let's say, 5 attributes. How would I be able to create a list of one of the attributes. Ex : data Person = Person name fname sexe age height Marie, John, Jessie :: Person Marie = Person "Marie" _ _ _ John = Person "John" _ _ _ Jessie = Person "Jessie" _ _ _ How can I return a list containing all the names : ( Marie , John , Jessie ) 回答1: Your code isn't valid Haskell. You could have data Person = Person FName LName Sexe Age Height type FName = String type

What is an Algebraic Data Type (ADT)?

风格不统一 提交于 2020-01-12 15:16:52
问题 I have heard people talking a lot about Algebraic Data Types (not to be confused with "Abstract Data Types") in functional programming. All I know is that ADT refers to some kind of composite (often recursive) data types like trees or math expressions. In Wikipedia, it's only said that: an algebraic data type is a kind of composite type, i.e., a type formed by combining other types. Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e. tagged

Record syntax and sum types

狂风中的少年 提交于 2020-01-12 06:22:06
问题 I have this question about sum types in Haskell. I'd like to create a sum type which is comprised of two or more other types, and each of the types may contain multiple fields. A trivial example would be like this: data T3 = T1 { a :: Int, b :: Float} | T2 { x :: Char } deriving (Show) In my understanding, T1 and T2 are data constructors which use record syntax. It seems that the definition of T3 will grow as the number of fields in T1 or T2 increases. My question is that how to practically

Surjectivity check when return type is sealed

自闭症网瘾萝莉.ら 提交于 2020-01-01 09:50:40
问题 Scala can warn when pattern match on a sealed type is not exhaustive, however can we check that a function returns all cases when the return type is sealed? For example, consider the following ADT sealed trait Foo case object Bar extends Foo case object Qux extends Foo Then function f: Foo => String on the algebraic data type Foo def f(x: Foo): String = x match { case Bar => "bar" } raises warning match may not be exhaustive. It would fail on the following input: Qux def f(x: Foo) = x match {

Algebraic data types in Kotlin

岁酱吖の 提交于 2019-12-31 10:58:27
问题 I am trying to figure out how to use algebraic data types in Kotlin, so I'm trying to implement a basic BinaryTree type the following way. sealed class Tree<T>{ class Node<T>(val left: Tree<T>, val right: Tree<T>): Tree<T>() class Leaf<T>(val value: T): Tree<T>() } This is all fine, and lets me construct the following tree: val myTree1: Tree<Int> = Node(Leaf(4), Leaf(2)) However I would like to have an "Empty" type as well, so I can express the following: val myTree1: Tree<Int> = Node(Node

haskell sum type multiple declaration error

半世苍凉 提交于 2019-12-29 09:34:14
问题 data A=A data B=B data AB=A|B Which makes a sum type AB from A and B. but the last line induces a compile error "multiple declarations of B" I also tried sth like this: data A=Int|Bool It compiles. but why ghc disallows me from making sum types for user-defined types? 回答1: You're getting fooled. You think when you write data A=Int|Bool that you are saying that a value of type A can be a value of type Int or a value of type Bool ; but what you are actually saying is that there are two new

How do you represent a graph in Haskell?

烂漫一生 提交于 2019-12-27 12:42:29
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

How do you represent a graph in Haskell?

帅比萌擦擦* 提交于 2019-12-27 12:40:10
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

How do you represent a graph in Haskell?

旧巷老猫 提交于 2019-12-27 12:39:50
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current