Реализации алгоритмов/Алгоритм Робинсона — Шенстеда: различия между версиями
Содержимое удалено Содержимое добавлено
РоманСузи (обсуждение | вклад) программа на Elixir |
РоманСузи (обсуждение | вклад) немного уплотнил |
||
Строка 6:
# выставка в таблицу
def insert(table, nil), do: table
def insert([], x), do: [[x]]▼
▲ def insert([], x) do
def insert([current | rest], x) do
{updated, x1} = insert_into_row(current, x)
Строка 20 ⟶ 14 :
# вставка в строку
def insert_into_row([], x), do: {[x], nil}
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)
# вывод таблицы на печать
def print_table(table), do: IO.inspect(table |> Enum.map (&List.to_tuple/1))
# примеры
Строка 54 ⟶ 40 :
end
def example2, do: [3,1, 2] |> from_word |> print_table
end
|