我有一个List,它可能包含比较相等的元素.我想要一个类似的List,但删除了一个元素.所以从(A,B,C,B,D)我希望能够"移除"一个B来得到例如(A,C,B,D).结果中元素的顺序无关紧要.
我有工作代码,在Scala中用Lisp启发的方式编写.有没有比较惯用的方法呢?
上下文是一种纸牌游戏,其中有两副标准牌正在进行中,因此可能存在重复的牌但仍然一次只能打一张牌.
def removeOne(c: Card, left: List[Card], right: List[Card]): List[Card] = {
if (Nil == right) {
return left
}
if (c == right.head) {
return left ::: right.tail
}
return removeOne(c, right.head :: left, right.tail)
}
def removeCard(c: Card, cards: List[Card]): List[Card] = {
return removeOne(c, Nil, cards)
}
Run Code Online (Sandbox Code Playgroud) 我输入的代码与Linux命令行相同:完整介绍,第369页,但提示错误:
line 7 `if[ -e "$FILE" ]; then`
Run Code Online (Sandbox Code Playgroud)
代码如下:
#!/bin/bash
#test file exists
FILE="1"
if[ -e "$FILE" ]; then
if[ -f "$FILE" ]; then
echo :"$FILE is a regular file"
fi
if[ -d "$FILE" ]; then
echo "$FILE is a directory"
fi
else
echo "$FILE does not exit"
exit 1
fi
exit
Run Code Online (Sandbox Code Playgroud)
我想知道是什么引入了这个错误?我该如何修改代码?我的系统是Ubuntu.
我有一个列表,可能包含比较相等的元素.我想要一个类似的列表,但删除了一个元素.所以从(:a:b:c:b:d)我希望能够"删除" 一个:b得到(:a:c:b:d).
上下文是纸牌游戏中的一手牌,其中有两副标准牌正在进行中,因此可能存在重复的牌但仍然一次玩一张.
我有工作代码,见下文.在Clojure中有更多惯用的方法吗?
(defn remove-one [c left right]
(if (= right ())
left
(if (= c (first right))
(concat (reverse left) (rest right))
(remove-one c (cons (first right) left) (rest right)))))
(defn remove-card [c cards]
(remove-one c () cards))
Run Code Online (Sandbox Code Playgroud)
以下是我不久前得到的Scala答案:什么是惯用的Scala方法从一个不可变的List中"删除"一个元素?