

Solving the Go Challenge 1 in Erlang - davidw
https://medium.com/@jlouis666/solving-the-go-challenge-1-in-erlang-8b1e0a0a5044

======
mroth
Funny, when I attempted this Go Challenge #1 I had the exact same reaction,
and halfway through ended up doing a solution in Elixir (which compiles to the
Erlang VM) before finishing the Go one.

I think the the particular problem (which by the way was super fun, great work
@mattetti!) was really well suited for pattern matching, so like OP I
gravitated towards those tools.

I had been keeping my solution private until the contest was over on the 28th,
but for sake of comparison of Elixir/Erlang idiomatic code (which should be
pretty similar), I'll make my repo public now too (which also contains my Go
solution):

[https://github.com/mroth/golang-challenge-1](https://github.com/mroth/golang-
challenge-1)

BTW, if you enjoy playing around with reverse engineering, I recommend NOT
looking at any of these solutions, and doing the challenge yourself first (in
whatever language you want). It was amongst the best of the "code challenge
exercises" I've attempted.

------
rwmj
Erlang pattern matching over binary data is one of the greatest things about
the language. I basically copied the idea wholesale for ocaml-bitstring[1].

[1]
[https://code.google.com/p/bitstring/](https://code.google.com/p/bitstring/)

~~~
detaro
I assume you are aware, but just to be sure: Friendly reminder that google
code is going away and you should migrate this somewhere else ;)

~~~
rwmj
I know I know :-(

------
mattetti
Go challenge author here, really glad to see this post and a great read for Go
and not Go developers.

------
jlouis
Author here. I'll try to watch this and answer questions.

~~~
hurin
Is there a link to just the code somewhere?

~~~
jlouis
Now there is:

[https://github.com/jlouis/go-challenge-1](https://github.com/jlouis/go-
challenge-1)

If you need a LICENSE, I can slam a MIT or Apache 2.0 on it for you, but...

------
hurin
Here is a Julia version for anyone who is interested (print's to stdout
instead of passing a struct though).

[https://gist.github.com/anonymous/23bbdf1f5077c7669844](https://gist.github.com/anonymous/23bbdf1f5077c7669844)

------
digitalzombie
I wish there was more explanation with the = sign in Erlang. In that it's not
an assignment but a pattern match first.

IIRC, it pattern match first and if the variable is not assign then it'll
assign it IIRC. The code and explanation just state that it's Erlang to do
assertion.

I think that's an interesting concept at least when I play with Erlang.

~~~
wwweston
I don't know Erlang, but I have done some Prolog and this sounds an awful lot
like unification of an equality predicate.

~~~
raphaelss
Erlang has some roots in Prolog. Its initial version was actually implemented
in Prolog.

------
djfm
I think I just fell in love with Erlang.

~~~
kibwen
Erlang is a wonderfully pragmatic and mature language, it doesn't get nearly
the hype it deserves.

~~~
amelius
I have one question. It is often said that in Erlang, one can have a large
number of threads. However, doesn't that cause problems with the memory
hierarchy (cache)?

~~~
jlouis
Erlang runs about K threads where K is the number of cores towards the OS. All
processes are "logical" in the same memory space and usually they happen to be
pretty small with a heap of a few kilobytes. As long as this is the case, then
even swapping among a number of threads, say 100, will not take you out of the
2nd level cache.

If you have a large number of processes, then it is a problem. And do note
that Erlang programs have way more pointer-redirections than a typical array-
like approach which is common in Go and other imperative languages. That is
the memory pressure by default will be higher than what you can achieve in
languages with static types.

It tends to be a problem for tight computational kernels, and Erlang
programmers usually fall down to C if they prove to become bottlenecks.

I have an older post here describing the schedulers:
[http://jlouisramblings.blogspot.dk/2013/01/how-erlang-
does-s...](http://jlouisramblings.blogspot.dk/2013/01/how-erlang-does-
scheduling.html) which is still reasonably up-to-date to give the overview.

------
tuxychandru
I tried rust on this. [https://github.com/tuxychandru/golang-challenge-
rust/tree/ma...](https://github.com/tuxychandru/golang-challenge-
rust/tree/master/drum)

It was shorter in LOC than my go version, mainly due to try!, but the lines
themselves were longer. The one difference in correctness was that I forgot to
close the files in Go, but RAII took care of it automatically in Rust.

------
BuckRogers
I'm primarily a Python programmer, but have had my eye on Erlang and Rust as
other languages of interest to look into at some point. While those two are to
my knowledge meant to solve problems in different domains, which one do you
think would be easier to grok or get going with faster between the two? I have
no C background. Just Python and SQL. I realize easier to get going with is a
poor metric. It's not the only one important to me but I'm just curious. My
initial looks into these led me to believe Erlang would probably be the easier
of the two coming from Python.

I love the looks of Rust because it's C ABI compatible and I could write
Python libraries with it.

Erlang because it looks to be the best thing going for webservices, which is
much of my work.

~~~
kibwen
Rust guy here, and I can tell you that there is a huge contingent of our
community made up of dynamic language programmers who have been seeking
experience in systems programming. That said, when it comes to systems
programming understanding the system itself is just as crucial as
understanding the language, and that takes time. If getting up to speed
quickly is important to you, perhaps you'd like Elixir ( [http://elixir-
lang.org/](http://elixir-lang.org/) ), which is sort of a Ruby-esque language
that runs on the Erlang VM.

~~~
BuckRogers
Thanks. I've looked into Elixir, I like it, but decided to not look into it
further. Erlang is already niche enough as it is.

------
amalag
I got stumped decoding the tempo. I see in a spoiler that the Hex is 00 00 F0
42 but I don't understand how 120.000 is decoded from that.

~~~
jlouis
Look up how we usually encode IEEE 754 floating point numbers and then also
think about endianess (little or big).

~~~
amalag
Thank you. I feel better prepared to lookup binary formats now. I should have
played with Synalize a bit more.

