[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: list transformers and Prolog HOF type behaviours...
From: |
Paulo Moura |
Subject: |
Re: list transformers and Prolog HOF type behaviours... |
Date: |
Mon, 16 Dec 2013 18:40:26 +0000 |
On 16/12/2013, at 17:24, Lindsey Spratt <address@hidden> wrote:
> Sean,
> Here's a solution:
>
> map(_Map, [], []).
> map(Map, [HIN|TIN], [HOUT|TOUT]) :-
> Map =..Terms,
> append(Terms, [HIN,HOUT], TermsExtended),
> MapExtended =.. TermsExtended,
> call(MapExtended),
> map(Map, TIN, TOUT).
The code can be simplified by using the built-in and standard call/N control
construct:
map(_Map, [], []).
map(Map, [HIN|TIN], [HOUT|TOUT]) :-
call(Map, HIN, HOUT),
map(Map, TIN, TOUT).
Cheers,
Paulo
> 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.
>
> Lindsey
>
> 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],
>> call(Xform),
>> 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"..is 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:
>>
>> lower_upper
>>
>> and then in a cool world using
>>
>> lower_upper(X,_)
>> or
>> lower_upper(_,X)
>>
>> instead and "it" would know to replace "_" with the input parameter...I am
>> going to see what I can come up with!
>> Thanks,
>> Sean.
>>
>>
>>
>> _______________________________________________
>> Users-prolog mailing list
>> address@hidden
>> https://lists.gnu.org/mailman/listinfo/users-prolog
>
> _______________________________________________
> Users-prolog mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/users-prolog
-----------------------------------------------------------------
Paulo Moura
Logtalk developer
Email: <mailto:address@hidden>
Web: <http://logtalk.org/>
-----------------------------------------------------------------