gpt4 book ai didi

haskell - 将 float 转换为整数

转载 作者:行者123 更新时间:2023-12-02 05:58:03 25 4
gpt4 key购买 nike

我正在尝试定义:

square :: Integer -> Integer 
square = round . (** 2)

我得到:

<interactive>:9:9: error:
• No instance for (RealFrac Integer) arising from a use of ‘round’
• In the first argument of ‘(.)’, namely ‘round’
In the expression: round . (** 2)
In an equation for ‘square’: square = round . (** 2)

<interactive>:9:18: error:
• No instance for (Floating Integer)
arising from an operator section
• In the second argument of ‘(.)’, namely ‘(** 2)’
In the expression: round . (** 2)
In an equation for ‘square’: square = round . (** 2)

我还是这门语言的新手,我似乎无法将 Floating 的实例转换为 Integer。有人知道我该怎么做吗?

最佳答案

Haskell 有多个不同类型的指数函数:

您要找的就是(^)。有了它,你甚至不需要round:

square :: Integer -> Integer 
square = (^ 2)

关于haskell - 将 float 转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com