我正试图深入研究,认为这对我来说似乎是一个非常强大的资产。但是,你能帮我解决这个问题:
foldr (/) 2 [1,2,3] -- (1/(2/(3/2))), result 0,75 {where 2 is base)
foldr1 (/) [2,2,3] -- (1/(2/(3/2))), result 3.00 {where 2 is base)
我认为我正在认真监督褶皱之间的本质区别。 THX
答案 0 :(得分:6)
foldr :: (a -> b -> b) -> b -> [a] -> b
具有实施方式:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr _ z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
这意味着如果我们输入foldr f z [x1, x2, x3]
,那么它将被评估为:
foldr f z [x1, x2, x3]
-> f x1 (foldr f z [x2, x3])
-> f x1 (f x2 (foldr f z [x3]))
-> f x1 (f x2 (f x3 (foldr f z [])))
-> f x1 (f x2 (f x3 z))
因此,您的示例将评估为:
(/) 1 ((/) 2 ((/) 3 2))
= 1 / (2 / (3 / 2))
= 1 / (2 / 1.5)
= 1 / 1.33333...
= 0.75
foldr1 :: (a -> a -> a) -> [a] -> a
函数几乎类似,除非我们看到1元素列表,我们返回该元素,因此区别为:
foldr1 :: (a -> a -> a) -> [a] -> a
foldr1 _ [x] = x
foldr f (x:xs) = f x (foldr1 f xs)
这意味着我们得到foldr1 f [x1, x2, x3]
:
foldr1 f [x1, x2, x3]
-> f x1 (foldr1 f [x2, x3])
-> f x1 (f x2 (foldr1 f [x3]))
-> f x1 (f x2 x3))
因此,对于样本输入,我们得到:
(/) 2 ((/) 2 3)
= 2 / (2 / 3)
= 2 / 0.6666...
= 3.0
因此,如果z
和xi
具有相同的类型,则foldr f z [x1, ..., xn]
等于foldr1 f [x1, ..., xn, z]
。
答案 1 :(得分:6)
一般
foldr1 op [y1, ..., yn, x]
与
基本相同foldr op x [y1, ..., yn]
只要n >= 1
。也就是说,在你的特殊情况下
foldr1 (/) [2, 2, 3]
将评估为2/(2/3) = 3
。
使用除法运算符,它有点无聊,因为
foldr1 (/) [y, y, x1, ... , xn]
= y/(y/(foldr1 (/) [x1, ..., xn]))
= foldr1 (/) [x1, ..., xn]
适用于所有n >= 1
。因此,在您的示例中,它应该只返回
foldr1 (/) [3]
当然是3.0
,它确实评估为3.0
。
这是一个不太简洁的例子:
foldr1 (/) [2, 3, 5, 7] -- evals to 0.47619047619047616
这里,它给出了与2 / (3 / (5 / 7))
相同的结果,而foldr (/) 7 [2, 3, 5]
依次为$(document).ready(function () {
var ds = new kendo.data.DataSource({
type: "aspnetmvc-ajax",
transport: {
read: "http://localhost:51618/Home/Customers_Read",
dataType: "json",
type: "POST"
},
pageSize: 20,
serverPaging: true,
serverFiltering: true,
serverSorting: true,
});
$("#grid").kendoGrid({
dataSource: ds,
height: 550,
groupable: true,
filterable:true,
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
columns: [{
template: "<div class='customer-photo'" +
"style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></div>" +
"<div class='customer-name'>#: ContactName #</div>",
field: "ContactName",
title: "Contact Name",
width: 240
}, {
field: "ContactTitle",
title: "Contact Title"
}, {
field: "CompanyName",
title: "Company Name"
}, {
field: "Country",
width: 150
}]
});
});
与
FileReader
我希望它能稍微澄清一下评估的顺序。