Haskell中的递归加法

时间:2017-01-13 13:26:39

标签: haskell recursion addition

问题:

  

您将获得一个函数var apiGeolocationSuccess = function(position) { alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude); }; var tryAPIGeolocation = function() { jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) { apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}}); }) .fail(function(err) { alert("API Geolocation error! \n\n"+err); }); }; var browserGeolocationSuccess = function(position) { alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude); }; var browserGeolocationFail = function(error) { switch (error.code) { case error.TIMEOUT: alert("Browser geolocation error !\n\nTimeout."); break; case error.PERMISSION_DENIED: if(error.message.indexOf("Only secure origins are allowed") == 0) { tryAPIGeolocation(); } break; case error.POSITION_UNAVAILABLE: alert("Browser geolocation error !\n\nPosition unavailable."); break; } }; var tryGeolocation = function() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( browserGeolocationSuccess, browserGeolocationFail, {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true}); } }; tryGeolocation(); 。在不使用任何其他plusOne x = x + 1的情况下,定义递归函数(+),以便additionaddition x yx加在一起。

(来自wikibooks.org

我的代码(它不起作用 - 无限循环):

y

问题:

  1. 如何将plusOne x = x + 1 addition x y | x > 0 = addition (plusOne y) (x-1) | otherwise = y 函数连接到plusOne递归函数?
  2. 应该如何写?

2 个答案:

答案 0 :(得分:1)

您在递归案例

中混淆了xy
addition x y | y > 0 = addition (plusOne x) (y - 1)  -- x + y == (x + 1) + (y - 1)
             | otherwise = x   -- x + 0 = x

答案 1 :(得分:0)

使用private void prepareData(final boolean refresh) { @Override public void success(Chapter[] data, Response response) { retrofit = new RestAdapter.Builder() .setEndpoint("http://bolex.cba.pl/") .setLogLevel(RestAdapter.LogLevel.FULL) .build(); webService = retrofit.create(WebService.class); try { webService.getData(new Callback<Chapter[]>() { @Override public void success(Chapter[] data, Response response) { if (refresh) { chapterList.clear(); } for (Chapter item : data) { chapterList.add(item); } // I JUST ADDED THIS LINE BELOW mAdapter.notifyDataSetChanged(); refreshLayout.setRefreshing(false); } ... } ==

0