exercism / prolog

Exercism exercises in Prolog.
https://exercism.org/tracks/prolog
MIT License
29 stars 37 forks source link

improvement on complex-numbers tests #80

Closed Average-user closed 5 years ago

Average-user commented 5 years ago

While mentoring at exercism, i saw someone came up with a "solution" to complex-numbers that made absolutely no sense, but still passed all tests. I believe this was on purpose but he hasen't confirmed that yet.

This is the code he uploaded:

real((X,_), X).

imaginary((_, Y), Y).

add((X, _), (Y, _), (Z, _)) :-
    nonvar(X), nonvar(Y), !,
    Z is X + Y.

add((X, _), (Y, _), (Z, _)) :-
    nonvar(Y), nonvar(Z), !,
    X is Z - Y.

add((X, _), (Y, _), (Z, _)) :-
    nonvar(X), nonvar(Z),
    Y is Z - X.

sub((X, _), (Y, _), (Z, _)) :-
    nonvar(Y), !,
    Y1 is -Y,
    add((X, _), (Y1, _), (Z, _)).

sub((X, _), (Y, _), (Z, _)) :-
    Y is X - Z.

mul((X, _), (Y, _), (Z, _)).

div((X, _), (Y, _), (Z, _)).

abs((X, _), 5).

conjugate((X, _), (Y, _)).

This passes the tests because they only check if the program recognize valid solutions for add sub ... etc. But not if its able to find those solutions. And in case of abs, because it only has tests with 5 as a result.

I believe I've fixed both cases.