
I've made a bizarre little program that I don't understand the behaviour of. It's: data BoolList = End | Node Bool BoolList instance Show BoolList where show (Node False rest) = "F " ++ show rest show (Node True rest) = "T " ++ show rest show End = "<>" grow list = (Node False rest) where (Node _ rest) = add list add End = (Node True End) add (Node truth rest) = (Node truth (add rest)) The show is fine; it's the add that's confusing me. Where we have, grow list = (Node False rest) where (Node _ rest) = add list I don't really understand what's going on with this binding and matching using "where", so I'm not seeing how the True and False values are managing to propagate up to being results of grow as they do, e.g.:
grow End F <> grow (grow End) F T <>
I have Hudak, Peterson, Fasel's gentle introduction to Haskell 98, and Thompson's "Craft of Functional Programming" book - maybe I just missed something in them? Is it easy to explain what's going on with this "where" behaviour so that I could figure out what "grow"'s results are without evaluating examples? -- Mark

----- Original Message -----
From: "Mark Carroll"
To:
I've made a bizarre little program that I don't understand the behaviour of. It's:
data BoolList = End | Node Bool BoolList
instance Show BoolList where show (Node False rest) = "F " ++ show rest show (Node True rest) = "T " ++ show rest show End = "<>"
grow list = (Node False rest) where (Node _ rest) = add list add End = (Node True End) add (Node truth rest) = (Node truth (add rest))
What "add list" does is adding a True to the end of the list, however, "grow list" will ignore the first element of the result, change it to False.... Main> Node True (Node False ( Node False (Node True End))) T F F T <> Main> grow(Node True (Node False ( Node False (Node True End)))) F F F T T <> in the example above, "add list" will return T F F T T <>, and then, "grow list" changes the first element to F. Hope this helps, Yao

On Wed, 1 Aug 2001, Yao Guo wrote: (snip)
What "add list" does is adding a True to the end of the list, however, "grow list" will ignore the first element of the result, change it to False.... (snip)
That makes a lot of sense. Thank you very much! I think I understand it now. (-: -- Mark
participants (2)
-
Mark Carroll
-
Yao Guo