def mainCaller() = {
val name = "xyz"
someList.foreach { u:Map => foo(name, u) }
}
def foo(name:String)(map:Map): Unit = {
//match case....
//recursive call to foo in each case where name remains same, but map changes
}
Run Code Online (Sandbox Code Playgroud)
我如何编写foo作为部分应用函数,我不必在每次递归调用中传递名称而只是调用foo(map1)?
在Learn You a Haskell的第6章中,介绍了以下函数:
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
Run Code Online (Sandbox Code Playgroud)
作者给出了几个使用它的例子,我发现它很容易遵循.然后这个:
ghci> zipWith' (zipWith' (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]]
Run Code Online (Sandbox Code Playgroud)
哪个输出 [[3,4,6],[9,20,30],[10,12,12]]
这是懒惰评估的一个例子吗?我试图将zipWith'翻译成Scheme(见下文).我使用了"简单"的例子,但不是最后一个,这让我觉得Haskell的懒惰可能会带来不同.
(define zipWith
(lambda (f listA listB)
(cond
((null? listA) (quote ()))
((null? listB) (quote ()))
(else (cons (f (car listA) (car listB)) (zipWith f (cdr listA) …Run Code Online (Sandbox Code Playgroud) scheme haskell partial-application lazy-evaluation higher-order-functions
我正在尝试使用boost :: bind将curried函数链接在一起,并获得无法解决的编译器错误.我可以做的最简单的例子,无法编译:
#include <iostream>
#include <boost/bind.hpp>
class A
{
public:
template <typename F>
void g(F fn, char c)
{
fn(c);
}
void h(char c)
{
std::cout << c << std::endl;
}
template <typename F>
void f(F fn, char c)
{
boost::bind(&A::g<F>, this, fn, ::_1)(c);
}
};
int main(int argc, char** argv)
{
char c = 'a';
A a;
a.f(boost::bind(&A::h, &a, ::_1), c);
}
Run Code Online (Sandbox Code Playgroud)
失败并出现此错误:
In file included from /usr/include/boost/bind.hpp:22,
from test8.cpp:2:
/usr/include/boost/bind/bind.hpp: In member function ‘void boost::_bi::list3<A1, A2, A3>::operator()(boost::_bi::type<void>, …Run Code Online (Sandbox Code Playgroud) 在我调试的Haskell项目中,代码中的实例map仅使用一个参数 - 列表 - 被传递.
例如
printReports :: [Report] -> IO ()
printReports = putStrLn . unlines . map show
Run Code Online (Sandbox Code Playgroud)
和
printRuns' :: [Run] -> IO ()
printRuns' = putStrLn . unlines . map showRecipes'
Run Code Online (Sandbox Code Playgroud)
map在这种情况下意味着什么/做什么?
我有这个功能:
map(map(fn x =>[x])) [[],[1],[2,3,4]];
Run Code Online (Sandbox Code Playgroud)
哪个产生:
val it = [[],[[1]],[[2],[3],[4]]]
Run Code Online (Sandbox Code Playgroud)
我不明白这个功能是如何工作的.每个地图功能都不需要功能和列表吗?似乎没有足够的参数来实际执行.
如果我跑:
map(fn x =>[x]) [[],[1],[2,3,4]];
Run Code Online (Sandbox Code Playgroud)
我明白了:
val it = [[[]], [[1]], [[2,3,4]]];
Run Code Online (Sandbox Code Playgroud)
这对我来说更有意义,因为它需要列表中的每个元素,并将其包装在另一个列表中.但是当我在其上放置另一张地图时,它会改变输出.任何人都可以向我解释这个吗?谢谢!
我有部分应用的函数add来创建一个新函数addOne.
add :: Int -> (Int -> Int)
add x y = x + y
Run Code Online (Sandbox Code Playgroud)
addOne可以使用显式参数定义
addOne :: Int -> Int
addOne y = add 1 y
Run Code Online (Sandbox Code Playgroud)
或没有explict参数
addOne :: Int -> Int
addOne = add 1
Run Code Online (Sandbox Code Playgroud)
我有四个问题:
这里与我的问题密切相关,但实际上是一个不同的问题......
考虑以下F#: -
type TestClass() =
let getValFromMap m k = Map.find k m
let mutable someMap : Map<string,int> = Map.empty
let getValFromMapPartial key = getValFromMap someMap key
let getValFromMapPartialAndTacit = getValFromMap someMap
module TestModule =
let getValFromMap m k = Map.find k m
let mutable someMap : Map<string,int> = Map.empty
let getValFromMapPartial key = getValFromMap someMap key
let getValFromMapPartialAndTacit = getValFromMap someMap
Run Code Online (Sandbox Code Playgroud)
在类案例和模块案例中,getValFromMapPartial并getValFromMapPartialAndTacit以非常不同的方式运行,并以不同方式编译为IL.在类和模块的情况下,前者的行为类似于真正的语法函数,后者的行为类似于lambda计算函数(我知道这要归功于用户Marc Sigrist).
在模块的情况下,类型签名似乎是正确的: -
getValFromMapPartial : key:string -> int
getValFromMapPartialAndTacit …Run Code Online (Sandbox Code Playgroud) 对于部分功能应用,我知道在Python中有几种方法可以做到这一点.但是,他们似乎不保留原始函数的docstring.
以functools.partial为例:
from functools import partial
def foo(a, b, c=1):
"""Return (a+b)*c."""
return (a+b)*c
bar10_p = partial(foo, b=10)
print bar10_p.__doc__
partial(func, *args, **keywords) - new function with partial application
of the given arguments and keywords.
Run Code Online (Sandbox Code Playgroud)
我们试试fn.py:
from fn import F
def foo(a, b, c=1):
"""Return (a+b)*c."""
return (a+b)*c
bar10_F = F(foo, b=10)
print bar10_F.__doc__
Provide simple syntax for functions composition
(through << and >> operators) and partial function
application (through simple tuple syntax).
Usage …Run Code Online (Sandbox Code Playgroud) 我看到Swift为声明curried函数提供了方便的语法.本手册给出了部分功能应用,作为curry功能派上用场的一个例子.
有人能给我一个部分函数应用程序有用的例子吗?我知道这是一个通用的函数式编程概念,但Swift中的一个例子将是最受欢迎的.
假设我有一个将3个参数作为输入的函数。如何在Elm中部分应用此函数,以便它使用第一个和最后一个参数,并等待第二个参数返回最终结果?
这可以在完成Ramda与R.__被命名placeholer。