我尝试实现一个基本的lambda函数但是遇到了一些错误,并且在搜索这里的问题之后无法找出解决方案。我的代码是:
myMap :: (a -> b) -> [a] -> [b]
myMap addSomething [] = []
myMap addSomething (x:xs) = addSomething x : myMap addSomething xs
-- instance Show (a -> b) where
-- show a = "funcion"
list = [0..4]
listTwo = [(5 :: Int)..9]
addSomething :: Int -> Int
addSomething x = x + 1
addSomethingTwo :: Num a => a -> a-> a
addSomethingTwo x = (\x->x+1)
main = do
print $ myMap addSomething list
print $ myMap addSomethingTwo listTwo
这是我收到的错误消息
No instance for (Show (Int -> Int)) arising from a use of `print'
Possible fix: add an instance declaration for (Show (Int -> Int))
In the expression: print
In a stmt of a 'do' block: print $ myMap addSomethingTwo listTwo
In the expression:
do { print $ myMap addSomething list;
print $ myMap addSomethingTwo listTwo }
如果我取消注释这些行
instance Show (a -> b) where
show a = "function"
我得到了这个奇怪的结果
[1,2,3,4,5]
[function,function,function,function,function]
[Finished in 0.4s]
提前谢谢你, 的Tamas
答案 0 :(得分:4)
addSomethingTwo有一个函数(Int - > Int)作为结果,而不是Int
您需要将功能定义为
addSomethingTwo x = (\x->x+1) x
或者,要清除lambda和函数参数中的x是不同的变量(它们在不同的范围内):
addSomethingTwo y = (\x->x+1) y
答案 1 :(得分:3)
class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}
if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
// The following can be used to open an Activity of your choice.
// Replace - getApplicationContext() - with any Android Context.
// Intent intent = new Intent(getApplicationContext(), YourActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(intent);
// Add the following to your AndroidManifest.xml to prevent the launching of your main Activity
// if you are calling startActivity above.
/*
<application ...>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
/*
}
}
生成一系列函数。它的类型为myMap addSomethingTwo listTwo
,因为[Int->Int]
是二进制函数addSomethingTwo
(提供a->(a->a)
),这意味着它需要多个类型Num a
并返回数值函数a
,感谢currying。这些函数a->a
由a->a
放入列表中。
默认情况下无法打印功能,因为错误消息报告:&#34; map
没有Show
实例。您评论的Int -> Int
实例允许将功能打印为&#34; Show
&#34;。
我对你实际想要实现的目标一无所知。