SML:从列表中删除条目

时间:2011-12-19 08:23:56

标签: list sml

如何删除列表elem中的元素L?如果列表不包含elem,则该函数应该返回列表不变。

例如:

L = [1, 3, 4, 0, 5, 7]    
elem = 5

到目前为止,我有以下功能:

fun removeElem elem myList[] = myList
  | removeElem (myList::tl) = if mem myList elem then
                                rm elem myList[]
                              else
                                removeElem elem tl

7 个答案:

答案 0 :(得分:3)

您可以回答问题并询问如何仅保留那些不等于elem的项目。这与filter

完全吻合
fun removeElem elem myList = filter (fn x => x <> elem) myList

答案 1 :(得分:0)

此代码将完成您要执行的操作:删除元素(实际上,如果有多个元素,它将删除元素的所有实例)并按原样返回列表的其余部分:

fun remove_element (list, element) =
    case list of
    [] => []
      | list_head::list_tail => let val a = remove_element(list_tail, element)
                       in
                           if list_head = element
                           then a
                           else list_head::a
                       end

答案 2 :(得分:0)

fun delete (s,[])     = []
  | delete (s,x::xs') = 
    if s = x then xs' (* more efficient than call delete function again *)
    else x::delete(s, xs') 

答案 3 :(得分:0)

fun remove_element (elemlist, elem) = 
  case elemlist of
  [] => []
  | head::tail => if elem = head
                  then remove_element (tail, elem)
                  else head::remove_element (tail, elem)

输出SML / NJ:

val remove_element = fn : ''a list * ''a -> ''a list
val it = () : unit
(* matching *)
- remove_element ([1,2,3,4,5], 4);
val it = [1,2,3,5] : int list
(* non matching *)
- remove_element ([1,2,3,4,5], 7);
val it = [1,2,3,4,5] : int list
(* multiple instances *)
- remove_element ([1,3,4,4,5],4);
val it = [1,3,5] : int list

答案 4 :(得分:0)

没有库或额外功能

fun remv(L, c) = 
  if null(L) then nil
  else if c=hd(L) then remv(tl(L), c)
  else hd(L)::remv(tl(L), c);

答案 5 :(得分:0)

您可以尝试使用此代码。

Widget build(BuildContext context) {
    return ScopedModelDescendant<ConnectedProductModel>(
      builder:
          (BuildContext context, Widget child, ConnectedProductModel model) {
        return Scaffold(
          drawer: Drawer(),
          appBar: AppBar(),
          body: Column(
            children: <Widget>[
              Container(
                child: RaisedButton(
                  onPressed: () => model.addProduct('NaNa',
                      'https://cdn.pixabay.com/photo/2019/01/27/22/31/girl-3959203__340.jpg'),
                  child: Text('Add Product'),
                ),
              ),
              Expanded(
                child: ListView.builder(
                  //build the cards
                  itemBuilder: _buildProductCard,
                  //lengta of the list,
                  itemCount: model.product.length,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildProductCard(BuildContext context, int index) {
      return Card(
        child: Column(
          children: <Widget>[
            Image.network(model.product[index].image),
            Text(model.product[index].address),
            ButtonBar(
              alignment: MainAxisAlignment.center,
              children: <Widget>[
                IconButton(
                  icon: Icon(Icons.location_on, color: Colors.blue, size: 40.0),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ProductNavigation()),
                    );
                  },
                ),
                IconButton(
                  icon: Icon(Icons.delete, color: Colors.blue, size: 40.0),
                  onPressed: () => model.deleteProduct(index),
                ),
                IconButton(
                  icon: Icon(Icons.info, color: Colors.blue, size: 40.0),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => ProductInfoPage(
                            model.product[index].address,
                            model.product[index].image,
                            model.product[index].id),
                      ),
                    );
                  },
                ),
                IconButton(
                  icon:
                      Icon(Icons.contact_mail, color: Colors.blue, size: 40.0),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => FormPage()),
                    );
                  },
                )
              ],

答案 6 :(得分:-2)

您也可以使用此功能从列表中删除重复的条目:

fun remove_duplicates(xs: ''a list) =
    let 
        fun helper(ds: ''a list, m: ''a) =
            if null ds 
            then []
            else if hd ds = m
            then helper(tl ds, m)
            else hd ds :: helper(tl ds, m)
    in 
        if null xs
        then []
        else hd xs :: remove_duplicates( helper(tl xs, hd xs) )
    end