# First Homework Assignment

Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.

First Homework Assignment
The first exercise sheet is now online on studon.

Please upload your solutions by friday next week (Nov. 2.) via studon in one and only one of the four tutorials, and please stick with that tutorial for the rest of the semester, as to not cause confusion when we compute your full bonus points at the end of the semester If you need to switch, please let us know so we can keep track of your points properly.

If you can’t use studon (apparently there’s a problem with erasmus students), please pick one tutorial which suits your schedule best and send your solutions directly to you tutor (via email or whatever they prefer).

In general we strongly prefer pdf for text and plain source files for programming exercises (exceptions will be stated explicitly). In programming exercises, please document your code generously with comments, it will only make your tutor’s lives easier and hence more inclined to be generous

If you have any questions regarding the exercises (except for actual solutions, of course), don’t hesitate to ask here - usually you’re not the only one who would like to hear the answer

Is it fine to solve the permutations without the hint? The hint confuses me more than it helps.

Also: Any hints on how to hand in stuff on studon without getting the pesky .sec ending?

Regarding Question 1.2.3: What is a symmetrical binary tree? I couldn’t find any definition in the lecture notes.

Yes, as long as it written in Prolog and it works, you can solve the problem however you like. You just might get lost in the process, that’s why we gave the hint. But I totally agree that the way it is written may confuse you, so you may hear it with my words.

The helper function [m]takeout(X,LSA,LSB) [/m] might be helpful, as it takes the element X and a List LSB and „returns“ a List LSA, where X was put into the list. By re-evaluating the function (space bar or using the „;“), you should get a different LSA, meaning that X is at another location in the list.

swipl example:

``````?- takeout(1,X,[2,3,4]).
X = [1, 2, 3, 4] ;
X = [2, 1, 3, 4] ;
X = [2, 3, 1, 4] ;
X = [2, 3, 4, 1] ;
false.``````

(This also shows the „magic“ of Prolog, since the definition of the function „changes“ by changing the variable parameters. But what you should learn from here is, that the definition does not change, since both descriptions of [m]takeout[/m] are correct. Once you realize this, the only two things left you need to become good at Prolog is a good understanding of recursion and the will to learn Prolog’s syntax )

No, not really. What file format did you hand in?

1 Like

Nope, studon just does that. Nevermind that, the tutors will have to take care of that unfortunately.

If you google „symmetrical binary tree“, you will find two definitions; one being on the structure of the tree and one being on the values of the tree. Only one of them makes sense in the context of the exercise

1 Like

I have a general question regarding the prolog assignments. As far as I understood the exercise, we are not allowed to use any built-in functions. Is that correct? Or should we just not use the built-in functions that we are required to implement ourselves (i.e. “delete”, “reverse”) but can use others, such as “append”?

Let me put it like that: You are not supposed to use any pre-defined functions on lists. But of course you’ll need to pattern match lists, and I can’t reasonably expect you to reimplement a pattern matching mechanism (bonus points if you do xD )

One short question regarding the zip function:
if zip() gets lists that differ by more than 1, how should it react? Or is it irrelevant and simply not specified?

mfg

Since the assignment says:

I would assume, that any other case is irrelevant

I have a question regarding myPermutation: is this function supposed to output false at the end? It’s not on the sheet, but the permutation built-in also outputs false at the end

You are right.

[quote]
I have a question regarding myPermutation: is this function supposed to output false at the end?[/quote]

Yes, at least our solution does this

Should construct fail if the given list is not duplicate-free?
E.g. should construct([1, 2, 3], T) and construct([1, 1, 2, 3], T) both yield the same result T = t(1, nil, t(2, nil, t(3, nil, nil))) or should the second one say “false”?

This is Prologs default behaviour: For prolog, every query is „find a proof/substitution that makes the queried statement true“. If it finds such a substitution, it will return that. If it doesn’t it will return false. So basically any „function“ (i.e. predicate) should return false if and only if no other solutions for the free variables in a query remain.

By the above: Since you’re not supposed to implement functionality for this case, Prolog would have to return false by default.

pff, to be honest, I didn’t even consider that case, so either behavior is fine, since it’s unspecified by the framing of the problem

I guess we can assume that the given list is duplicate-free since the exercise sheet tells us it is a list of distinct numbers, right?

Yes, you can. I’ve interpreted the previous question as a sort-of fallback. What we care about is whether your predicate behaves as intended on „valid“ inputs only – I assume you know to throw exceptions in real-life situations