gpt4 book ai didi

haskell - 导入时如何隐藏运算符?

转载 作者:行者123 更新时间:2023-12-03 15:04:52 25 4
gpt4 key购买 nike

这是我的代码,试图重新定义 * .只有当* 时才能实现。以前隐藏:

import Prelude hiding (*)

(*) :: Int -> Int -> Int
x * 0 = 0
x * y = x + x*(y-1)
但它不起作用:
$ ghci test.hs

GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help

test.hs:1:24: error: parse error on input ‘*’

Failed, modules loaded: none.

Prelude>


我可以隐藏其他功能:
import Prelude hiding (read)

import Prelude hiding (show)
虽然它不适用于 * 这样的运算符(operator), + , - .
我该如何隐藏它们?

最佳答案

回想一下您如何查询 ghci对于函数的类型:

:t read
:t show

关于运营商:

你输入 :t + ?

不,你会收到一个解析错误。

你做 :t (+) .

至于你的情况,你用额外的括号隐藏它: ((*))
import Prelude hiding ((*))

(*) :: Int -> Int -> Int
x * 0 = 0
x * y = x + x*(y-1)

关于haskell - 导入时如何隐藏运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40237270/

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