Skip to content

Hw 1#1

Open
AnNyiiik wants to merge 6 commits intomainfrom
HW_1
Open

Hw 1#1
AnNyiiik wants to merge 6 commits intomainfrom
HW_1

Conversation

@AnNyiiik
Copy link
Owner

No description provided.

Comment on lines +60 to +61
else if List.head list = x then pos
else findElement (x) (pos + 1) (List.tail list)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше использовать pattern-matching вместо List.head / List.tail

printfn "%A" result_reveresed

let rec findElement x pos list =
if List.length list = 0 then -1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c pattern-matching можно проверить список на пустоту через сравнение с [], вычисление длины у связного списка работает за O(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Также можно возвращать None вместо -1 и переименовать функцию на tryFind

Comment on lines +45 to +51
let acc = acc * 2
makeListOfPowers
(n)
(m)
(i + 1)
(acc)
(acc :: list)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это получается список степеней в обратную сторону. Его нужно или обратить, вместо умножения головы делить!

2 * a

let rec makeListOfPowers n m i acc list =
if n > m then []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему так?

Comment on lines +32 to +39
let rec power n =
if n = 0 then 1
else if n % 2 = 0 then
let a = power (n / 2)
a * a
else
let a = power (n - 1)
2 * a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

эта функция, кажется, нигде не используется?

Comment on lines +25 to +26
let it = List.head list_old
reverse (it :: list_new) (List.tail list_old)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше использовать паттерн-матчинг. Он может с большей силой гарантировать безопасность при работе со списком, ещё на этапе компиляции. В данном случае нет, но в общем случае обращение к List.head может выбросить ошибку, если список пустой, что можно поймать ещё на этапе компиляции с правильным паттерн-матчингом.

Comment on lines +10 to +17
let rec fibonachi n1 n2 n =
if n = 0 then n1
else if n = 1 then n2
else
fibonachi
(n2)
(n1 + n2)
(n - 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше использовать эту функцию внутри функции, принимающей только n. В данном случае аккумулятор и n1, n2 являются деталями реализации и должны быть скрыты.

Comment on lines +1 to +8
let rec factorial acc x =
if x = 1 || x = 0 then acc
else
factorial
(acc * x)
(x - 1)
let a = factorial 1 1
printfn "%d" a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

аналогично данная функция с acc должна быть внутренней в функции, принимающей только один параметр x

Comment on lines +31 to +38
let rec power n =
if n = 0 then 1
else if n % 2 = 0 then
let a = power (n / 2)
a * a
else
let a = power (n - 1)
2 * a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно было записать с аккумулятором тогда уже

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants