我创建了一个程序,它自动删除我在Lst2中指定的元素3,即元素3.现在,我想手动选择要输入的列表中要删除的元素。
-module(list).
-import(lists,[delete/2]).
-export([del/1]).
del([]) -> [];
del(N) ->
Lst1 = N,
Lst2 = delete(3,Lst1),
io:fwrite("~w~n",[Lst2]).
答案 0 :(得分:1)
调用del
函数时传入索引将起作用:
-module(list).
-import(lists,[delete/2]).
-export([del/2]).
del(_Index, []) -> [];
del(Index, List) ->
List2 = delete(Index,List),
io:fwrite("~w~n",[List2]).
或者,如果您想从用户输入获取索引,您可以从STDIN中读取:
-module(list).
-import(lists,[delete/2]).
-export([del/2]).
del([]) ->
io:format("Empty list. Nothing to remove.~n"),
[];
del(List) ->
{ok, [Index]} = io:fread("Index to remove : ", "~d"),
List2 = delete(Index,List),
io:fwrite("~w~n",[List2]).