
Hi
data Bin = Zero | One As suggested by someone on this list. It's a really neat idea although I'm wondering how I can apply this to my int to binary function. The Zero or One declaration is saying that Bin is a data type that can hold either a one or a zero. I tried testing this with a very simple Bin -> Bin function and the Integral values 0 and 1 but ran into difficulties. Any ideas what I'm doing wrong here? Incidentally, does the general syntax above come from the old BNF notation? Cheers, Paul

To convert to and from integrals you could define two functions: binToNum :: Num a => Bin -> a binToNum Zero = 0 binToNum One = 1 numToBin :: Num a => a -> Bin numToBin 0 = Zero numToBin _ = One Or you could derive Enum for your Bin type. This wil automatically associate integers with the constructors of your type, in the order given. (See section 10.2 of the haskell report: http://www.haskell.org/onlinereport/derived.html) data Bin = Zero | One deriving Enum fromEnum Zero
0 fromEnum One 1
If this is not what you want, could you specify more precisely what difficulty you ran into?
participants (2)
-
PR Stanley
-
Roel van Dijk