TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

61 pointsby andrzejszabout 10 years ago

5 comments

TheLoneWolflingabout 10 years ago
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 未加载
taericabout 10 years ago
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.
zeroonetwothreeabout 10 years ago
I would just use std::next_permutation (or equivalent partial permutation from a library). This produces pretty clean code.
awruefabout 10 years ago
&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.
tjradcliffeabout 10 years ago
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 未加载