reset password
Author Message
cgaldam2
Posts: 9
Posted 11:47 Nov 29, 2016 |

Abbott,

I am a little confused about a small section in Project 9.

In the section of code for the function bestWithStartEnd, in the the list comprehension you have the following check 'p1 >?< p2'. If this is satisfied then you apply the >+ function to the two paths and add it to the list. But I noticed that you then also check once again  'p1 >?< p2' in the >+ function. What is the purpose of this? If you already did it in the list comprehension, once you get to the >+ function wouldn't it already be true that  p1 >?< p2? 

rabbott
Posts: 1649
Posted 13:56 Nov 29, 2016 |

That's a reasonable question. There are a couple of answers.

Originally the test to see that the paths are compatible, that they satisfy the >?< condition, before joining them was only a comment in the  >+ function. I later changed it to make it part of the function.  It wasn't part of the function when I wrote bestWithStartEnd. So the test was necessary there. Putting that test in the >+ function is like testing to see if a list is not empty in the head function before trying to take head. It's additional protection against something going wrong. Other than that it's not functional. 

More to the point, the test in bestWithStartEnd is used to determine which paths should be joined. It's part of the logic of the list comprehension. The fact that the paths that are selected satisfy the >?< condition only ensures that they will pass that test when using the  >+ function. That guard could be removed from the  >+ function. The test for >?< could not be removed from the list comprehension. If it were we would attempt to join paths that cannot be joined. When we do that the program would abort. The test for >?< in the list comprehension causes the search to back up and look for another pair. We don't want to crash, just backtrack. 

Last edited by rabbott at 13:56 Nov 29, 2016.