[Haskell-beginners] Recursion with a self-defined type
Galaxy Being
borgauf at gmail.com
Fri Mar 12 16:19:16 UTC 2021
I'm trying to translate *The Little MLer *into Haskell. I've got this
data Shishkebab = Skewer | Onion Shishkebab | Lamb Shishkebab | Tomato
Shishkebab deriving Show
Then I have this which works
veggieKebab :: Shishkebab -> Bool
veggieKebab Skewer = True
veggieKebab (Onion (shk)) = veggieKebab shk
veggieKebab (Tomato (shk)) = veggieKebab shk
veggieKebab (Lamb (shk)) = False
> veggieKebab (Tomato (Onion (Tomato (Onion Skewer))))
True
but I'm wondering if I could do something like this
veggieKebab :: Shishkebab -> Bool
veggieKebab Skewer = True
veggieKebab (shkb (sk)) | (shkb == Onion) || (shkb == Tomato) = veggieKebab
sk
| otherwise = False
This doesn't work, giving a "Parse error in pattern: shkb". I've been
advised that I'm trying to treat what is a data constructor like a
variable, but I can't fathom what that means in this case. What I'm trying
to leverage is what I've learned from dealing with lists and recursion
through the consed list. So if effect I'm trying to recurse through a
consed Shishkebab object. It works in the first case, but hyow could I do
this in this more generic way like the second try does?
LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210312/418bdcae/attachment.html>
More information about the Beginners
mailing list