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.

Amb in JavaScript

66 pointsby mnemonikabout 14 years ago

4 comments

swannodetteabout 14 years ago
Great post but it's the language and syntax hoop-jumping that is required in JavaScript to do paradigm experimentation (or <i>playing</i> as Feynman would call it) that's pushed me to take the time to explore other languages.<p>Here's the Fish/Zebra/Einstein puzzle in my Clojure logic programming library based on miniKanren, this program is solved on my machine in <i>2 milliseconds</i> - even though this version involves unification and logic variables. It also reads better IMO and should look familiar to the Prologist:<p><pre><code> (defn-e first-o [l o] ([[?a . ?d] ?a])) (defn-e member-o [x l] ([_ [x . ?tail]]) ([_ [?head . ?tail]] (member-o x ?tail))) (defn-e on-right-o [x y l] ([_ _ [x y . ?r]]) ([_ _ [_ . ?r]] (on-right-o x y ?r))) (defn next-to-o [x y l] (cond-e ((on-right-o x y l)) ((on-right-o y x l)))) (defn zebra-o [hs] (macro/symbol-macrolet [_ (lvar)] (all (== [_ _ [_ _ 'milk _ _] _ _] hs) (first-o hs ['norwegian _ _ _ _]) (next-to-o ['norwegian _ _ _ _] [_ _ _ _ 'blue] hs) (on-right-o [_ _ _ _ 'ivory] [_ _ _ _ 'green] hs) (member-o ['englishman _ _ _ 'red] hs) (member-o [_ 'kools _ _ 'yellow] hs) (member-o ['spaniard _ _ 'dog _] hs) (member-o [_ _ 'coffee _ 'green] hs) (member-o ['ukrainian _ 'tea _ _] hs) (member-o [_ 'lucky-strikes 'oj _ _] hs) (member-o ['japanese 'parliaments _ _ _] hs) (member-o [_ 'oldgolds _ 'snails _] hs) (next-to-o [_ _ _ 'horse _] [_ 'kools _ _ _] hs) (next-to-o [_ _ _ 'fox _] [_ 'chesterfields _ _ _] hs)))) (run 1 [q] (zebra-o q))</code></pre>
评论 #2428103 未加载
cslabout 14 years ago
Here is a nice paper on how to prune the execution tree when using <i>amb</i>: <i>Non-deterministic lisp with dependency-directed backtracking</i>; <a href="http://www.aaai.org/Papers/AAAI/1987/AAAI87-011.pdf" rel="nofollow">http://www.aaai.org/Papers/AAAI/1987/AAAI87-011.pdf</a><p>I've not played much with <i>amb</i> or Prolog, but it would be cool to try out some alternative strategies for exhausting the search tree, perhaps simulated annealing or something like that.
skybrianabout 14 years ago
This is the sort of language feature that looks very neat at first, but there are simpler ways to solve the same problems. If you want to search a space by trying all possibilities ("generate and test"), the most straightforward way is a nested for loop with an if statement.<p>The advantage of using nested for loops is that it's pretty clear that there's a performance problem and most programmers will have a good idea what to do about it. (Perhaps change the order of the loops, do checks in the outer loops to avoid running the inner loops, and so on. For larger problems, figure out how to parallelize.)<p>Of course, a for loop isn't very modular, so something like Python's generator expressions might be more suitable for building pipelines. In Java, this can be done with iterators and some good iterator utility methods, such as those in Guava.
swiftabout 14 years ago
Haven't seen this operator before; reminds me of angelic programming. (see eg. <a href="http://portal.acm.org/citation.cfm?id=1706339" rel="nofollow">http://portal.acm.org/citation.cfm?id=1706339</a>)<p>This also seems like a very natural application of monads.