Skip to content

Homework 2 is ready#5

Open
viktoriia-fomina wants to merge 15 commits into
masterfrom
FSharpHW2
Open

Homework 2 is ready#5
viktoriia-fomina wants to merge 15 commits into
masterfrom
FSharpHW2

Conversation

@viktoriia-fomina
Copy link
Copy Markdown
Owner

No description provided.

Copy link
Copy Markdown

@yurii-litvinov yurii-litvinov left a comment

Choose a reason for hiding this comment

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

1 и 4 надо немного поправить, 2 и 3 зачтены

// Использование рекурсии не допускается, зато нужен FsCheck для проверки функций
// на эквивалентность

/// <summary>Counts even numbers in the list using map.</summary>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Вообще, в F# не принято писать summary, пишут просто ///. Тулы про это знают и могут понять, что это и есть summary


/// <summary>Counts even numbers in the list using map.</summary>
let countEvenInListMap list =
if list = [] then None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не-а, в пустом списке ровно 0 чётных чисел

/// <summary>Counts even numbers in the list using map.</summary>
let countEvenInListMap list =
if list = [] then None
else Some(List.sum(List.map(fun x -> if x % 2 = 0 then 1 else 0) list))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Это было бы красивее через |>

let infiniteSeqOfPrimes =
let rec isPrime n acc =
if n = 2 then true
elif float acc > sqrt (float n) then true
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Проще было бы acc * acc > n


Seq.initInfinite(fun index ->
let n = index + 1
findNPrime n 2) No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Да ну, слишком сложно. Можно сгенерить последовательность всех чисел вообще и выбрать (через Seq.filter) из них те, которые удовлетворяют предикату isPrime. Думайте в терминах ленивых вычислений :)

Comment thread hw2/FSharpHW2/FSharpHW2/Program.fs Outdated
// let myTree = Node(5, Node(2, Leaf, Leaf), Leaf)
// let sq x = x * x

// mapTree myTree sq
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Закомментированный код нехорошо, да и вообще, есть тесты и F# Interactive, main не очень полезен


[<Test>]
let ``CountEvenInListMap for the list [2; 2; 2; 2] should return Some(4)`` () =
countEvenInListMap[2; 2; 2; 2] |> should equal (Some 4)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут просили FsCheck


[<Test>]
let ``Infinite sequence of primes should return 0`` () =
Seq.findIndex(fun x -> x = 2) infiniteSeqOfPrimes |> should equal 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Мне кажется, проще через Seq.nth

Copy link
Copy Markdown

@yurii-litvinov yurii-litvinov left a comment

Choose a reason for hiding this comment

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

В целом всё так, зачтены

Comment on lines +5 to +6
if list = [] then 0
else List.sum <| List.map(fun x -> if x % 2 = 0 then 1 else 0) list
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Зачем if, List.map и для пустого списка работает

/// Counts even numbers in the list using filter.
let countEvenInListFilter list =
if list = [] then 0
else List.filter(fun x -> x % 2 = 0) list |> List.length
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Что-то с пробелами беда. Это же не вызов функции filter от аргумента (fun x -> x % 2 = 0), это вызов от двух аргументов, первый из которых в скобках, чтобы приоритет операций ничего не сломал. Перед "(" надо пробел.

Seq.initInfinite(fun index ->
let n = index + 1
findNPrime n 2)
*)
Copy link
Copy Markdown

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