Questions tagged [value-constructor]

10 questions
12
votes
2 answers

What is the difference between value constructors and tuples?

It's written that Haskell tuples are simply a different syntax for algebraic data types. Similarly, there are examples of how to redefine value constructors with tuples. For example, a Tree data type in Haskell might be written as data Tree a =…
ely
  • 74,674
  • 34
  • 147
  • 228
11
votes
2 answers

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

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…
8
votes
1 answer

Why am I able to use my value constructor even though I don't export it?

For practice, I'm implementing a queue data type in a module called "Queue". My data type is also called "Queue", as is its only value constructor: module Queue (Queue, enq, emptyQueue) where data Queue a = Queue { inbox :: [a], outbox ::…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
3
votes
3 answers

data type with a default field and that needs a function that works with it

Say, I have a data type data FooBar a = Foo String Char [a] | Bar String Int [a] I need to create values of this type and give empty list as the second field: Foo "hello" 'a' [] or Bar "world" 1 [] 1) I do this everywhere in my code…
akonsu
  • 28,824
  • 33
  • 119
  • 194
2
votes
2 answers

How to define a function that takes natural numbers of a data type and return their sum?

I am still learning how value constructors work. I read a lot about data types and their value constructors. The problem is that I never saw them in practice. For example data Nat = Zero | Succ Nat Now if we want to define a functions that takes…
Cara
  • 165
  • 1
  • 4
  • 15
1
vote
1 answer

Value constructors in haskell

I have a question about Haskell. I'm new so I don't understand too well, but if anyone could help me I'd really appreciate. I have this exercise from a book that I bought. Create a type Question with value contructors Sim or Nao. Make a function…
1
vote
0 answers

Define function independent of specific value constructor (templates?)

Is it possible to write a function for generic types (in this case applicatives) without making assumptions about the name of type constructors? I can write the following: f :: Maybe a -> Maybe a f (Just a) = (Just a) That one works on Maybes and…
1
vote
1 answer

Why are types declared in value constructors not types in Haskell?

Suppose I have the following value constructor: data Shape = Circle Float Float Float | Rectangle Float Float Float Float Now I can run: ghci> :t Circle Circle :: Float -> Float -> Float -> Shape Now I can write a type declaration surface :: Shape…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
0
votes
1 answer

Working with value constructors in Haskell

I'm quite stuck in this exercise, the program should receive an input like: dividing (Number 50) (Number 10) And then output something like: Number 10 I tried this: data Number = Ok Double | Error String deriving Show dividing :: Number ->…
devinho
  • 404
  • 4
  • 18
0
votes
1 answer

Default and value constructed objects have different types

Consider the following code with one variable default constructed and the other one value constructed: #include #include struct A { A() : m_() {} A(int m) : m_(m) {} int m_; }; int main() { A a, b(), c(5); …
Erwin411
  • 734
  • 1
  • 7
  • 14