[Haskell-beginners] type class question from Ternary Trees

MH mhamro at gmail.com
Wed Jul 29 10:28:41 EDT 2009


I am going over ternary tree library and it all looks clear to me with
exception of the method below. Could you please explain to me how to
read this:

data TernarySet a = Node !a !(TernarySet a) !(TernarySet a) !(TernarySet a)
                  | Null !(TernarySet a)
                  | End                deriving (Show, Eq)

instance Binary a => Binary (TernarySet a) where
...... skipped

   get = do  ---first call to get
        tag <- getWord8
        case tag of
            _ | tag < 8 ->
                do
                    ch <- get   --- second call to get
                    l <- if tag `testBit` 2 then get else return End
---third call to get
                    e <- if tag `testBit` 1 then get else return End
                    h <- if tag `testBit` 0 then get else return End
                    return (Node ch l e h)
            8 -> return (Null End)

           .......skipped
What I don't understand here is recursive(?) call to 'get'. The first
'get' as I understand is an instance implementation of get from Binary
module.
What is 'ch <- get' (the second call) and 'get' inside 'if tag
`testBit` 2 then get ...' (the third call)?
tag <- getWord8 reads word8 from monad, then ch <- get reads what
(just a copy of tag???)?
Is get inside if ..then else statements is a look ahead get calls, to
read next word8?
Please provide more details if possible.
Thanks a lot.

Malik


More information about the Beginners mailing list