Реализации алгоритмов/Алгоритм Робинсона — Шенстеда: различия между версиями

Содержимое удалено Содержимое добавлено
программа на Elixir
 
немного уплотнил
Строка 6:
 
# выставка в таблицу
def insert(table, nil), do: table
def insert([], x), do: [[x]]
table
end
 
def insert([], x) do
[[x]]
end
 
def insert([current | rest], x) do
{updated, x1} = insert_into_row(current, x)
Строка 20 ⟶ 14 :
 
# вставка в строку
def insert_into_row([], x), do: {[x], nil}
{[x], nil}
end
 
def insert_into_row([current | rest], x) when current <= x do
{updated, x1} = insert_into_row(rest, x)
{[current | updated], x1}
end
 
def insert_into_row([current | rest], x) when current > x do
{[x | rest], current}
Строка 34 ⟶ 24 :
 
# формирование таблицы на основе слова Шенстеда (списка)
def from_word(w), do: Enum.reduce(w, [], fn(x, acc) -> insert(acc, x) end)
Enum.reduce(w, [], fn(x, acc) -> insert(acc, x) end)
end
 
# вывод таблицы на печать
def print_table(table), do: IO.inspect(table |> Enum.map (&List.to_tuple/1))
IO.inspect(table |> Enum.map (&List.to_tuple/1))
end
 
# примеры
Строка 54 ⟶ 40 :
end
 
def example2, do: [3,1, 2] |> from_word |> print_table
[3,1, 2] |> from_word |> print_table
end
 
end