[erlang-questions] General advice on FP (& Erlang)
Milen Georgiev Dzhumerov
Sat Aug 11 20:41:55 CEST 2007
I've got a couple of questions. I'm probably not the first person who
is struggling to switch to a functional mindset. So, hopefully, any
replies to my questions will help other people too!
The way I got started is by reading the "Programming Erlang" book by
Joe (great book btw). As I was reading the book, everything seemed so
obvious and easy but when I'm trying to do something myself - I just
can't. I always go back to my "shared state" mind and don't seem to
be able come up with a design and a plan on how to implement it.
So, what are you guys (who get FP) suggesting I do? I think the
problem comes from having C hardwired into my brain and I can't get
And I definitely have got to ask this - how are we supposed to mutate
state? Using processes? Function calls? I just can't wrap my head
around how some C code is going to be translated.
Let's take a simple example. I'm writing a network app in Erlang
which accepts "messages" on a socket, where a "message" is defined as
length:content. If I was to do this in C, I would have a function
which gets called every time bytes arrive on the socket and then
depending on which state I'm in, I'm either going to append the bytes
to a length buffer and when I reach the ":" character, I'll change
the state and start appending to the content buffer (while
decrementing the number of bytes still left to be read).
Now all of this depends on having access to mutable buffers (& a
counter) outside the scope of the functions - at this point I'm lost
on how to do it in Erlang. How would guys start solving this problem?
Obviously, the more experience you have, the less thinking you're
going to put into this and the solution is going to come up
naturally, but what about people who haven't had any real world
experience with FP?
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 2419 bytes
Desc: not available
More information about the erlang-questions