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
List 使用Haskell计算给定高低范围内的数字_List_Haskell_Range - Fatal编程技术网

List 使用Haskell计算给定高低范围内的数字

List 使用Haskell计算给定高低范围内的数字,list,haskell,range,List,Haskell,Range,我试图计算输入列表中有多少数字在给定的高低范围内 以下是我目前掌握的情况: countRange :: Int -> Int -> [Int] -> Int countRange _ _ [] = 0 countRange low high (x:xs) | (low < x) && (high > x) = 1 + (countRange low high xs) | otherwise

我试图计算输入列表中有多少数字在给定的高低范围内

以下是我目前掌握的情况:

countRange :: Int -> Int -> [Int] -> Int
countRange  _ _ [] = 0
countRange low high (x:xs)  | (low < x) && (high > x) = 1 + (countRange low high xs)
                            | otherwise = countRange low high xs
一些例子:

计数范围5 8[]=>0 计数范围13[1,2,3,4,5]=>3
我认为这更像是一个语义上的讨论。如果我们假设这些范围是独占的,那么您的代码工作正常。但您的示例表明边界是包含的,因此我们可以将其改写为:

countRange :: Int -> Int -> [Int] -> Int
countRange  _ _ [] = 0
countRange low high (x:xs)  | low <= x && high >= x = 1 + countRange low high xs
                            | otherwise = countRange low high xs
请注意,我们无需任何额外的努力即可概括函数签名:

countRange :: Ord a => a -> a -> [a] -> Int
countRange low hig = length . filter f
    where f x = x >= low && x <= hig

我认为这更像是一个语义上的讨论。如果我们假设这些范围是独占的,那么您的代码工作正常。但您的示例表明边界是包含的,因此我们可以将其改写为:

countRange :: Int -> Int -> [Int] -> Int
countRange  _ _ [] = 0
countRange low high (x:xs)  | low <= x && high >= x = 1 + countRange low high xs
                            | otherwise = countRange low high xs
请注意,我们无需任何额外的努力即可概括函数签名:

countRange :: Ord a => a -> a -> [a] -> Int
countRange low hig = length . filter f
    where f x = x >= low && x <= hig

这里的边界不应该包含吗?这里的边界不应该包含吗?