我是一个可以翻译文本的有用助手。
我正在解决婴儿积木问题。我有一段Java代码想要翻译成Haskell:
Java:
我正在解决婴儿积木问题。我有一段Java代码想要翻译成Haskell:
Java:
for (int i = 1; i <optHeight.length ; i++) {
int maxHeightIndex = 0;
for (int j = i-1; j >=0 ; j--) {
// Need help from here
if(boxes[j].width>boxes[i-1].width && boxes[j].depth>boxes[i-1].depth) {
if(optHeight[maxHeightIndex]<optHeight[j+1]) { <-- How do I write this condition
maxHeightIndex = j+1;
}
}
}
optHeight[i]=optHeight[maxHeightIndex] + boxes[i-1].height;
}
其中optHeight
是一个一维数组,boxes
是一个对象,包含height,width,depth
作为数据成员。在Haskell中,它只是一个列表的列表。由于缺乏可变数组/变量,我完全无助。
Haskell:
b list = do
forM_ [1..length list] $ \i -> do
let maxHeight = 0
forM_ [0..(i-1)] $ \j -> do
if list!!j!!1 > list!!i-1!!1 && list!!j!!2 > list !!j!!2 then
maxHeight = j + 1
PS: 我完全是Haskell新手