-- A recursive-descent parser for bit strings which computes -- the decimal value of its input -- the type of tokens delivered by the scanner: data Token = Bit0 | Bit1 | EOF deriving (Eq,Show) -- the semantic value is a pair of the number of digits and the value -- of the consumed input: digits :: [Token] -> ([Token],(Int,Int)) digits (t:ts) = case t of Bit0 -> let (ts1,(l,v)) = digits ts in (ts1,(l+1,v)) Bit1 -> let (ts1,(l,v)) = digits ts in (ts1,(l+1,2^l+v)) EOF -> (ts,(0,0)) parse :: [Token] -> Int parse ts = value where (_,(_,value)) = digits ts main1 = parse [Bit1, Bit0, Bit1, Bit1, EOF] main2 = parse [Bit1, Bit1, Bit1, Bit1, EOF] main3 = parse [Bit1, Bit0, Bit0, Bit0, Bit0, EOF]