For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2004 > Problem with append function !!! help !!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with append function !!! help !!
harry270768

2004-05-12, 9:20 pm

hi guys i have the function :-
is_list([H|T]).

collect([],[]).
collect([A|B],RES):-
is_list(A) -> collect(B,RES);append(collect(B,RES),[A],RES).

what i am trying to do is for example i have a list
[1,2,[3,4]] then my new list should be [2,1]
that is i exlude the elements in the sublist.

the problem with this function is when i run it with
collect([1,2],X)?
it gives me an error saying 1st argument should be a list ( I think the
error is on append(collect(B,RES),[A],RES).
this line)

why is that ?????
any suggestions on how to make it work ?

bart demoen

2004-05-12, 9:20 pm

harry270768 wrote:
> hi guys i have the function :-
> is_list([H|T]).


Oh dear, you are in such trouble from the very start: Prolog
is NOT about functions, it is about relations.



> collect([],[]).
> collect([A|B],RES):-
> is_list(A) -> collect(B,RES);append(collect(B,RES),[A],RES).
>
> what i am trying to do is for example i have a list
> [1,2,[3,4]] then my new list should be [2,1]
> that is i exlude the elements in the sublist.
>
> the problem with this function is when i run it with
> collect([1,2],X)?
> it gives me an error saying 1st argument should be a list ( I think the
> error is on append(collect(B,RES),[A],RES).
> this line)
>
> why is that ?????
> any suggestions on how to make it work ?
>


To quote Anthony Hopkins in Zorro: "this is going to be more difficult
than I thought" ...

1) The error message is quite explicit: it says that the first argument
of append (you guessed that one right) should be a list - you don't
see that in your program it is not a list, right ?
That's because you think Prolog is about functions, while it is not.

When you write something like collect(B,RES) as an argument, for Prolog
this means a term whose principal functor has name collect and with
arity 2, and two arguments ...

Just to give you a simple example - suppose I want to define a relation
foo/2 that holds only between the numbers 2 and 16, then I would write
in my program the fact

foo(2,16).

If at some point in the program, I have a value - say X - and want to
give as an argument to a predicate gee/1 a value that is in relation foo
to X, I must write:

foo(X,Y), gee(Y)

Something that looks like gee(foo(X)) would have a completely different
meaning to Prolog.


2) just quoting you again:
> what i am trying to do is for example i have a list
> [1,2,[3,4]] then my new list should be [2,1]
> that is i exlude the elements in the sublist.


in [1,2,[3,4]] the list [3,4] is not a sublist: it is an element

It is important to learn the terminology of lists.

Which Prolog are you using ? (asking because the Prologs I have here,
don't care about the first argument of append/3 being a list.

Cheers

Bart Demoen

harry270768

2004-05-12, 9:20 pm

Hi bart
Thanx for the reply man

sorry i didnt mean sublist but what i meant was like
in the list [1,2,[2]] i should only make another list as
[1,2].

i am using iprolog(8 April 2001)
can you tell me how to fix the problem ???

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com