我正在尝试像在Haskell中那样做:
mkList start end nb_chunck will output [start, boun1, bound2, bound3 ..., end]
但是我不希望将列表拆分为相等大小的块,而是遵循对数刻度。
我想在Haskell中转换的C算法在这里可用:How to get a logarithmic distribution from an interval
我真的不知道怎么做。
这是我到目前为止的尝试:
mkList :: Int -> Int -> Int -> Int -> Int -> [Int]
mkList _ _ _ _ 7 = []
mkList lower upper start end n = [lower, ((fromIntegral (log(2 + (fromIntegral n :: Int)) )+start) * scale)] ++ (mkList (fromIntegral(((fromIntegral (log(2 + (fromIntegral n :: Int)) )+start) * scale)+1) :: Int) ((fromIntegral (log(2 + (fromIntegral (n) :: Int)) )+start) * scale) (start) end (fromIntegral (n+1) :: Int)) where
scale = (end - start) `quot` floor(log(1 + (6)))
但是,我无法验证此代码,因为在编译时会弹出错误消息:
haskell_par3.hs:71:58:
No instance for (Floating Int) arising from a use of `log'
Possible fix: add an instance declaration for (Floating Int)
In the first argument of `fromIntegral', namely
`(log (2 + (fromIntegral n :: Int)))'
In the first argument of `(+)', namely
`fromIntegral (log (2 + (fromIntegral n :: Int)))'
In the first argument of `(*)', namely
`(fromIntegral (log (2 + (fromIntegral n :: Int))) + start)'
ghc -cpp: /usr/hs/ghc/7.6.3/bin/ghc failure (return code=1)
我试图在不同的地方使用fromIntegral
,但它没有帮助,正如StackOverflow上其他已回答的问题所示。
所以我要问两件事:
fromIntegral
有关,但我无法摆脱这个错误。)答案 0 :(得分:1)
您可以执行以下操作来创建日志缩放间隔
scale k n = map (floor . (*k) . (/(log n)) . log) [1..n]
e.g。
scale 100 9
[0,31,50,63,73,81,88,94,100]
并使用索引按开始/结束索引对数组进行分区。
您可以将输出转换为[Int],因为floor将其转换为Integral类型
Prelude> scale 10 3 :: [Int]
[0,6,10]
Prelude> :t it
it :: [Int]