Francisca Almeida
Francisca Almeida

Reputation: 29

Converting string to list in newType

I am trying to create a new data type called BigNumber which consists of a list of its digits. But when I try to create a function that receives a String and converts it to a BigNumber I always get an error.

newtype BigNumber = Digits [Char]

scanner :: String -> BigNumber
scanner a = id a

I have tried using the id function and map function but nothing works.

I always get the error

Couldn't match type ‘[Char]’ with ‘BigNumber’
  Expected type: BigNumber
    Actual type: String
• In the expression: id a
  In an equation for ‘scanner’: scanner a = id atypecheck(-Wdeferred-type-errors)

Upvotes: 1

Views: 73

Answers (1)

racherb
racherb

Reputation: 335

The error is due to the fact that the "scanner" function expects a BigNumber type and a String type is being passed.

This code should work:

newtype BigNumber = Digits [Char]
scanner :: String -> BigNumber
scanner = Digits

Upvotes: 1

Related Questions