Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "no parse" from Prelude.read

Tags:

haskell

I've just started Haskell, and I've been trying to make a simple Number data type, which has a single Int like so:

data Number
  = Num Int
  deriving (Read, Show)

I then tried to do read "1234" :: Number hoping that would give something like Num 1234, but I seem to be getting an "Exception: Prelude.read: no parse" instead. Is there something else that I'm missing?

like image 472
Sp3000 Avatar asked Sep 13 '14 11:09

Sp3000


Video Answer


1 Answers

The instance you derive would give read "Num 1234" = Num 1234.

This is the behavior one expects for instances of Read, but if you really want a different behavior, you'll have to implement read for Number yourself (you can re-use Int's, though).

like image 147
gspr Avatar answered Oct 03 '22 02:10

gspr