我有这个相当简单的函数来计算大列表元素的平均值,使用两个累加器来保存到目前为止的总和以及到目前为止的计数:
mean = go 0 0
where
go s l [] = s / fromIntegral l
go s l (x:xs) = go (s+x) (l+1) xs
main = do
putStrLn (show (mean [0..10000000]))
Run Code Online (Sandbox Code Playgroud)
现在,用严格的语言,这将是尾递归,并且没有问题.然而,由于Haskell很懒,我的谷歌搜索让我明白(s + x)和(l + 1)将作为thunk传递递归.所以整件事崩溃和烧伤:
Stack space overflow: current size 8388608 bytes.
Run Code Online (Sandbox Code Playgroud)
进一步的谷歌搜索后,我发现seq
和$!
.这似乎我不理解,因为我在这种情况下使用它们的所有尝试都证明是徒劳的,错误信息说的是关于无限类型的东西.
最后我发现-XBangPatterns
,它通过改变递归调用来解决所有问题:
go !s !l (x:xs) = go (s+x) (l+1) xs
Run Code Online (Sandbox Code Playgroud)
但我对此并不满意,因为-XBangPatterns
目前这是一个扩展.我想知道如何在不使用的情况下严格评估-XBangPatterns
.(也许还可以学到一些东西!)
只是让你理解我缺乏理解,这就是我尝试过的(编译的唯一尝试,即):
go s l (x:xs) = go (seq s (s+x)) (seq l (l+1)) xs …
Run Code Online (Sandbox Code Playgroud) 我想要一个简单的方法让我的用户在购买前选择服装尺寸,但可配置产品似乎有点过分.你必须制作各种尺寸的产品,然后你必须制作一个可配置的产品.
这家商店是给我爸爸的,如果我必须向他解释他将如何做到这一点,他将需要几个星期的时间来了解这一点,(花了几天的时间让他学会创造一个简单的产品)
是否有任何其他可能性来创建产品,并在前端让用户选择大小属性..
必须有一个比这更简单的方法吗?
我在Fluent NHibernate中遇到了映射错误.当我明确指定列时,为什么还在寻找_id?
Invalid column name 'Account_id'.
[GenericADOException: could not initialize a collection: [ProtoStack.Business.Entities.Account.LedgerEntries#1][SQL: SELECT ***ledgerentr0_.Account_id*** as Account5_1_, ledgerentr0_.Id as Id1_, ledgerentr0_.Id as Id43_0_, ledgerentr0_.LedgerEntryDate as LedgerEn2_43_0_, ledgerentr0_.Amount as Amount43_0_, ledgerentr0_.AccountId as AccountId43_0_ FROM dbo.LedgerEntry ledgerentr0_ WHERE ledgerentr0_.Account_id=?]]
Run Code Online (Sandbox Code Playgroud)
我已明确指定该列为"AccountId".
public class AccountMap : ClassMap<Account>
{
public AccountMap()
{
Table("dbo.Account");
Id(x => x.Id)
.Column("Id");
Map(x => x.Code);
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.Category);
References(x => x.Group)
.Column("AccountGroupId");
HasMany(x => x.LedgerEntries)
.Inverse()
.Cascade.All();
}
}
public class LedgerEntryMap : ClassMap<LedgerEntry> …
Run Code Online (Sandbox Code Playgroud) 我有一个List,我需要按DateTime排序,类MyStuff看起来像:
public class MyStuff
{
public int Type {get;set;}
public int Key {get;set;}
public DateTime Created {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我需要能够通过Created(DateTime)字段对集合List进行排序.
使用BackgroundWorker创建thead和使用System.Threading.Thread创建线程有什么区别?
我试图找出最快的方法(在 PHP 5 中)来检查一个值是否是我需要的类型。我创建了两行代码,它们都做同样的事情。问题是我无法根据基准确定哪个最快。
(is_scalar($value) ? intval($value) : 0);
settype($value, 'integer');
Run Code Online (Sandbox Code Playgroud)
我创建了以下测试代码,但除了我自己的 PC(Core2Quad + XP 32 位 + php5.2.5)和一个用于测试它的 Dreamhost 帐户之外,我没有其他任何东西 - 这两个代码显示的时间大致相同。
$array = array(
'false' => FALSE,
'false2'=> 0,
'false3'=> '0',
'false4'=> 'FALSE',
'true' => TRUE,
'true2' => 1,
'true3' => '1',
'true4' => 'TRUE',
'char' => chr(250),
'char2' => chr(10),
'utf' => 0xF0,
'utf1' => 0xFE,
'number' => '452.5435',
'number2' => '-3948.33e2',
'number3' => -343.54,
'number4' => 99.999,
'number5' => '3jk439fjk23945t324098523.349fj324r',
'int' => …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个全能路由来跟踪联盟字符串何时在URL中.联盟代码由a x
后跟a 标记int
,并且仅出现在URL的末尾(但在查询字符串之前).
我的想法是,我将提取联盟会员ID,进行一些日志记录,然后在没有联盟会员ID的情况下对同一请求执行301.
例如:
http://www.domain.com/x32
http://www.domain.com/x32/
http://www.domain.com/path/to/something/x32
http://www.domain.com/x32?query=string
http://www.domain.com/x32/?query=string
http://www.domain.com/path/to/something/x32?query=string
http://www.domain.com/path/to/something/x32/?query=string
Run Code Online (Sandbox Code Playgroud)
我有这条路
routes.Add(new Route("{url}/x{affiliateExternalId}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(
new { controller = "Home", action = "LogCookieAndRedirect" }
),
Constraints = new RouteValueDictionary(new { affiliateExternalId = @"\d{1,6}" })
});
Run Code Online (Sandbox Code Playgroud)
哪个只匹配
http://www.domain.com/path/x32
http://www.domain.com/path/x32/
Run Code Online (Sandbox Code Playgroud)
我需要做什么来匹配所有内容并将查询字符串传递给控制器?有一个*
我怀疑我应该使用的操作员,但是我不能让它做我需要的.
c# ×2
php ×2
sorting ×2
asp.net-mvc ×1
attributes ×1
benchmarking ×1
c ×1
configurable ×1
file ×1
filesystems ×1
ghc ×1
haskell ×1
linked-list ×1
linux ×1
magento ×1
optimization ×1
performance ×1
powershell ×1
psake ×1
python ×1
routing ×1
setvalue ×1
url-routing ×1
vb.net ×1