编写一个doItX :: (a -> a -> a) -> a -> Int -> a函数,将第一个参数乘以f x第二个参数。
doItX (*) 2 1 == 2
doItX (++) "a" 6 == "aaaaaa"
doItX (*) 2 3 == 8
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
doItX f a x
| x==1 = a
| x>1 = doItX f (f a a) (x-1)
Run Code Online (Sandbox Code Playgroud)
如果 是 2 或更小,则效果很好x,但对于第三个示例,它返回16而不是8。
我想创建一个函数对象类,它可以按需在数字上集成一些函数。它的公共接口将包含一个void T::step(double dt)计算积分步骤的方法(并用新值更新函数对象的内部状态)和当前计算值的获取器。
虽然我确信这可以通过纯独立函数来完成,但我将要集成的函数有许多共同的中间计算步骤,并且依赖于许多内部参数,这些参数必须在每个步骤中更新(并且在很大程度上与用户),所以我想到使用函数对象来封装这个机制。
class myIntegrals
{
public:
void step(double dt);
double get_f1_integral() { return f1_integral; };
double get_f2_integral() { return f2_integral; };
private:
// funcs to be integrated (n.b. they share parameter/return types)
double f1(double t); // depends on the state of member attributes;
double f2(double t); // idem, and also on the results of f1
double t;
double f1_integral;
double f2_integral;
// and many internal parameters, updated on every step
}
void myIntegrals::step(double dt)
{
// …Run Code Online (Sandbox Code Playgroud) c++ templates pointer-to-member higher-order-functions c++11
我想使用Haskell高阶函数Foldr来计算字符串的长度
stringlength = foldr (\_n -> 1 + n) 0
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误.这段代码有什么问题?
Unresolved top-level overloading
*** Binding : stringlength
*** Outstanding context : (Num b, Num (b -> b))
Run Code Online (Sandbox Code Playgroud) 如果我在另一个函数中使用map或filter,那么该函数是否会成为一个高阶函数?例如:
removeSpaces :: String -> String
removeSpaces xs = filter (not . isSpace) xs
Run Code Online (Sandbox Code Playgroud)
是removeSpaces一个更高阶的函数?
我想创建三个Haskell函数:a,b,和c.
每个函数都有一个参数.这个论点是三个函数之一.
我想函数a有这种行为:
a则返回函数a.b.a.这里是我想要的功能回顾a:
a a = a
a b = c
a c = a
Run Code Online (Sandbox Code Playgroud)
这是我希望其他两个功能的行为:
b a = a
b b = a
b c = c
c a = c
c b = b
c c = c
Run Code Online (Sandbox Code Playgroud)
一旦创建,我希望能够以各种方式组合函数,例如:
a (c b)
= a (b)
= c
Run Code Online (Sandbox Code Playgroud)
我该如何创建这些功能?
haskell functional-programming function function-composition higher-order-functions
我从不认为使用,Reduce但我有一个问题,我认为这将是好事.我想确保向量的每个迭代元素的大小等于或大于前一个元素.我可以这样做,sapply但我的尝试Reduce失败了.我怎么用这个Reduce?
#This works
y <- c(1,2,3,2,4,4)
sapply(seq_along(y)[-length(y)], function(i) y[i] <= y[i+1])
#attempts
Reduce('<', c(1,2,3,2,4,4)), accumulate = TRUE)
Reduce('<', c(1,2,3,2,4,4)))
Run Code Online (Sandbox Code Playgroud) 嗨,我是Haskell初学者,我真的迷路了.这是我的任务,它要求我使用高阶函数执行类似下面的操作
Main> mySort (<) [1,5,3,6,4,1,3,3,2]
[1,1,2,3,3,3,4,5,6]
Main> mySort (>) [1,5,3,6,4,1,3,3,2]
[6,5,4,3,3,3,2,1,1]
Main> mySort longerWord [“Hello”, “The”, “a”, “Daniel”, “Declarative”]
[“Declarative”, “Daniel”, “Hello”, “The”, “a”]
Run Code Online (Sandbox Code Playgroud)
首先,我认为我应该创建一个区分它是<,>还是更长的函数
checkConditionStr::String->Int
checkConditionStr str
|str=="(<)" =1
|str=="(>)" =2
|str=="longerWord" =3
Run Code Online (Sandbox Code Playgroud)
但是这个例子没有引号(即mysort(<)不是我的排序"(<)"所以这是我的第一个问题.我这个函数但它没有编译.否则就是longWord
checkCondition::Ordering->Int
checkCondition ord
|ord==(<) =1
|ord==(>) =2
|otherwise =2
Run Code Online (Sandbox Code Playgroud)
其次,我仍然难以理解高阶函数.这有道理吗?
mySort::(String->Int)->[a]->[a]
mySort i list
|i==1 map (sortBy compare) list
|i==2 map (sortBy(flip compare)) list
Run Code Online (Sandbox Code Playgroud) 假设我们有两个类型的函数(a -> b -> c).我希望有一个单一的功能,其中,当应用于a和b将给予d,从合并c由一个特定的功能(c -> c -> d).我用箭头想出了这个解决方案:
combine :: (a -> b -> c) -> (a -> b -> c) -> (c -> c -> d) -> (a -> b -> d)
combine f g op = ((uncurry op) .) . (uncurry (&&&)) . (f &&& g)
Run Code Online (Sandbox Code Playgroud)
有没有办法以更优雅的方式做到这一点,或者将其概括为适用于更大的功能(例如(a -> b -> c -> d) -> (a -> b -> c -> d) -> (d -> …
我开始学习一些C++并且不理解高阶函数在C++中是如何工作的.有人可以在一个简单的例子中解释更高阶的c ++ 11函数吗?我无法在网上找到有关此主题的更多信息.
我正在尝试编写一个非常简单的函数,它接受一个列表(例如:) [1,2,3,1,5]并返回一个直接在特定元素之后的元素列表.
到目前为止我所得到的是:
function element list = filter (\x -> element:x) list
Run Code Online (Sandbox Code Playgroud)
我想要的输出:
功能1 [1,2,3,1,5]
=> [2,5]