我有一个功能:
powerOf :: Int -> Int -> Int
Run Code Online (Sandbox Code Playgroud)
示例os用法:
*Main Data.List> powerOf 100 2
2
*Main Data.List> powerOf 100 5
2
Run Code Online (Sandbox Code Playgroud)
我有两个问题.首先 - 为什么它不起作用:
map (powerOf 100) [2, 5]
Run Code Online (Sandbox Code Playgroud)
我想得到[2,2].
第二个问题.我试图创建pariatl函数.像这样的东西:
powerOfN :: Int -> Int
powerOfN num = powerOf num
Run Code Online (Sandbox Code Playgroud)
使用它这样的方式:
let powerOf100 = powerOfN 100
powerOf100 2
powerOf100 5
Run Code Online (Sandbox Code Playgroud)
但我收到了错误消息:
simplifier.hs:31:15:
Couldn't match expected type `Int'
against inferred type `Int -> Int'
In the expression: powerOf num
In the definition of `powerOfN': powerOfN num = powerOf num
Run Code Online (Sandbox Code Playgroud)
这里有很多代码: …
如果我想在字符末尾添加一个空格来返回一个列表,如果我没有传递任何参数,我将如何使用部分应用程序完成此操作?
这种类型也是?
space :: Char -> [Char]
Run Code Online (Sandbox Code Playgroud)
由于使用++和:运算符的'解析错误',我在最后添加空格时遇到了麻烦.
到目前为止我所拥有的是:
space :: Char -> [Char]
space = ++ ' '
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!谢谢
我正在学习偏见以及何时使用它们.在这个关于partials vs lambdas的页面中,接受的答案解释了partialsover 的优点之一lambdas是partials具有对内省有用的属性.因此我们可以使用partials来执行以下操作:
import functools
f = functools.partial(int, base=2)
print f.args, f.func, f.keywords
((), int, {'base': 2})
Run Code Online (Sandbox Code Playgroud)
实际上,我们不能这样做lambdas:
h = lambda x : int(x,base=2)
print h.args, h.func, h.keywords
AttributeError: 'function' object has no attribute 'args'
Run Code Online (Sandbox Code Playgroud)
但实际上,我们不能用"普通"Python函数做到这一点:
def g(x) :
return int(x,base=2)
print g.args, g.func, g.keywords
AttributeError: 'function' object has no attribute 'args'
Run Code Online (Sandbox Code Playgroud)
为什么partials比普通的Python函数有更多的功能?这种设计的目的是什么?内省被认为对正常功能无用吗?
我有两个功能 -
partialSubtractionWith5 :: (Num a) => a -> a
partialSubtractionWith5 = (subtract 5)
Run Code Online (Sandbox Code Playgroud)
和
partialSubtractionWith5' :: (Num a) => a-> a
partialSubtractionwith5' = (`subtract` 5)
Run Code Online (Sandbox Code Playgroud)
调用partialSubtractionWith5 x返回相当于x - 5,而调用partialSubtractionWith5' x返回相当于5 - x.
在Learn You a Haskell中,Lipovača定义了以下功能 -
isUpperAlphanum :: Char -> Bool
isUpperAlphanum = (`elem` ['A'..'B'])
Run Code Online (Sandbox Code Playgroud)
其中(基于我的实验subtract)我会认为当被称为时会表现得如此isUpperAlphanum 'some char':
Prelude> ['A'..'B'] `elem` 'some char'
False
Run Code Online (Sandbox Code Playgroud)
显然,事实并非如此.但为什么?有没有办法预测哪些函数在部分应用时会反转它们的参数?
我刚刚对值进行了一些验证,以查看它是三的乘积。大量使用模函数。我想管它。很好地使用部分应用程序。但显然不是。
这是我的fsi在vs代码中的一个示例。
> 27 % 3
-
- ;;
val it : int = 0
> (%) 3 27
- ;;
val it : int = 3
Run Code Online (Sandbox Code Playgroud)
我真的没想到从中缀和部分中得到不同的结果。
这是上下文管道中的操作:
...
|> Seq.length // 27
|> (%) 3 // 3
Run Code Online (Sandbox Code Playgroud) Hutton 的 Haskell 编程
一般来说,if 是一个运算符,则参数 和 的
#形式的表达式 称为节,其作为函数的含义可以使用 lambda 表达式形式化,如下所示:(#)(x #)(# y)xyRun Code Online (Sandbox Code Playgroud)(#) = \x -> (\y -> x # y) (x #) = \y -> x # y (# y) = \x -> x # y
“部分”和“柯里化”有什么区别和关系?
一个部分是将柯里化操作应用于多参数函数的结果吗?
谢谢。
haskell operators currying partial-application operator-sections