TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Using Monads in C++ to Solve Constraints: 1. The List Monad

61 点作者 andrzejsz大约 10 年前

5 条评论

TheLoneWolfling大约 10 年前
This method is <i>really</i> inefficient. There are b! &#x2F; (b - n)! ways to choose n of b unique numbers, whereas this solution is O(b^n).<p>That being said, it&#x27;s still fast enough for this example.<p>Also, Python solution along the same lines:<p><pre><code> import itertoolsdef def valueOf(*values, base=10): v = 0 for v in itertools.accumulate(values, lambda a,b: a*base+b): pass return v def solve(): for s,e,n,d,m,o,r,y in itertools.permutations(range(10), r=8): if valueOf(s,e,n,d) + valueOf(m,o,r,e) == valueOf(m,o,n,e,y): yield (s,e,n,d),(m,o,r,e),(m,o,n,e,y) </code></pre> (If anyone knows of a better way to do valueOf, let me know)
评论 #9528560 未加载
评论 #9571470 未加载
评论 #9539374 未加载
评论 #9550406 未加载
评论 #9528328 未加载
评论 #9528945 未加载
评论 #9527822 未加载
评论 #9529572 未加载
评论 #9529440 未加载
taeric大约 10 年前
Do you get bonus points for knowing that these are Alphametics? And covered in ridiculous detail in volume 4A of The Art of Computer Programming? I&#x27;m curious how the solutions there stack up to what is in mind here.
zeroonetwothree大约 10 年前
I would just use std::next_permutation (or equivalent partial permutation from a library). This produces pretty clean code.
awruef大约 10 年前
&quot;Once you internalize the many-worlds approach to programming, the implementation is pretty straightforward.&quot; are you serious? this is why people don&#x27;t take Haskell seriously.
tjradcliffe大约 10 年前
It is disingenous in the extreme to ask, &quot;So what’s all this talk about mutation and state? Well, who said you can’t have state in functional programming?&quot; when the <i>very first line</i> of the <i>very first hit</i> on &quot;functional programming mutation state&quot; is the Wikipedia article on functional programming, which says:<p>&quot;In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.&quot; -- <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Functional_programming" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Functional_programming</a><p>So the answer to the question &quot;Who said you can&#x27;t have state in functional programming?&quot; is: &quot;Every introduction to functional programming ever.&quot;<p>This is why people hate Haskell and make fun of Haskellers. In their quest to appear more clever than the rest of us, they engage in utterly disingenous rhetoric. Which is too bad, because I&#x27;m pretty sure they are actually more clever than the rest of us--certainly more clever than me--and don&#x27;t need this nonsense.<p>Haskell is an amazingly cool language, and I&#x27;ve talked to people at meetups who are using it for interesting things, and learning a bit of it myself has been an interesting and educational challenge. But getting around to learning it took five years longer than it should have because I was so put off by the rhetorical nonsense the language community engages in.
评论 #9532724 未加载