List 在Haskell中将列表拆分为非等宽块

List 在Haskell中将列表拆分为非等宽块,list,haskell,scale,logarithm,List,Haskell,Scale,Logarithm,我试着在哈斯克尔做一些类似的事情: mkList start end nb_chunck will output [start, boun1, bound2, bound3 ..., end] 但是,我不想将列表分成大小相等的块,而是遵循对数比例 我想在Haskell中转换的C算法在这里可用: 我真的不知道怎么做 以下是我迄今为止所做的尝试: mkList :: Int -> Int -> Int -> Int -> Int -> [Int] mkList _ _

我试着在哈斯克尔做一些类似的事情:

mkList start end nb_chunck will output [start, boun1, bound2, bound3 ..., end]
但是,我不想将列表分成大小相等的块,而是遵循对数比例

我想在Haskell中转换的C算法在这里可用:

我真的不知道怎么做

以下是我迄今为止所做的尝试:

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)
我试着在不同的地方使用
from integral
,但没有帮助,正如在StackOverflow的其他回答问题中所看到的那样

所以我要问两件事:

  • 你知道我怎样才能修复这些编译错误吗?(我知道它与integral的
    有关,但我无法摆脱这个错误)
  • 更重要的是:你认为这段代码可以实现我想要做的吗?如果没有,你有什么建议吗

您可以这样做来创建对数刻度间隔

scale k n = map (floor . (*k) . (/(log n)) . log) [1..n]
e、 g

并使用索引按开始/结束索引对数组进行分区

您可以将输出转换为[Int],因为floor会将其转换为整型

Prelude> scale 10 3 :: [Int]
[0,6,10]
Prelude> :t it
it :: [Int]

关于你的编译错误,你没有问过同样的问题吗?这可能是相关的。但是,我编辑了我的问题,表明我试图在代码中的不同位置使用
fromIntegral
来解决我的问题。但它不起作用。这就是我创建一个新问题的原因之一。
fromIntegral
log
的结果不起作用,
(fromIntegral n::Int)
根本不会更改类型(您的意思是
fromIntegral(n::Int)
。您可能应该阅读有关数字类型以及它们之间正确转换的教程。是的,但我在提问中看不到(其他人可能也看不到)在这个论坛上,我认为这是因为,这个问题被标记为这个论坛上的另一个已回答问题。
n
是一个
Int
,这是问题的一部分。非常感谢!你解决了我的主要问题
Prelude> scale 10 3 :: [Int]
[0,6,10]
Prelude> :t it
it :: [Int]