[mercury-users] Perl split, assign, retrieve in Mercury

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Jul 14 14:17:48 AEST 2001


On 13-Jul-2001, Terrence Brannon <tmbranno at oracle.com> wrote:
> 
> A very common and easy thing to do in Perl is to read a file and split
> each line on a delimiter and assign each element of the line to a
> member of a hash and then obtain some member of the hash for
> processing of some sort. A typical Perl program for this task is
> included below. How would this be done in Mercury?
> 
> while (<DATA>) {
> 
>     @split{letters, numbers, junk} = split /:/;
>     print $split{numbers};
> }
> 
> __DATA__
> abc:123:@#$#@
> def:456:@!#@
> hij:789:%$%$

It's easy to write a library function that splits a list at a certain element:

	:- import_module list.

	:- func split_at(list(T), T) = list(list(T)).
	split_at(List, Separator) = split_at_2(List, Separator, []).

	split_at_2([], _, []) = [].
	split_at_2([], _, [Y|Ys]) = [list__reverse([Y|Ys])].
	split_at_2([X|Xs], S, Ys) =
		(if X = S then
			[list__reverse(Ys) | split_at(Xs, S)]
		else	
			split_at_2(Xs, S, [X|Ys])
		).

Then you can easily write a loop that reads in a line,
splits it, at the delimiter, and prints the results:

	:- import_module io, string, list, char, exception.

	main -->
		io__read_line(Result),
		( { Result = ok(Line) },
			( { [_Letters, Numbers | _Junk] = split_at(Line,':') } ->
				io__write_string(string__from_char_list(Numbers)),
				nl
			;
				{ throw("syntax error in input") }
			),
			main
		; { Result = eof }
		; { Result = error(Error) },
			{ throw(Error) }
		).

Alternatively, you can define a library procedure to read in a list of lines:

	:- type line == list(char).
	:- pred read_lines(list(line)::out, io__state::di, io__state::uo) is det.

	read_lines(Lines) -->
		io__read_line(Result),
		( { Result = ok(Line) },
			read_lines(Lines1),
			{ Lines = [Line | Lines1] }
		; { Result = eof },
			{ Lines = [] }
		; { Result = error(Error) },
			{ throw(Error) }
		).

Then main can be defined a bit more simply:

	main -->
		read_lines(Lines),
		list__foldl(process_line, Lines).

	:- mode process_line(in, di, uo) is det.
	process_line(Line) -->
		( { split_at(Line,':') = [_Letters, Numbers | _Junk] } ->
			io__write_string(string__from_char_list(Numbers)), nl
		;
			{ throw("syntax error in input") }
		).

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list