My Erlang binary search
Ruslan Spivak - - August 15, 2007I was lurking and watching Erlang quite some time during this year and finally decided give it a shot after Programming Erlang was released. I ordered book in pdf format to rest assured i’ll get it in fastest possible way. After i read book a bit i was hooked
Today i came across binary search in Erlang blog post and decided to implement quickly binary search myself. My first attempt was to do it in python as it’s my daily job’s programming language and it turned this way (no check for edge cases like empty sequence):
def binsearch(seq, key, start, end): if end < start: return -1 mid = (start + end) / 2 if key == seq[mid]: return mid if key < seq[mid]: return binsearch(seq, key, start, mid-1) if key > seq[mid]: return binsearch(seq, key, start+1, end)
After i looked at code a bit i was surprised i wrote it recursively, earlier i would do it like:
def binsearch(seq, key): start = 0 end = len(seq) - 1 while True: if end < start: return -1 mid = (start + end) / 2 if key == seq[mid]: return mid if key < seq[mid]: end = mid - 1 else: start = mid + 1
Erlang obviously bends my mind
And my version in Erlang which was just exercise for myself and mainly looks like in original blog post:
-module(search). -export([binsearch/2]). -import(lists, [nth/2]). -include_lib("eunit/include/eunit.hrl"). binsearch(List, Key) -> binsearch(List, Key, 1, length(List)). binsearch(List, Key, LowerBound, UpperBound) -> if UpperBound < LowerBound -> -1; true -> Mid = (LowerBound + UpperBound) div 2, Item = nth(Mid, List), if Key < Item -> binsearch(List, Key, LowerBound, Mid-1); Key > Item -> binsearch(List, Key, Mid+1, UpperBound); true -> Mid end end. setup_test_() -> {setup, fun() -> [7, 11, 14, 40, 50] end, fun(L) -> [?_assertMatch(3, binsearch(L, 14)), ?_assertMatch(-1, binsearch(L, 70))] end }.
Running unit tests:
(em@localhost)1> search:test(). All 2 tests successful. ok
Categories: Blogs Ruslan Spivak
Erlang on Twitter
» coreyhaines (Corey Haines): Fun time at erlang meetup, happy to be heading home to @fablednet !
» FrancescoC (Francesco Cesarini): RT @ErlangSolutions: The #OpenFlow Webinar is tomorrow! Don’t forget to register http://t.co/QbassfPF9q Find out what the #SDN buzz is all …
» priestjim (Panos Papadomitsos): Dumping mnesia in favor of ets for in-VM beancounting (reqs/sec etc)? Yes, please! #erlang #devops
» sfalcon (Seth Falcon): RT @0xAX: Just pushed to github very simple #smtp client for #Ybot in #erlang - https://t.co/dQdNL4q1z0, maybe it will be useful for somebo…
» stephenpegoraro (Stephen Pegoraro): @fredwu enjoy! I started playing with it a few months ago. We all know how powerful the erlang vm is, having a ruby-eque syntax is a bonus!
» TrainByTweet_VO (TrainByTweet_VO): VOIP: #Engineering #Network #VOIP #Erlang #Ethernet #Codec #Voice #Bandwidth #Signaling #Protocol #Firewall #Security #Encryption
» yosukehara (Yosuke leo Hara): “dieswaytoofast / erlasticsearch” https://t.co/dRSnsf023t - I’ll try it :) #erlang #elasticsearch
» nslater (Noah Slater): @cirbif hop on to the Erlang mailing list. It’s for learning CouchDB and Erlang simultaneously. http://t.co/94psKRF5KQ
» RisakottaMarsha (Marsha Risakotta): Sakit apaaaa naya? RT @niayosephine: Pada akhirnya harus kesini juga (ㅠ.ㅠ) (at Praktek umum Dr. Erlang Setiawan) — http://t.co/2ElF9IHg6P
» podmostom (Jonn Mostovoy): RT @defnull: Hmm.. #erlang https://t.co/d2ser8cQL7
Statistics
Number of aggregated posts: 10650
Most recent article: May 20, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…