Is there a variation of number_codes/2 that can return a difference list instead of a closed list?
I found that number_codes/2 is implemented with C (src), but further digging lead to xchars (src) and while my C++ is quite rusty, I don’t see any option for a return type of a difference list.
Since I learned how to transform input using DCGs for parsing and difference list for reconstructing the transformed data, I am running into many predicates that would be nice if they returned difference list instead of closed list. One major example of this is all of the predicates in library DCG/basics that return closed list but would be nice if they also returned difference list.
However a hint of returning difference list was found with format/3 which uses with_output_to/2
I know the following simple predicate can easily transform any closed list into an open list, but this is just a band-aid that cost a performance penalty.
% Create open list and hole given closed list
open_closed_list(Open_list,Hole,Closed_list) :-
is_of_type(proper_list,Closed_list),
is_of_type(var,Open_list),
is_of_type(var,Hole),
open_closed_list_prime(Closed_list,Hole,Open_list),
must_be(partial_list,Open_list).
open_closed_list_prime([],Hole,Hole).
open_closed_list_prime([H0|T0],Hole,[H0|T]) :-
open_closed_list_prime(T0,Hole,T).
Personal Notes
References:
codesio.pl – I/O on Lists of Character Codes (src)
format.pl (Prolog src) (C src)
Wouter Beek has a nice implementation of DCG parsing using closed list and difference list.
See: digit*