Erlang. Упражнения.

В рамках программы изучения Erlang я делаю упражнения из книги «Erlang programming» Франческо Чезарини и Саймона Томпсона. Оказалось что это на редкость полезное занятие, – теперь синтаксис не кажется таким уж страшным, и решение хоть и простых, но практических задач, придает уверенности в себе. Ниже я приведу условия задачи (уж простите, без перевода) и свой вариант решения (зачастую не оптимальный).

Exercise 3-1: Evaluating Expressions

Write a function sum/1which, given a positive integer N, will return the sum of all the integers between 1 and N.

Example: sum(5) ⇒ 15.

Write a function sum/2which, given two integers N and M, where N =< M, will return the sum of the interval between N and M. If N > M, you want your process to terminate
abnormally.

Example:
sum(1,3) ⇒ 6.
sum(6,6) ⇒ 6.

-module(test3_1).
-export([start/0]).
-import(io).
-import(lists).

listCreate(L, Start, End) ->
  if
	Start < End -> listCreate(L ++ [End], Start, End - 1);
	true -> L
  end.

sum(N) ->
  %Res = listCreate([], 0, N),
  %io:format("~w~n", [Res]),
  lists:sum(listCreate([], 0, N)).

sum(Start, End) ->
  lists:sum(listCreate([], Start, End)).

start() ->  
  io:format("~w~n~w~n", [sum(5), sum(1,3)]).

Exercise 3-2: Creating Lists

Write a function that returns a list of the format [1,2,..,N-1,N].

Example:
create(3) ⇒ [1,2,3].

Write a function that returns a list of the format [N, N-1,..,2,1].

Example:
reverse_create(3) ⇒ [3,2,1].

-module(test3_2).
-export([start/0]).

create(List, 0) -> List;
create(List, N) -> create(List ++ [N], N - 1).
create(N) -> create([], N).

start() ->
  create(3).

Exercise 3-3: Side Effects

Write a function that prints out the integers between 1 and N.

Hint: use io:format(“Number:~p~n”,[N]).

Write a function that prints out the even integers between 1 and N.

Hint: use guards.

-module (test3_3).
-import (io).
-export ([start/0]).

% implementation with if
print(Max, Cur) ->
  NewCur = Cur + 1,
  if 
    Max > Cur ->
      io:format("Number:~p~n",[NewCur]),
      print(Max, NewCur);
    true -> io:format("Number:~p~n",[NewCur])
  end.

print(Max) ->
  print(Max - 1, 0).

% implementation with guard
print1(Max, Min) when Max > Min ->
  NewCur = Min + 1,
  io:format("Number:~p~n",[NewCur]),
  print1(Max, NewCur);
print1(_, _) -> false.

print1(Max) -> 
  print1(Max, 0).

start() ->
%  print(3).
  print1(3).

Site Footer

Sliding Sidebar

About Me

About Me

For whom this blog for?

For those who are interested in modern Internet technologies, IT business, startups, management, quality control, personal effectiveness, motivation. Here I write about what is interesting, about problems I faced and solutions I found. I hope it will be interesting to you either.

What motivates me to write?

The desire to improve, to study deeper topics that interest me. Find people with similar problems and tasks, together look for ways out and solutions.

Feel free to contact if you have anything to say to me

Old Flash site with my artistic works and misuc.