⬆️ ⬇️

Solving Project Euler with F #: Task 1

After reading the first few articles from the cycle Fall in love with F # , I really, if I didn't fall in love with it, then at least I became interested. So much so that he could not bear the expectations of the next dose and decided to continue studying on his own.

In the process of lurking, I came across an extremely exciting Project Euler site, which, in my opinion, is the best suited to gradually learn everything, or at least most of the subtleties of F #. Now I propose to begin to consider the solution of the very first problem from this site. It is very simple, and I think its solution will not cause difficulties even for those who are familiar with F # only in the above article. So…



The condition is as follows:



If we list all the natural numbers below, we can get 3, 5, 6 and 9. The sum of these multiples is 23.



Find the sum of all the multiples of 3 or 5 below 1000.

')

The solution to this problem is extremely concise:



#light

let _ =

[1..999]

|> List.filter ( fun x -> x%3=0 or x%5=0)

|> List.fold_left (+) 0

|> printfn( "The answer is %d" )





I think that in general there is nothing difficult, but you should pay attention to a few things that have not yet been mentioned in the series of articles about F #.

First, it is the operator "|>", which is formally defined as follows:

let (|>) af = fa


in simple terms, it passes the value calculated in front of it as an argument to the next function. This simple operator allows in many cases to significantly reduce the code.



Secondly, the lambda function is used here to determine the numbers divisible by 3 or 5, already familiar even to most imperative programmers thanks to LINQ. Its syntax differs from LINQ except by having fun before defining a function, and using -> instead of =>.



Third, it uses two built-in functions for working with lists: List.filter and List.fold_left . The first of them filters the list in accordance with a given Boolean function, which we use as our lambda. The second in our case summarizes all the elements of the list. Zero, the second argument in the list, specifies the initial value of the battery. Thus, to multiply all the elements of the list, the code will look like this: List.fold_left (*) 1 .

Naturally, you can write both of these functions yourself, for example, for filtering, it might look something like this:



let rec filter filter_fun lst =

match lst with

|[] -> []

|head::tail -> if filter_fun head then head::(filter filter_fun tail)

else filter filter_fun tail





but I think everyone has long been convinced that reinventing the wheel is not always worth it.

Thus, the entire program consists essentially of two consecutive steps that immediately bring the desired result. Pretty pretty, isn't it?



UPD: Friends, if you minus the topic or karma, please write the sail lines, for what. I am calm about criticism, and next time I will try to do better. Thank.



Source: https://habr.com/ru/post/52150/



All Articles