标签: higher-order-functions

Haskell高阶函数问题

现行守则

我有两个功能

f1::Int->Int->Int
f1 a b | a==1 &&  b==1 = 1
       | otherwise = 0
Run Code Online (Sandbox Code Playgroud)

[Int]通过另一个函数将此函数应用于a

f2::[Int]->[Int]->[Int]
f2 a b = map f1 a b
Run Code Online (Sandbox Code Playgroud)

错误

Type error in application
*** Expression     : map f1 c d
*** Term           : map
*** Type           : (e -> f) -> [e] -> [f]
*** Does not match : a -> b -> c -> d
Run Code Online (Sandbox Code Playgroud)

结论

实际上我所要求的是应用f1哪个执行Int到一个[Int]使用f2 是这个问题可以在更高阶函数中解决?或任何其他方法?...或者我如何转变f2为更高阶的功能f1 …

haskell higher-order-functions

1
推荐指数
1
解决办法
267
查看次数

Haskell更高阶函数需要帮助

这段代码有什么问题?

addNum :: Int->Int-> Int
addNum a b = a+b

divideby :: ( Int->Int -> Int ) -> Int  ->float
divideby  f  z  =  f /z
Run Code Online (Sandbox Code Playgroud)

我想将该addNum函数作为divideby带除数的输入,然后输出答案.因此该divideby函数应该充当更高阶函数.

这段代码有什么问题?它给出以下错误:

*** Expression     : f / z
*** Term           : f
*** Type           : Int -> Int -> Int
*** Does not match : Int
Run Code Online (Sandbox Code Playgroud)

haskell higher-order-functions

1
推荐指数
1
解决办法
244
查看次数

调用时间(()=> {})和时间{}之间有什么区别?

我正在使用metrics-scala库,并且无法理解为什么下面的两个调用表现完全不同

// method 1
writeTimer.time(() => {
  // expensive operation
})

// method 2
writeTimer.time {
  // expensive operation
}
Run Code Online (Sandbox Code Playgroud)

在方法1的情况下,从不调用昂贵的操作,而在方法2中,它是.

writeTimer是一个实例com.yammer.metrics.Timer,其中time方法声明为:

/**
 * Runs f, recording its duration, and returns the result of f.
 */
def time[A](f: => A): A
Run Code Online (Sandbox Code Playgroud)

我刚刚在我的代码中解决了一个错误,我必须使用方法2来使其工作.

scala higher-order-functions

1
推荐指数
1
解决办法
101
查看次数

Haskell:创建一个在列表上行动的功能

可能重复:
为什么haskell中不允许这样的函数定义?

我想创建一个函数flist,它接受一个函数f作为参数,并返回另一个函数,其参数将是一个列表,但行为完全相同f.

例如:

let f x1 x2 x3 = x1+ x2 + x3
Run Code Online (Sandbox Code Playgroud)

我想要这种行为

(flist f) [x1,x2,x3] = x1+x2+x3
Run Code Online (Sandbox Code Playgroud)

当列表长度不是3时,它可能以任何方式表现.flist应该照顾任何函数(不仅是带有3个参数的函数,即if g x1 x2 x3 x4 = x1+x2+x3*x4,then (flist g) [x1,x2,x3,x4] = x1+x2+x3*x4).

我试过这个,

flist f [] = f
flist f (x:xs) = flist (f x) xs
Run Code Online (Sandbox Code Playgroud)

但它没有用.我该如何实现这一目标?我可以使用数据类型来执行此操作吗?

haskell higher-order-functions

1
推荐指数
1
解决办法
224
查看次数

高阶谓词

boolean(true).
boolean(false).
formula_0(P, Q):- (P; Q), \+ P.

solution_for_formula(P, Q, Formula):-
    maplist(boolean, [P, Q]), call([Formula, P, Q]).
Run Code Online (Sandbox Code Playgroud)

对我之前的问题的跟进。为什么这行不通?(如果我call([Formula, P, Q])formula_0(P, Q)它替换它有效。)

prolog higher-order-functions meta-predicate

1
推荐指数
1
解决办法
918
查看次数

JS中的高阶函数

我现在正在学习JavaScript.我有一些问题.以下代码来自Eloquent JavaScript:

