For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2006 > H=[K] ? (Re: Need help with flattening a list)









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 H=[K] ? (Re: Need help with flattening a list)
newser.bbs@bbs.ee.ncu.edu.tw

2006-05-08, 8:02 am

Vivek B wrote:

> app([],Y,Y).
> app([H|X],Y,[H|Z]) :- app(X,Y,Z).
>
> flat([],[]) :- !.
> flat([H|T],Y) :- flat(H,K), flat(T,L), append(K,L,Y),!.
> flat(H,[H]) :- not(H = [K]).


H=[K] ? what do you mean by that ?

> my_not(X) :- X,!,fail.


my_not should be not , but that is not an important point
of concept.

> not(_).


I am going to give some corrections to these codes.
Of course , your codes functions well , but it doesn't
has a clear sense for H=[K]. My codes on gprolog get
the followings :
--------------------------------------------------------------------------------
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?- [user].
compiling user for byte code...

not(X) :- X,!,fail.
not(_).

app([],Y,Y).
app([H|X],Y,[H|Z]) :- app(X,Y,Z).

flat([],[]) :- !.
flat([H|T],Y) :- !,flat(H,K), flat(T,L), append(K,L,Y).
flat(H,[H]).

user compiled, 11 lines read - 1410 bytes written, 3740 ms

(3790 ms) yes
| ?- flat([1,[2,[3,4],[5],6],[7,8]],L).

L = [1,2,3,4,5,6,7,8]

