I have defined data type for Binary numbers as follows
data Bin = Nil | O Bin | I Bin
deriving (Show, Eq)
i want to define a function reverse :: Bin -> Bin so that when i give input like
reverse (I (O (I (I Nil)))) i should get the outut
I (I (O (I Nil))) that means reversed as input, any body please give me hint how i can do this ?
Why are you doing this this way? Why not something like this:
data Bit = I | O
newtype Bin = List Bit
Then you could just use the Prelude's reverse operation directly...
Edit A simple substitution from the Prelude's function:
reverse x = rev x []
where
rev [] a = a
rev (x:xs) a = rev xs (x:a)
yields:
reverse x = rev x Nil
where
rev Nil a = a
rev (I xs) a = rev xs (I a)
rev (O xs) a = rev xs (O a)
The thing is, your type is very similar to the list type:
data List a = a : (List a) | []
so the logic for the List routines applies directly to your type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With