Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用递归删除haskell中列表的最大值_Haskell_Recursion - Fatal编程技术网

使用递归删除haskell中列表的最大值

使用递归删除haskell中列表的最大值,haskell,recursion,Haskell,Recursion,我不熟悉函数式编程。我想实现一个从int列表中删除max元素的方法,或者如果存在max元素的重复项,只需使用递归删除其中一个即可。例如,[2,1,4,4,3]应该变成[2,1,4,3]。不排序列表。findMax函数计算列表的最大元素,removeMax删除第一个出现的最大元素。你可以把它们粘在一起来计算你要找的东西 findMax :: [Int] -> Int -> Int findMax [] acc = acc findMax (h : rest) acc | h >

我不熟悉函数式编程。我想实现一个从int列表中删除max元素的方法,或者如果存在max元素的重复项,只需使用递归删除其中一个即可。例如,[2,1,4,4,3]应该变成[2,1,4,3]。不排序列表。

findMax函数计算列表的最大元素,removeMax删除第一个出现的最大元素。你可以把它们粘在一起来计算你要找的东西

findMax :: [Int] -> Int -> Int
findMax [] acc = acc
findMax (h : rest) acc
  | h > acc = findMax rest h
  | otherwise = findMax rest acc


removeMax :: [Int] -> Int -> [Int]
removeMax []  mx = []
removeMax (h : rest) mx
  | h == mx = rest
  | otherwise = h : removeMax rest mx  

λ> findMax [2, 1, 4, 4, 3] 0
4
λ> removeMax [2, 1, 4, 4, 3] 4
[2,1,4,3]
λ> 

StackOverflow不是代码编写服务。请试着写一个解决方案,然后问一个问题,如果你遇到任何问题。目前,这不是一个问题。这看起来也像是家庭作业。为什么不分两步进行:第一步,找到最大值,第二步,修改列表?应为O(n)
deleteMax xs=delete(max xs)xs