[m-dev.] some string manipulation funcs

David Glen JEFFERY dgj at fengshui.cs.mu.OZ.AU
Fri Nov 5 14:18:26 AEDT 1999


Hi,

Philip Dart forwarded these to me. Any complaints if I add them to the string
module?

----- Forwarded message from Philip Dart <philip at cs.mu.OZ.AU> -----

Date: Wed, 3 Nov 1999 12:26:16 +1100 (EST)
From: Philip Dart <philip at cs.mu.OZ.AU>

%-----------------------------------------------------------------------------%
% Copyright (C) 1995-1998 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%

% file: higher_order.m
% main author: philip

:- module higher_order.
:- interface.
:- import_module list, string.

:- func string_list_to_string(func(X) = string, string, string, string,
		list(X)) = string.
:- mode string_list_to_string(func(in) = out is det, in, in, in,
		in) = out is det.
%	string_list_to_string(F, Pre, In, Post, List).
%		Converts a list to a string using the function F to convert
%		each element to a string, separating each element by In,
%		and pre- and post-fixing the rsult with Pre and Post.  Eg.
%		F = (func(I) = O :- string__int_to_string),
%		string_list_to_string(F, "<", "; ", ">", [1,2,3]) = "<1; 2; 3>"

:- func string_list_to_string(func(X) = string, list(X)) = string.
:- mode string_list_to_string(func(in) = out is det, in) = out is det.
%	string_list_to_string(F, List).
%		Converts a list to a string equivalent to
%		string_list_to_string(F, "[", ", ", "]", List). Eg.
%		F = (func(I) = O :- string__int_to_string),
%		string_list_to_string(F, [1,2,3]) = "[1, 2, 3]"

:- implementation.

% string_list_to_string(F, Pre, In, Post, L) = S :-
% 	list__map((pred(I::in, O::out) is det :- O = F(I)), L, SL),
% 	list__length(SL, N),
% 	list__duplicate(N - 1, In, InL),
% 	list__append(InL, [Post], InL1),
% 	list__zip(SL, InL1, SL1),
% 	string__append_list([Pre|SL1], S).

string_list_to_string(F, Pre, In, Post, L) = S :-
	( L = [],
		S0 = Post
	; L = [H|T],
		P = (pred(I::in, O::out) is det :- string__append(In, F(I), O)),
		list__map(P, T, InL),
		list__append([F(H)|InL], [Post], InL1),
		string__append_list(InL1, S0)
	),
	string__append(Pre, S0, S).

% string_list_to_string(F, Pre, In, Post, L) = S :-
% 	( L = [],
% 		S2 = Post
% 	; L = [H|T],
% 		string_list_to_string(F, In, T, Post, S1),
% 		string__append(F(H), S1, S2)
% 	),
% 	string__append(Pre, S2, S).
% 
% :- pred string_list_to_string(func(X) = string, string, list(X),
% 	string, string).
% :- mode string_list_to_string(func(in) = out is det, in, in,
% 	in, out) is det.
% string_list_to_string(_F, _In, []) -->
% 	[].
% string_list_to_string(F, In, [H|T]) -->
% 	string_list_to_string(F, In, T),
% 	string__append(F(H)),
% 	string__append(In).

string_list_to_string(F, L) = string_list_to_string(F, "[", ", ", "]", L).

----- End forwarded message -----

-- 
David Jeffery (dgj at cs.mu.oz.au) | If your thesis is utterly vacuous
PhD student,                    | Use first-order predicate calculus.
Dept. of Comp. Sci. & Soft. Eng.|     With sufficient formality
The University of Melbourne     |     The sheerist banality
Australia                       | Will be hailed by the critics: "Miraculous!"
                                |     -- Anon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list