On Mon, Feb 28, 2011 at 9:15 PM, Jesper Louis Andersen <
> wrote:
> 2011/2/28 Wang Wei <>:
> > Hello, I has a question about how to convert the bellow C program into
> Erlang.
>> I've been doing exactly this for some time. The problem is that you
> have two monarchies. In the empire of imperativism, the control flow
> king rules, whereas the data flow queen is being forced off the
> throne. If the queen wants to cheat on the king with a knight, she has
> to delve into the dungeon of global scope, or sneak out to the house
> of structs. There, the knight and the queen can do their naughty deed
> by destructively updating a variable.
>> In the realm of declarativism however, the queen is so seductive and
> intelligent, she effectively rules over the king. Knights here are
> always passed as parameters and are never used in foreign lands for
> war. Nay, a knight is for local dancing and feast. After the dance,
> the knight is dutifully sent on to the next party.
>>GOOD fairy tale, I like it!
In functional programming, I'd like to have a basic program:
compare(Subject, Object, DoFunc) ->
if
Subject == Object -> apply(DoFunc, [Subject]) ;
true -> nil
end.
that I'll get something done when a test is passed or otherwise nothing.
Then I can test cases A, B, C, and so on by using compare/3.
I need a control flow function:
test_seq( [] ) -> ok ;
test_seq( [{S,O,F}|SOFs] ) ->
case compare(S, O, F) of
nil -> test_seq(SOFs) ;
Result -> apply(F, [S])
end.
It won't return any data because in imperative world things are effected but
picked. By doing a function-call
test_seq( [{ getA(), CONF1, fun doSomeThing/1}
, { getB(), CONF2, fun doSomeThing/1 } ] ),
I get it work. However, for imperative purpose, the state of a program is
very important. There may be a states-handling function manage/N, and
an environment should be added into and approached inside test_seq/1:
test_seq( State, [] ) -> State ;
test_seq( State, [{S,O,F}|SOFs] ) ->
case compare(S, O, F) of
nil -> test_seq(State, SOFs) ;
Result -> manage(State, apply, F, [S])
end.
that manage/N takes care of State.
--
Y.-H. H.