Newbe help : Why am I getting this output ?!? 
Author Message
 Newbe help : Why am I getting this output ?!?

Yes, this IS a homework assignment BUT I do not need anyone to solve
it for me - (already have...) just to explain the strange output I'm
getting :
The program demands are simple : I have to build a function to compute
the following : (for example) :
?- subsum([1,2,5,3,2],Sub,5).
Sub = [1,2,2];,2,2];
Sub = [2,3];
Sub = [5];
Sub = [3,2];2];
no

Sub being all subgroups of List (first parameter), the sum of which
equal Num (third parameter). I built this program :
subsum([Sub|List],Sub,Num):-
        Num=:=Sub.
subsum([X|List],[X|Sub],Num):-
        X=<Num,
        subsum(List,Sub,Num-X).
subsum([X|List],Sub,Num):-
        subsum(List,Sub,Num).
It works BUT (and heres my problem), the output it gives me is in the
form of :
Sub = [1 ,2|2];
Sub = [2|3];
Sub = 5;
Sub = [3|2];
no
this being true but ugly (the bars...). I am using SWI-prolog and
cant figure out what I'm doing wrong. Please help as this is a
genuine interest I have in the matter and no longer a grade problem :)



Sun, 02 May 1999 03:00:00 GMT  
 Newbe help : Why am I getting this output ?!?

Quote:

>Sub being all subgroups of List (first parameter), the sum of which
>equal Num (third parameter). I built this program :
>subsum([Sub|List],Sub,Num):-
>    Num=:=Sub.
>subsum([X|List],[X|Sub],Num):-
>    X=<Num,
>    subsum(List,Sub,Num-X).
>subsum([X|List],Sub,Num):-
>    subsum(List,Sub,Num).
>It works BUT (and heres my problem), the output it gives me is in the
>form of :
>Sub = [1 ,2|2];
>Sub = [2|3];
>Sub = 5;
>Sub = [3|2];
>no

The program contains a type error, which would be fixed if the first
line was changed to:

subsum([Sub|List], [Sub], Num) :-
...

(although the variable name "Sub" is a bit misleading).

Explanation:

The first two arguments are lists of integers, and the third argument is
an integer.  In the second clause you can figure out the type of each
variable by inspection:

Quote:
>subsum([X|List],[X|Sub],Num):-

        % [X|List] is of type list(int)
        %       therefore X is of type int
        %       and List is of type list(int)
        % [X|Sub] is of type list(int)
        %       therefore X is of type int
        %       and Sub is of type list(int)
        % Num is of type int
Quote:
>    X=<Num,

        % X and Num must be the same type
Quote:
>    subsum(List,Sub,Num-X).

        % List is of type list(int)
        % Sub is of type list(int)
        % Num-X evaluates to an int
        %       therefore X is of type int
        %       and Num is of type int

Note that throughout the entire clause all of the statements about type
are consistent.  Now consider the first clause:

Quote:
>subsum([Sub|List],Sub,Num):-

        % [Sub|List] is of type list(int)
        %       therefore Sub is of type int
        %       and List is of type list(int)
        % Sub is of type list(int)
        % Num is of type int
Quote:
>    Num=:=Sub.

        % Num and List have the same type

And here we discover a problem with the type of Sub.

This technique does not point out what the error is, but can quite often
isolate where there may be a problem.  If you analyse the first clause
with the head replaced as above, then you shouldn't get any type
problems (NB: this does not mean the program has _no_ errors -- you
should still test it as usual).

Quote:
>this being true but ugly (the bars...). I am using SWI-prolog and
>cant figure out what I'm doing wrong. Please help as this is a
>genuine interest I have in the matter and no longer a grade problem :)

The output was what gave me a clue that it might be a type error.  The
program uses ints and list(int)s, but the output:

Quote:
>Sub = [1 ,2|2];

is not even a list (the term would need to be [1, 2 | [2]], which is
equivalent to [1, 2, 2]).

Considering that you are genuinely interested (and good on you!) you
could take a look at the Mercury home page (from memory it is
http://www.cs.mu.oz.au/mercury/).  The Mercury system can find type
errors at compile time for you.

Cheers,
Mark.

--
---------------------------------------
My  Karma  just  ran  over  your  Dogma
---------------------------------------



Sun, 02 May 1999 03:00:00 GMT  
 Newbe help : Why am I getting this output ?!?

Quote:

> [...]
> Sub being all subgroups of List (first parameter), the sum of which
> equal Num (third parameter). I built this program :
> subsum([Sub|List],Sub,Num):-
>    Num=:=Sub.
> subsum([X|List],[X|Sub],Num):-
>    X=<Num,
>    subsum(List,Sub,Num-X).
> subsum([X|List],Sub,Num):-
>    subsum(List,Sub,Num).
> It works BUT (and here's my problem), the output it gives me is in the
> form of :
> Sub = [1 ,2|2];
> Sub = [2|3];
> Sub = 5;
> Sub = [3|2];
> no
> this being true but ugly (the bars...). I am using SWI-prolog and
> can't figure out what I'm doing wrong. Please help as this is a
> genuine interest I have in the matter and no longer a grade problem :)

The source of the problem is found in the first clause:

Quote:
> subsum([Sub|List],Sub,Num):-
>    Num=:=Sub.

You want Sub to be a list of all numbers that adds up to Num, but here
Sub is just a number, not a list of one number.  What you would have
wanted is something like

subsum([X|List],[X],Num):-
        Num=:=X.

Quote:
> Sub = [1 ,2|2];

This means that the last tail of this list is not the empty list, but
the integer 2.

/Andreas



Sun, 02 May 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Why am I getting bind errors?

2. ERROR 48 - Why am I getting it?

3. why am i getting processor stack fault error?

4. Why am I getting a NaN?

5. The Smalltalk Store: Why we've been slow, and why we're getting better

6. Into what am I getting?

7. I am getting THISTHREADACTIVE error (lots of them)

8. I Am Getting Fed Up With Clarion

9. how to compile and a set of .prg file I am getting this warning

10. I am getting timeouts trying to access parallel port with VISA

11. A fatal error i am getting in CosmoWorlds.

12. output buffering--am I missing something obvious?

 

 
Powered by phpBB® Forum Software