Wireless
Wireless

Reputation: 107

Trouble Understanding Haskell Trees

I'm having a few issues with trees, I was trying to make a simplified Family tree (genealogy tree) where I start with only 1 person, let's say Grandpa Bob, I'll try my best drawing this:

                                        BOB
                                         |
                                     /        \
                                 SIMON         RUDY
                                /    \         /  \
                            ROBBIE  MARTHA  TOM   ISABEL

So, Grandpa Bob had 2 kids with Grandma(which doesn't matter here), Simon and Rudy, then Simon and Rudy both had 2 kids each (only 1 parent matters again), note that this is not necessarily the tree I want to make but only an example which can be used by you guys to help me out. I want to have it as a data Family, then have a function that initiates the "root" which will be Grandpa Bob and then have another function that will let me add someone to the tree, as in add Simon descending from Bob.

So far this is what I have atm(I've tried other things and changed it all over):

    module Geneaology where
    data Family a = Root a [Family a] | Empty deriving (Show, Read)

    main :: IO()
    main = do
    root :: String -> Family
    root a = ((Root a) [Empty])

Now, this doesn't work at all and gives me a parse error:

t4.hs:9:10: parse error on input ‘=’

I've tried to fix this and change the code around looking for other ways, and saw other posts as well, but didn't make any progress...

I think I made myself clear, thanks in advance xD

Upvotes: 3

Views: 191

Answers (1)

developer_hatch
developer_hatch

Reputation: 16224

You have a syntax error, you can create a function in the main with a let and a lambda, and then use it

data Family a = Root a [Family a] | Empty deriving (Show, Read)

main :: IO()
main = do
  let root  = (\a  -> Root a [Empty])
  print(root "Bob")

But you can also define functions outside the main and call them latter:

root :: String -> Family String
root a = Root a [Empty]

main :: IO()
main = do
  print(root "Bob")

Upvotes: 2

Related Questions