Getting an open list from a closed list

22 views
Skip to first unread message

Carey Phillips

unread,
Dec 18, 2016, 12:49:39 AM12/18/16
to swi-p...@googlegroups.com
I wanted something with this behavior:

closed_to_open_list( [a,b,c], X ).

X = [a, b, c|_22434] ;


This feels faintly ridiculous, but I couldn't find any easy way to do this. As much as anything, I don't know how to define an "empty open list", i.e. a list that is essentially just an anonymous tail. I can of course define a list with an anonymous tail that has at least one element as its head.

I ended up with this silly-looking thing that works (so long as Closed has at least one element):

closed_to_open_list( [X], [X|_] ).

closed_to_open_list( Closed, Open ) :-
    length( Closed, N ),
    N > 1,
    AllBut1 is N - 1,
    last( Closed, CLast ),
    firstNOf( AllBut1, Closed, CFirstList ),
    append( CFirstList, [CLast|_], Open ).

What am I missing here?

Kuniaki Mukai

unread,
Dec 18, 2016, 2:23:37 AM12/18/16
to Carey Phillips, <swi-prolog@googlegroups.com>
At a glance, your closed_to_open_list/2 sounds like
a variation of the append/3, though I may be
missing your point.

?- append([a,b,c,d], X, R).
R = [a, b, c, d|X].

Kuniaki Mukai
> --
> You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Carey Phillips

unread,
Dec 18, 2016, 2:08:25 PM12/18/16
to Kuniaki Mukai, <swi-prolog@googlegroups.com>
Ah...that's funny. I was so focused on providing an explicit representation of an open list that I overlooked the fact that (within a specific context) an unbound variable *is* essentially an empty open list.

Thanks for the gestalt shift moment.

On Sat, Dec 17, 2016 at 11:23 PM, Kuniaki Mukai <kuniak...@gmail.com> wrote:
At a glance, your closed_to_open_list/2 sounds like
a variation of the append/3, though I may be
missing your point.

?- append([a,b,c,d], X, R).
R = [a, b, c, d|X].

Kuniaki Mukai

> On Dec 18, 2016, at 14:49, Carey Phillips <careyph...@gmail.com> wrote:
>
> I wanted something with this behavior:
>
> closed_to_open_list( [a,b,c], X ).
>
> X = [a, b, c|_22434] ;
>
>
> This feels faintly ridiculous, but I couldn't find any easy way to do this. As much as anything, I don't know how to define an "empty open list", i.e. a list that is essentially just an anonymous tail. I can of course define a list with an anonymous tail that has at least one element as its head.
>
> I ended up with this silly-looking thing that works (so long as Closed has at least one element):
>
> closed_to_open_list( [X], [X|_] ).
>
> closed_to_open_list( Closed, Open ) :-
>     length( Closed, N ),
>     N > 1,
>     AllBut1 is N - 1,
>     last( Closed, CLast ),
>     firstNOf( AllBut1, Closed, CFirstList ),
>     append( CFirstList, [CLast|_], Open ).
>
> What am I missing here?
>
> --
> You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages