Self-test 2 Solutions
|
Unification and lists 2 | ||
Do the following unify and (if so) what are the variable instantiations? | ||
Notice here that RH_Tail is given as instantiated to [Hd2|LH_Tail].
Notice here that although the left-hand list has three members, it
still unifies with the right-hand list (which seems to have four members)
because the tail of the list after the list constructor ("|") can be
instantiated to the empty list ("[]"). CommentaryUnification of lists is usually kept simple (as with: [a, b, c] = [Head|Tail]). However. examples with more than one head are a source of difficult to both novices and more experienced Prolog programmers. If you wish to use such kinds of examples in your programs, you can always use the Prolog interpreter to test the unifications you believe your program will handle.You should not be particularly discouraged if you only got half of the previous questions correct.
Describe what will happen if you attempt the following and why it will happen:
S = [a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a...What has happened is that the tail of the list has been instantiated to the whole of the list, which of course includes itself. A similar thing happens with the example: [Hd1, Hd2|Tail] = [Head|Tail] where Tail in the right-hand list is required to be instantiated with [Hd2|Tail]. The example: [Head, Tl] = [Head|Tl] is apparently more complex. However, it falls into the same kind of pattern, with the variable Tl from the left-hand list being unified with the tail of the second list. The tail of the list is that part after the list constructor ("|") and is always itself a list. So if the Prolog you are using gives any kind of response to such a goal, it would give: Tl = [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[...These examples look particularly horrid. They are given here to illustrate one of the reasons that Prolog programs sometimes fail to give the expected results. |
||