iMatthewCM
iMatthewCM

Reputation: 449

Haskell - Types, Enums, and Functions

Good morning everyone,

Here's what I'm working on today, and the issue I'm running in to:

--A
data Row = A | B | C | D | E | F | G | H | I | J deriving (Enum, Ord, Show, Bounded, Eq, Read)
data Column = One | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten  deriving (Enum, Ord, Show, Bounded, Eq, Read)
--B
data Address = Address Row Column deriving (Show, Read, Eq)

Then a few lines later I get to the problem child:

toAddress r c = Address(toEnum r, toEnum c)

I need to feed Address a Row and Column, but I need to turn r and c into Row and Column (not Ints)

Obviously toAddress is not structured correctly to carry out this task. The requirement is as follows:

Write a function toAddress that takes in a row and column, each in [0 − 9]. Construct an Address and return it. Use toEnum to index into your Row and Column enums lists.

Does anyone have any suggestions on how to accomplish what I'm going for here?

Thank you!

Upvotes: 2

Views: 423

Answers (1)

Martin Ring
Martin Ring

Reputation: 5426

You got the syntax wrong.

A function application of a function f :: A -> B -> C in haskell looks like this f a b and not f(a,b). f(a,b) still is correct syntax but not what you want: it passes only one parameter to the function (i.e. the tuple consisting of a and b).

So the correct implementation of toAddress looks like this:

toAddress r c = Address (toEnum r) (toEnum c)

Upvotes: 3

Related Questions