From d259285459176fc281193adb12483a6e6d527cf5 Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Tue, 6 Sep 2016 16:48:08 +0200 Subject: English translation for sum/2 added. --- prolog/problems/lists_advanced/sum_2/en.py | 87 +++++++++++++++++++++++++++++- prolog/problems/lists_advanced/sum_2/sl.py | 6 +-- 2 files changed, 88 insertions(+), 5 deletions(-) (limited to 'prolog/problems/lists_advanced/sum_2') diff --git a/prolog/problems/lists_advanced/sum_2/en.py b/prolog/problems/lists_advanced/sum_2/en.py index 767a6f4..3abc348 100644 --- a/prolog/problems/lists_advanced/sum_2/en.py +++ b/prolog/problems/lists_advanced/sum_2/en.py @@ -2,10 +2,93 @@ name = 'sum/2' slug = 'find the sum of all elements in list' description = '''\ -

sum(L, Sum): Sum is the sum of all elements in the list L.

+

sum(L, Sum): Sum is the sum of all elements in list L.

 ?- sum([1,2,3], Sum).
   Sum = 6.
 
''' -hint = {} +plan = ['''\ +

The sum of an empty list is really not all that large. And if it's not empty, then we add it up, +element by element, recursively.

+''', '''\ +

If the sum of the elements in the tail (list without a head) equals SumT, then the sum +of the elements in the whole list equals SumT + H.

+''', '''\ +

If I take away the head, and the recursion solves this smaller problem (tail), and if I add the value +of the head to the result returned by the recursion, then I get the sum of the whole list.

+'''] + +hint = { + 'eq_instead_of_equ': '''\ +

The operator == is "stricter" than operator = in the sense that +for the latter it is enough to be able to make the two operands equal (unification). Perhaps by using = +you can make the predicate sum/2 more general (e.g. able to work with output arguments becoming inputs).

+

Of course, you can also solve the exercise without explicit use of either of these two operators, just +remember that unification is implicitly performed with the predicate's arguments (head of clause).

+''', + + 'eq_instead_of_equ_markup': '''\ +

Perhaps the operator for unification (=) would be better?

+''', + + 'base_case': '''\ +

Did you think of a base case? What's the sum of the elements in an empty list?

+''', + + 'recursive_case': '''\ +

The base case is ok. However, what about the general recursive case?

+''', + + 'predicate_always_false': '''\ +

It seems your predicate is always "false". Did you give it the correct name, +or is it perhaps misspelled?

+

If the name is correct, check whether something else is misspelled, perhaps there is a full stop instead of +a comma or vice versa, or maybe you typed a variable name in lowercase?

+

It is, of course, also possible that your conditions are too restrictive, or even impossible to satisfy +(as would be, for example, the condition that N is equal to N + 1, +or something similarly impossible).

+''', + + 'timeout': '''\ +

Is there an infinite recursion at work here? How will it ever stop?

+

Or perhaps is there a missing, faulty, or simply incompatible (with the general recursive case) base case?

+''', + + 'arbitrary_base_case': '''\ +

What's the sum of the elements in an empty list? Give a number!

+''', + + 'args_not_instantiated': '''\ +

The error that prolog reported means that when it encountered an arithmetic operation, not all the +values were known. Unfortunately, the ordering of goals is important when dealing with arithmetics.

+

Perhaps you can try moving the arithmetic operation more towards the end of the predicate?

+''', + + '=_instead_of_is': '''\ +

Did you use the operator = instead of is? Operator = is used for +unification and tries to leave both its operands with as little modifications as possible while still making +them equal. Operator is, on the other hand, performs actual arithmetic evaluation of its +second operand and only then attempts the unification of both operands.

+''', + + '+1_instead_of_+H': '''\ +

Did you really add one instead of the element's value? Copy/paste operation from the previous exercise? ;)

+''', + + 'forcing_result_onto_recursion': ''' +

Don't force the result onto recursion, don't tell it what it should return. Just assume it will do its job. +If this assumption is correct, then the rule will work for a larger case.

+

Is your recursive call of the form sum(Tail, SumTail + H)? This forces the recursive call to +return the sum of the whole list, not just the tail! This will not work. It is your job to +increase (by value of the head) the result returned by the recursion. In short, perform the addition +outside the recursive call.

+''', + + 'same_var_on_both_sides_of_is': '''\ +

Does one of your goals look similar to N is N + 1? Let's assume N is equal to 3. +With this goal you just stated that 3 must be equal to 4 (3+1). Prolog is a logical language and will +gladly say "false" to such a statement! Just use a new variable. The garbage collector will take care of +those not needed anymore automatically.

+''', +} diff --git a/prolog/problems/lists_advanced/sum_2/sl.py b/prolog/problems/lists_advanced/sum_2/sl.py index 539271b..d3bddcf 100644 --- a/prolog/problems/lists_advanced/sum_2/sl.py +++ b/prolog/problems/lists_advanced/sum_2/sl.py @@ -22,7 +22,7 @@ hint = { 'eq_instead_of_equ': '''\

Operator == je strožji od operatorja = v smislu, da je za slednjega dovolj, da elementa lahko naredi enaka (unifikacija). Morda z uporabo = narediš predikat -memb/2 delujoč tudi v kakšni drugi smeri.

+sum/2 delujoč tudi v kakšni drugi smeri.

Seveda pa lahko nalogo rešiš brez obeh omenjenih operatorjev, spomni se, da lahko unifikacijo narediš implicitno že kar v argumentih predikata (glavi stavka).

''', @@ -76,8 +76,8 @@ svoji levi strani.

'forcing_result_onto_recursion': '''

Ne vsiljuj rekurziji kaj naj vrne, prepusti se ji. To je tisti del, ko narediš predpostavko, če je ta izpolnjena, potem bo tvoje pravilo delovalo za večji primer.

-

Je tvoj rekurzivni klic oblike len(T, LenT + H)? S tem vsiljuješ rekurziji -da mora vrniti vsoto celega seznama in ne samo repa. To ni v redu, za vrednost glave moraš ti povečati +

Je tvoj rekurzivni klic oblike sum(Tail, SumTail + H)? S tem vsiljuješ rekurziji +da mora vrniti vsoto celega seznama in ne samo repa. To ni v redu, za vrednost glave moraš ti povečati rezultat, ki ti ga rekurzija vrne. Skratka, prištevanje naredi izven rekurzivnega klica.

''', -- cgit v1.2.1