这是一个快速的概念性问题,我目前正在努力学习和更好地理解Haskell.
我知道Show函数用于将值转换为字符串,但为什么函数类型不能与show一起使用?
Prelude> (\x -> x*3)
<interactive>:7:1:
No instance for (Show (a0 -> a0))
arising from a use of `print'
Possible fix: add an instance declaration for (Show (a0 -> a0))
In a stmt of an interactive GHCi command: print it
Prelude>
Run Code Online (Sandbox Code Playgroud) 想知道我是否可以帮助编写这个功能.我正在尝试创建一个反转列表中每个"对"的函数.
module Invert where
invert :: [(a,b)] -> [(b,a)]
invert [(a,b)] = [(b,a)]
Run Code Online (Sandbox Code Playgroud)
当我进入invert [(3,1) (4,1) (5,1)]......它应该给我[(1,3) (1,4) (1,5)......但它给了我......
*Invert> [(3,1) (4,1) (5,1)]
<interactive>:2:2:
The function `(3, 1)' is applied to two arguments,
but its type `(t0, t1)' has none
In the expression: (3, 1) (4, 1) (5, 1)
In the expression: [(3, 1) (4, 1) (5, 1)]
In an equation for `it': it = [(3, 1) (4, 1) (5, 1)]
Run Code Online (Sandbox Code Playgroud) 这可能是一个相当明显的问题,但我无法弄清楚.
我正在尝试编写一个在列表中对偶数进行平方的函数.当我尝试运行它时,我收到有关使用偶数函数的错误.我怎样才能解决这个问题?
module SquareEvens where
squareEvens :: [Integer] -> [Integer]
squareEvens n = [ns * ns | ns <- n, even n]
Run Code Online (Sandbox Code Playgroud) 尝试创建一个Haskell程序,将列表中的每个数字递增1.
module Add1List where
add1_list_comp :: [Integer] -> [Integer]
add1_list_comp [x] = [x + 1| x <- [x]]
Run Code Online (Sandbox Code Playgroud)
当我打电话add1_list_comp [3]给它时它起作用......它给了我[4]
但是当我这样做时add1_list_comp [3, 4, 5]......它会让我错误地说出来
"函数add1_list_comp中的非穷举模式"
任何帮助将非常感激!
haskell ×4