Haskell foldr意外地给出了错误

时间:2018-01-12 22:24:17

标签: haskell

为什么用这种方式使用foldr编写地图

map' :: (a -> b) -> [a] -> [b]
map' f xs = foldr (\x acc -> f x : acc) []

给出以下错误?

test.hs:2:13: error:
• Couldn't match expected type ‘[b]’ with actual type ‘t0 a -> [b]’
• Probable cause: ‘foldr’ is applied to too few arguments
  In the expression: foldr (\ x acc -> f x : acc) []
  In an equation for ‘map'’:
      map' f xs = foldr (\ x acc -> f x : acc) []
• Relevant bindings include
    xs :: [a] (bound at test.hs:2:8)
    f :: a -> b (bound at test.hs:2:6)
    map' :: (a -> b) -> [a] -> [b] (bound at test.hs:2:1)
|
2 | map' f xs = foldr (\x acc -> f x : acc) []
|             ^^^^^^^^^^^^^^^^^^^^^^^^^

1 个答案:

答案 0 :(得分:7)

您不使用controller/action。你可以写:

   public function actionUpdateRating() {

        $response['success'] = false;

        if ( Yii::$app->request->post () ) {

            $id = Yii::$app->request->post ( 'blog_id' );
            $model = Blog::findOne ( $id );

            //update your database with the new rating 
            //............
            //get new rating in $newRating

            $response['rating'] = $newRating;
            $response['success'] = true;
        }
        echo Json::encode ( $response );
        Yii::$app->end ();
    }

xs