Mam zadanie, aby ukończyć szyfr cezara w 7 językach. Pracuję nad ukończeniem tego Erlanga obecnie. Byłem wcześniej wystawiony na funkcjonalne języki, więc ogólnie rozumiem, co muszę zrobić. Mam specyficzne problemy ze zrozumieniem korzystania z funkcji foreach w Erlang. Wiem, że jest używany, gdy interesuje Cię efekt uboczny, więc jestem pewien, że jest to "właściwy" sposób robienia tego, co chcę. Przeczytałem this answer i definicję foreach w języku Erlang o numerze referencyjnym here. Jednak wciąż jestem trochę zdezorientowany i mam problemy z prawidłowym składni.Zrozumienie i używanie foreach w Erlang
-module(caesar).
-export([main/2]).
enc(Char,Key) when (Char >= $A) and (Char =< $Z) or
(Char >= $a) and (Char =< $z) ->
Offset = $A + Char band 32, N = Char - Offset,
Offset + (N + Key) rem 26;
enc(Char, _Key) -> Char.
encMsg(Msg, Key) ->
lists:map(fun(Char) -> enc(Char, Key) end, Msg).
main(Message, Key) ->
Encode = (Key),
Decode = (-Key),
Range = lists:seq(1,26),
io:format("Message: : ~s~n", [Message]),
Encrypted = encMsg(Message, Encode),
Decrypted = encMsg(Encrypted, Decode),
io:format("Encrypted: ~s~n", [Encrypted]),
io:format("Decrypted: ~s~n", [Decrypted]),
io:format("Solution: ").
%% Foreach belongs here, should execute Encrypted = encMsg(Message, N) where
%% N is the value in Range for each value in the list, and then print Encrypted.
Idealny, wyjaśnił to dla mnie. Dzięki! – NickAbbey