yes
| ?-
-------------------------------------------------------
Although your codes function well , it is not because
H = [K] gets i't function , but it is because the "shells"
(ie ['s and ]'s) have to be removed completely by the
second clause of flat before the last clause of flat is
evaluated ,

For illustration of the problem of H=[K] , just look at
the followings :
+++++++++++++++++++++++++++++++
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?- [user].
compiling user for byte code...
not(X) :- X,!,fail.
not(_).

user compiled, 3 lines read - 396 bytes written, 16370 ms

(16430 ms) yes
| ?- H is [[[1],2]],not(H=[K]).
uncaught exception: error(type_error(evaluable,'.'/2),(is)/2)
+++++++++++++++++++++++++++++++++++
See ? An error happens for the H=[K].

newser.bbs@bbs.ee.ncu.edu.tw

2006-05-08, 8:02 am

Another example is
--------------------------
| ?- not([[1],2]=[K]).

yes
--------------------------
See ? [[1],2] is obvious not atom ,
but the evaluation of that not predicate
says "yes".

Thus , the codes you use ( ie
not(H=[K])
) don't guarantee the H be atom.

If no such guarantee , what do you want
those codes for ?

Vivek B

2006-05-14, 7:03 pm


newser.bbs@bbs.ee.ncu.edu.tw wrote:
> Vivek B wrote:
>
>
> H=[K] ? what do you mean by that ?
>
>
> my_not should be not , but that is not an important point
> of concept.
>
>
> I am going to give some corrections to these codes.
> Of course , your codes functions well , but it doesn't
> has a clear sense for H=[K]. My codes on gprolog get
> the followings :
> --------------------------------------------------------------------------------
> GNU Prolog 1.2.16
> By Daniel Diaz
> Copyright (C) 1999-2002 Daniel Diaz
> | ?- [user].
> compiling user for byte code...
>
> not(X) :- X,!,fail.
> not(_).
>
> app([],Y,Y).
> app([H|X],Y,[H|Z]) :- app(X,Y,Z).
>
> flat([],[]) :- !.
> flat([H|T],Y) :- !,flat(H,K), flat(T,L), append(K,L,Y).
> flat(H,[H]).
>
> user compiled, 11 lines read - 1410 bytes written, 3740 ms
>
> (3790 ms) yes
> | ?- flat([1,[2,[3,4],[5],6],[7,8]],L).
>
> L = [1,2,3,4,5,6,7,8]
>
> yes
> | ?-
> -------------------------------------------------------
> Although your codes function well , it is not because
> H = [K] gets i't function , but it is because the "shells"
> (ie ['s and ]'s) have to be removed completely by the
> second clause of flat before the last clause of flat is
> evaluated ,
>
> For illustration of the problem of H=[K] , just look at
> the followings :
> +++++++++++++++++++++++++++++++
> GNU Prolog 1.2.16
> By Daniel Diaz
> Copyright (C) 1999-2002 Daniel Diaz
> | ?- [user].
> compiling user for byte code...
> not(X) :- X,!,fail.
> not(_).
>
> user compiled, 3 lines read - 396 bytes written, 16370 ms
>
> (16430 ms) yes
> | ?- H is [[[1],2]],not(H=[K]).
> uncaught exception: error(type_error(evaluable,'.'/2),(is)/2)
> +++++++++++++++++++++++++++++++++++
> See ? An error happens for the H=[K].


As I mentioned before I am a newbie to prolog and could make
mistakes...
I know that the second clause of flat removes the shells completly
before the last clause is been evaluated. But since I use append to
append the flattened head and tail to get the flattened list in case
the head is atom i need to change it to a list.

It is true that not([1,2] = [K]). is true even if [1,2] is not an atom.
But in this case since the shells of the first element is removed
completly by the second clause we would be having any problem.

This is my perception... You could rectify me...

Thank you newser

Vivek B

2006-05-14, 7:03 pm


newser.bbs@bbs.ee.ncu.edu.tw wrote:
> Vivek B wrote:
>
>
> H=[K] ? what do you mean by that ?
>
>
> my_not should be not , but that is not an important point
> of concept.
>
>
> I am going to give some corrections to these codes.
> Of course , your codes functions well , but it doesn't
> has a clear sense for H=[K]. My codes on gprolog get
> the followings :
> --------------------------------------------------------------------------------
> GNU Prolog 1.2.16
> By Daniel Diaz
> Copyright (C) 1999-2002 Daniel Diaz
> | ?- [user].
> compiling user for byte code...
>
> not(X) :- X,!,fail.
> not(_).
>
> app([],Y,Y).
> app([H|X],Y,[H|Z]) :- app(X,Y,Z).
>
> flat([],[]) :- !.
> flat([H|T],Y) :- !,flat(H,K), flat(T,L), append(K,L,Y).
> flat(H,[H]).
>
> user compiled, 11 lines read - 1410 bytes written, 3740 ms
>
> (3790 ms) yes
> | ?- flat([1,[2,[3,4],[5],6],[7,8]],L).
>
> L = [1,2,3,4,5,6,7,8]
>
> yes
> | ?-
> -------------------------------------------------------
> Although your codes function well , it is not because
> H = [K] gets i't function , but it is because the "shells"
> (ie ['s and ]'s) have to be removed completely by the
> second clause of flat before the last clause of flat is
> evaluated ,
>
> For illustration of the problem of H=[K] , just look at
> the followings :
> +++++++++++++++++++++++++++++++
> GNU Prolog 1.2.16
> By Daniel Diaz
> Copyright (C) 1999-2002 Daniel Diaz
> | ?- [user].
> compiling user for byte code...
> not(X) :- X,!,fail.
> not(_).
>
> user compiled, 3 lines read - 396 bytes written, 16370 ms
>
> (16430 ms) yes
> | ?- H is [[[1],2]],not(H=[K]).
> uncaught exception: error(type_error(evaluable,'.'/2),(is)/2)
> +++++++++++++++++++++++++++++++++++
> See ? An error happens for the H=[K].


As I mentioned before I am a newbie to prolog and could make
mistakes...
I know that the second clause of flat removes the shells completly
before the last clause is been evaluated. But since I use append to
append the flattened head and tail to get the flattened list in case
the head is atom i need to change it to a list.

It is true that not([1,2] = [K]). is true even if [1,2] is not an atom.
But in this case since the shells of the first element is removed
completly by the second clause, the third clause is going to have only
atoms to handle.

This is my perception... You could rectify me...

Thank you newser

Bart Demoen

2006-05-14, 7:03 pm

The troll of the year (newser.bbs@bbs.ee.ncu.edu.tw) wrote:

> | ?- H is [[[1],2]],not(H=[K]).
> uncaught exception: error(type_error(evaluable,'.'/2),(is)/2)
> +++++++++++++++++++++++++++++++++++
> See ? An error happens for the H=[K].
>


No, the error happens for your abuse of is/2: it says so clearly in the
error message. Reading error messages reduces some of your noise.
newser.bbs@bbs.ee.ncu.edu.tw

2006-05-15, 4:13 am


Vivek B wrote:

> It is true that not([1,2] = [K]). is true even if [1,2] is not an atom.
> But in this case since the shells of the first element is removed
> completly by the second clause, the third clause is going to have only
> atoms to handle.


And then, flat(H,[H]) is quite enough and no needs for H=[K] .

>
> This is my perception... You could rectify me...
>
> Thank you newser


In fact , I think your codes are the most elegant in this threat .
The problems I mentioned didn't cover it's glory.

newser.bbs@bbs.ee.ncu.edu.tw

2006-05-15, 4:13 am


Bart Demoen wrote:

> The troll of the year (newser.bbs@bbs.ee.ncu.edu.tw) wrote:

....
> No, the error happens for your abuse of is/2: it says so clearly in the
> error message. Reading error messages reduces some of your noise.


You are right . I abused is/2 .

In Turbo prolog , almost all the assignments are made by =/2 , thus
I have not even the slightest idea that I might abuse is/2. Yes , I saw
the error message , but that only made me think that H=[K] just caused
a problem , becaue I had not the slightest idea that a very simple
assignment can't be done by ISO prolog.

Now you tell me and I agree that I made a fool of myself .
But I still believe in the value of open discussion , thus I will
keep on discussing freely .

If I make a fool of myself again , please tell me , and I will thank
you
sincerely .

Nick Wedd

2006-05-16, 4:03 am

In message <1147683436.528751.189070@i39g2000cwa.googlegroups.com>,
newser.bbs@bbs.ee.ncu.edu.tw writes
>
>Bart Demoen wrote:
>
>...
>
>You are right . I abused is/2 .
>
>In Turbo prolog , almost all the assignments are made by =/2 , thus
>I have not even the slightest idea that I might abuse is/2. Yes , I saw
>the error message , but that only made me think that H=[K] just caused
>a problem , becaue I had not the slightest idea that a very simple
>assignment can't be done by ISO prolog.


No Prolog can do an assignment.

Prolog does not assign values to variables. It matches terms - if one
of the terms matched is a variable, it becomes equal in value to the
other.

>Now you tell me and I agree that I made a fool of myself .
>But I still believe in the value of open discussion , thus I will
>keep on discussing freely .
>
>If I make a fool of myself again , please tell me , and I will thank
>you
>sincerely .


You are not a fool, you are learning. It is easier to teach people who
admit their thoughts.

Nick
--
Nick Wedd nick@maproom.co.uk
Sponsored Links







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

Copyright 2008 codecomments.com