reset password
Author Message
AverageStudentX
Posts: 11
Posted 12:02 Nov 25, 2019 |

Has anyone in here figured out how to access the age field inside the student object that is inside the tree node object?

Just asking since I am currently working on part 4a and gave up figuring out how to do the above (4b).

I probably should have been working on this over the weekend. (If things go bad, I hope he drops this project.)

 

LHS
Posts: 26
Posted 12:14 Nov 25, 2019 |

Write a function that takes student and outputs that student's age.

Call that function every time you need to compare. 

eric436chan
Posts: 4
Posted 12:40 Nov 25, 2019 |

Haskell actually automatically creates the function for you when you create the data type Student

So if you have a student called x, all you would need to do is call age x and it will return the age of student x.

AverageStudentX
Posts: 11
Posted 12:56 Nov 25, 2019 |

Well I kinda don't understand how any of those works. Maybe I'm missing some connections. Or I am doing this wrong...

Let me make it a bit clear with what I have.

I know age is a function when I define Student as - data Student = Student { ............., age :: Int } ...

I was told that for #2 I am supposed to make the tree student node similar to the tree node definition that the prof. has in lec9 lines 251-253.

e.g. - data STree student = EmptyTree | Node student (STree student) ...

I already tried writing a function that takes a student. the problem is that I am passing a STree (it acts like a node), and the STree has the student object inside.

That's where I am stuck on.

eric436chan
Posts: 4
Posted 13:12 Nov 25, 2019 |

So you base your studentNode and studentTree off just the original models. A node would have a value(let’s say int) and then left and right nodes. Your studentNode would be the same as that where the value would just be a student instead. Same is done with the tree. Instead of just taking regular nodes, it’ll take studentNodes instead. Hope that makes things a bit more clear. And then when you search it’ll just be on the age of the student.

AverageStudentX
Posts: 11
Posted 13:18 Nov 25, 2019 |

Well I am still stuck on the problem how to get to get the age.

Like what do I do when I pass the STree object in?  age works if i past a student object in, but STree has the student object inside itself.

Unless maybe i should not define the tree node this way.

eric436chan
Posts: 4
Posted 13:43 Nov 25, 2019 |

So your studentNode would look something like a leftNode rightNode where a is the student. You can then just call age a and that will return the age of that student in the node.

AverageStudentX
Posts: 11
Posted 16:37 Nov 25, 2019 |

Man I didn't understand that last post. At this point I don't know if I should feel bad for wasting that person's time,

or feel bad for not understanding what that person is telling me. I'm still trying to figure out how write the function for getting the age

and I'm still running into function declaration errors here.

LHS
Posts: 26
Posted 17:16 Nov 25, 2019 |

He is saying you should be able to call age on the node element.

Like "age a" where "age" is the function, "a" is the input, and the out put of the function would be an (int). Where "left" is the left child node of"a", and "right" is the right child node of "a".

"a" is the element of (student tree node)- current node being compared.

"right" and "left" is the link to the children nodes. 

So the new student gets assigned to variable "x".

You compare (age x == age a) = "do something"

I made a function personally that specified each data value of student to a variable and returned the variable for age. I was sleep deprived when writing it though, so I can't remember why I made it. It may or may not help with type errors. Just remember if you want a function to return a specific data type explicitly state it. As opposed to letting Haskell guess.

 

AverageStudentX
Posts: 11
Posted 18:04 Nov 25, 2019 |

I ran into some one a while ago on campus after bugging a English prof. of mines.

The guy told me that for the declaration I'm supposed to put something like

getAge :; StdTree Student -> Int

getAge (Node s lst rst) = age s

That actually worked if I pass my definition of a tree node in, though I am curious to know why. I hope to run into him again

Anyways thanks for trying to help me here. Right now I am working on the the comparing and insertion part. I might post another topic on it later.

 

LHS
Posts: 26
Posted 19:48 Nov 25, 2019 |

Wasn't the homework due Thursday of last week? 

Or did we have more time?

The assignment is closed already for me. I already finished and turned it in, but I am curious about it.

AverageStudentX
Posts: 11
Posted 20:17 Nov 25, 2019 |

For my class it's due on the 27th. Running into that Spanish dude with the gray sweater was like a blessing to me. I thought I was totally screwed.

Prof. Sargent said that the only office hours he'll have after assigning the homework for us was last Thursday. And I already bugged him a lot for help then.

I am grateful for a few of my other professors who hold office hours this week til Wednesday. Still I think I should feel bad for not understanding anything in haskell

or anything in this assignment.