如何在Haskell中汇总扩展列表的中间元素?

时间:2018-11-20 16:52:23

标签: list haskell sum expand

到目前为止,我知道如何从末端扩展列表,但是由于第一个条件(使单身人数增加一倍),它们最终会成倍增加。代码像这样有意义吗?

sumExpand :: [Integer] -> [Integer]

sumExpand l = expand l []
  where
    expand [] a     = a
    expand (x:[]) a = x: expand [] (x:a)
    expand (x:xs) a = expand (x:a) (expand xs a)

让我来处理它的输出:

[1,1,2,2,3,3] from [1,2,3]
instead of [1,3,5,3]

后者是我的愿望?这是我找到两个元素列表的临时解决方案的方法:

expand (x:xs) a = x: tail (expand (map (x+) xs) (last xs:a))

输出:

*Main> sumExpand [1,2]
[1,3,2]
*Main> sumExpand [1,2,3]
[1,7,4,3]

编辑:基本上,我希望算法像这样工作:[a, b, c] => [a, a+b, b+c, c]

1 个答案:

答案 0 :(得分:4)

基本上,您只想计算输入列表和其转换版本之间的按组件和:

float tsale = 0;
Connection con = getConnection();
PreparedStatement ps;
try {
ps = con.prepareStatement("select sum(amount) from sales where add_date = 
?;");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate;
addDate = dateFormat.format( new java.sql.Date(datec.getDate().getTime()));
ps.setString(1, addDate);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
    tsale = (float) rs.getLong(1);
}

} catch (SQLException ex) {
    Logger.getLogger(specSale.class.getName()).log(Level.SEVERE, null, ex);
}
salel.setText(String.valueOf(tsale));

用0(Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at specSale.datecAncestorAdded(specSale.java:174) at specSale.access$100(specSale.java:21) at specSale$2.ancestorAdded(specSale.java:80) at javax.swing.AncestorNotifier.fireAncestorAdded(AncestorNotifier.java:86) at javax.swing.AncestorNotifier.componentShown(AncestorNotifier.java:193) at java.awt.Component.processComponentEvent(Component.java:6365) at java.awt.Component.processEvent(Component.java:6313) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Window.processEvent(Window.java:2025) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) a b c d e a b c d e --------------------------- a a+b b+c c+d d+e e )填充每个空插槽,您只需要0:x

x++[0]