简体   繁体   English

对 Haskell 数字类型感到困惑

[英]Confused about Haskell number types

I'm trying to create a foldr example that converts an Integral to a list of digits (ie, [Int] ).我正在尝试创建一个foldr示例,将Integral转换为数字列表(即[Int] )。

pickDigit num pos = (num `div` (10^pos)) `mod` 10

toDigits num = foldr (\pos acc -> (pickDigit num pos):acc) [] [0 .. floor (logBase 10 num)]

This loads successfully.这成功加载。 But when I try to run it, for example但是当我尝试运行它时,例如

> toDigits 1234

I get a type error message I don't understand.我收到一条我不明白的类型错误消息。

When I ask Haskell for the types of the loaded code and explicitly include those types in the source file, I get error messages on loading.当我向 Haskell 询问已加载代码的类型并在源文件中明确包含这些类型时,我会在加载时收到错误消息。 When I try to explicitly restrict the types to Integer or Int , I get more error messages.当我尝试将类型明确限制为IntegerInt ,我收到更多错误消息。

I'd appreciate some suggestions.我很感激一些建议。 Thanks.谢谢。

logBase requires Floating, but it's not logBase需要浮动,但它不是

This works:这有效:

[0 .. floor (logBase 10 $ fromIntegral num)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM