[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: list transformers and Prolog HOF type behaviours...

From: Lindsey Spratt
Subject: Re: list transformers and Prolog HOF type behaviours...
Date: Mon, 16 Dec 2013 12:24:28 -0500

Here's a solution:

map(_Map, [], []).
map(Map, [HIN|TIN], [HOUT|TOUT]) :-
Map =..Terms,
append(Terms, [HIN,HOUT], TermsExtended),
MapExtended =.. TermsExtended,
map(Map, TIN, TOUT).

This works 'both ways':
map(lower_upper, [a,b], U) gives U=['A','B']
map(lower_upper, L, ['A','B']) gives L=[a,b].

The 'Map' can be any term where it is meaningful to append two arguments to the end of the term and evaluate the extended term.


On Dec 16, 2013, at 11:41 AM, emacstheviking <address@hidden> wrote:

I decided to write a maplist type predicate that will be used to map a function over a list and return the list of transformed values. I have wanted this for a while now and I sat down and came up with this. It works BUT I am not happy with it for reasons outlined below so any suggestions etc. are welcome.

maplist2(In, X, Out) :-
transform_list(In, X, [], Out).

transform_list([], _, Acc, Final) :-
reverse(Acc, Final).

transform_list([E|Es], Xfn, Acc, Final) :-
Xform =.. [Xfn, E, Out],
transform_list(Es, Xfn, [Out|Acc], Final).

For example, if I wanted to convert a string from lower case to upper case I would have to use a list of atoms to satisfy lower_upper/2, that's not an issue. The assumption for my maplist2 is that the transformer function has arity 2 and the first paramter is the input and the second is the output.

What is the issue though is that lower_upper/2 works either way around and if I wanted to convert a->'A' or 'A'->a then how would that work?

Haskell has sections and "flip" there a Prolog way to say that I want to change the order of evaluation of a function if you catch my drift?

Thinking on my feet here, it would be the difference between currently passing in:


and then in a cool world using


instead and "it" would know to replace "_" with the input parameter...I am going to see what I can come up with!

Users-prolog mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]