Quantcast
Channel: Haskell - data type functions - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Haskell - data type functions

$
0
0

I'm learning Haskell and started looking over data types. I tried doing a simple example with converting Yard and feet to inches with a data type defined as LengthUnit. I want to add two LengthUnit vars so I created a helper function called convert that would take in a LengthUnit and convert it to inches.

I tried to do the following but I keep getting an error 'Couldnt match expected type LengthUnit with type Int.

Here is what I have:

data LengthUnit =  INCH  Int | FOOT  Int | YARD  Int                   deriving (Show, Read, Eq)convert :: LengthUnit -> Int    convert (INCH x) = x convert (FOOT x) = x * 12convert (YARD x) = x * 36-- addLengths addLengths :: LengthUnit -> LengthUnit -> LengthUnitaddLengths (INCH x) (INCH y) = convert(x) + convert(y)-- I tried this as well and still receive same erroraddLengths (INCH x) (INCH y) = x + yaddLengths (INCH x) (FOOT y) = convert(x) + convert(y)...

I cant seem to find the equivalence of :

addLengths (LengthUnit x) (LengthUnit y) = convert(x) + convert(y)

Any help is appreciated, thanks!


Viewing all articles
Browse latest Browse all 2

Trending Articles