Getting lists from ASCII input

Matthew McDonnell matt@REDACTED
Tue May 16 10:04:45 CEST 2006


On Mon, 15 May 2006, Deryk Barker wrote:

> I'm trying to write a program (a client - my students will write the
> server) which needs to allow the user to enter a list in repsonse to a
> prompt.
>
> I cannot quite figure out what combination of list_to_atom and
> atom_to_list I need to do this.
>
> e.g. if the user enter:
>
> 	remove [a,b,c]
>
> I'd like to be able to get the remove as an atom and the [a,b,c] as a
> list of three atoms. Currently, I can get the list

io:scan_erl_exprs does just about what you want:

1> io:scan_erl_exprs('enter text>').
enter text>remove [a,b,c].
{ok,[{atom,1,remove},
     {'[',1},
     {atom,1,a},
     {',',1},
     {atom,1,b},
     {',',1},
     {atom,1,c},
     {']',1},
     {dot,1}],
    2}

Something like the following maybe? (untested, probably typos):

parse() ->
  {ok, [ActionAtom, {'[',_}, AtomList], _Num} = io:scan_erl_exprs(),
  List = accumulate(AtomList,[])
  {ActionAtom, List}.

accumulate([{atom, _, At} | Lst], Acc) ->
  accumulate(Lst, [At | Acc]);
accumulate([{']',_} | ], Acc) ->
  lists:reverse(Acc).

Cheers,
	Matt

Matt McDonnell
Email: matt@REDACTED
Web:   http://www.matt-mcdonnell.com/



More information about the erlang-questions mailing list