我试图在Haskell mkStdGen中生成10个随机数,范围为0(含)到100(不包括).
等同于以下Java代码的东西
Random ran = new Random();
ran.nextInt(100);
Run Code Online (Sandbox Code Playgroud)
注意,我必须使用 mkStdGen
这就是我到目前为止所拥有的
rand low high seed = fst (randomR (low, high) (mkStdGen seed))
randomlist :: Int -> Int -> Int -> [Int]
randomlist l h num = take num (map (rand l h) [0..])
val deck = 1 to 52 toList
// shuffles the deck, and prints it out nicely
deck sortWith ((_, _) => Random.nextBoolean) foreach (x => print(x + " "))
由于我将一遍又一遍地洗牌和打印甲板,我正在努力做到以下几点
val deck = 1 to 52 toList
def shuffle : (List[Int] => List[Int]) = {_ sortWith ((_, _) => Random.nextBoolean)}
def printDeck : (List[Int] => Unit) = {_ foreach(x => print(x + " "))}
deck shuffle printDeck // this doesnt work
// I can only do
printDeck(shuffle(deck)) // this works …Run Code Online (Sandbox Code Playgroud) 所以我在GHCI做这个
Prelude> show (5, 6)
Run Code Online (Sandbox Code Playgroud)
会得到我
Prelude> "(5,6)"
Run Code Online (Sandbox Code Playgroud)
但是我想要打印出来(5,6)而不用逗号.所以我试过了
Prelude> show (5 6)
Run Code Online (Sandbox Code Playgroud)
而且我希望得到
Prelude> (5 6)
Run Code Online (Sandbox Code Playgroud)
但它让我失望:
No instance for (Num (a1 -> a0))
arising from the literal `5'
Possible fix: add an instance declaration for (Num (a1 -> a0))
In the expression: 5
In the first argument of `show', namely `(5 4)'
In the expression: show (5 4)
Run Code Online (Sandbox Code Playgroud) 所以我的表中有以下数据
id id2 flag
1 11 0 <- this row should not be part of the result
1 12 1 <- this row should survive the distinct operation
2 13 0
3 14 0
Run Code Online (Sandbox Code Playgroud)
我想要我的结果
id id2 flag
1 12 1
2 13 0
3 14 0
Run Code Online (Sandbox Code Playgroud)
我如何构建这样的查询?
谢谢
编辑1:抱歉,使用两列虚拟数据无法正确反映我面临的问题.我添加了另一列,这使问题复杂化.正如你所看到的,我无法对id2进行分组,因为它们都是唯一的.但是结果中应该省略id2 = 11的行.
编辑2:更改问题使用'省略'而不是'删除'
EDIT3:
select id, id2, max(flag)
from table
group by id, id2
Run Code Online (Sandbox Code Playgroud)
此查询返回所有4行,因为按ID2分组包括所有4行.