function forEach(array, action) {
    for (var i = 0; i < array.length; i++)
        action(array[i]);
}

var numbers = [1, 2, 3, 4, 5],
    sum = 0;

forEach(numbers, function(number) {
    sum += number;
});

console.log(sum); 
Run Code Online (Sandbox Code Playgroud)

这段代码发生了什么?当调用forEach函数时,它如何确定数字是多少?它是如何从数字数组中提取数字的?

javascript higher-order-functions

1
推荐指数
1
解决办法
92
查看次数

使用purrr :: reduce2复制嵌套gsub调用的行为

给定样本字符串:

tst_str <- c("abc", "123", "klm", "lop")
Run Code Online (Sandbox Code Playgroud)

我想做以下替换:

  • abc -> za12
  • 123 -> poi
  • klm -> uyt

期望的结果

简单的嵌套嵌套gsub可以产生以下结果:

gsub(
    pattern = "abc",
    replacement = "za12",
    x = gsub(
        pattern = "123",
        replacement = "poi",
        x = gsub(
            pattern = "klm",
            replacement = "uyt",
            x = tst_str
        )
    )
)
# [1] "za12" "poi"  "uyt"  "lop" 
Run Code Online (Sandbox Code Playgroud)

问题

我想使用purrr::map*purrr::reduce函数得出相同的结果.我最初的想法是利用purrr::reduce2

purrr::reduce2(
    .x = c("abc", "123", "klm"),
    .y = c("za12", "poi", "uyt"),
    .f = function(x, y, …
Run Code Online (Sandbox Code Playgroud)

functional-programming r higher-order-functions purrr

1
推荐指数
1
解决办法
79
查看次数

Kotlin:高阶函数,如何向集合中添加函数并调用它们

我有一个类,它包含一组应该在某个事件上调用的函数("监听器")(Android上的Gps更新,但这在这里不重要).看起来像这样(为了清晰起见,大大简化了):

class myClass {
private var listeners = mutableSetOf<(Location) -> Unit>()

fun addListener(listener: (Location) -> Unit) {
    listeners.add { listener }
}

private fun updateListeners(location: Location) {
    if (!listeners.isEmpty()) {
        listeners.forEach {
            it.invoke(location)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试从另一个类中添加一个函数,我想在调用updateListeners()时调用它.

class myOtherClass {

private fun registerLocationListener() {
    myClass.addListener (this::onLocationUpdateReceived)
}

private fun onLocationUpdateReceived(location: Location) {
    // do something with the location data
}
Run Code Online (Sandbox Code Playgroud)

编译器在这里没有给我任何警告,所以我首先假设这是正确的.但onLocationUpdateReceived不会被调用.如果我用.toString()记录我的集合中的项目,我得到

Function1<android.location.Location, kotlin.Unit>
Run Code Online (Sandbox Code Playgroud)

这似乎是我想要的 - 但我对此事的经验有限,所以我可能错了.所以我知道updateListeners()被调用,我知道"东西"被放入我的集合中,但onLocationUpdateReceived永远不会被调用.

任何人都可以帮助我如何设置它以便它有效吗?

lambda function set higher-order-functions kotlin

1
推荐指数
1
解决办法
80
查看次数

了解翻转功能

我正在从“了解Haskell带来的好处!”中学习高阶函数。由Miran Lipovaca撰写。

对于以下函数flip,该函数需要一个函数并返回一个具有翻转前两个参数的函数:

flip' :: (a -> b -> c) -> (b -> a -> c)
flip' f = g
   where g x y = f y x
Run Code Online (Sandbox Code Playgroud)

我完全不明白是什么fg有。它们是两个不同的功能吗?同样,在where绑定中,究竟是什么g x y = f y x意思?

haskell functional-programming higher-order-functions

1
推荐指数
2
解决办法
105
查看次数

Haskell中flip函数的定义

Currently I am trying to learn Haskell with the book 'Learn You a Haskell' and I'm trying to understand the implementations of the flip function in chapter 5. The problem is that the author states that if g x y = f y x is valid, then f y x = g x y must be also true. But how and why does this reversal affects the two function definitions?

I know how currying works and I also know …

haskell functional-programming higher-order-functions

1
推荐指数
1
解决办法
121
查看次数