Haskell

Haskell 21: Factorial exercise in Haskell

20 videos

111 minutes

Well friends continue with the exercises haskell and this time we will see one of the exercises that you proposed which it is to calculate the factorial of a number haskell, so I keep encouraging them that if they exercise more proposals put them in the comments to see them resolved in future videos as in this case and good as we will see, I have here are three ways to solve this exercise with three different ways

and already I have written to save time then rather I mean, this is a haskell files .hs It is as simple as putting two L load points haskell.hs load and compile is fine with no errors, and I explain to them the first and second are recursively which is how this function is normally done in most languages ​​and is very simple

here are the pattern of the function, the factorial one that is the first function receives an integer and returns an integer and this is so when the number received is called N and the number that is returned as if zero is returned one and if not returned least one recursively likewise it called to multiply and there is as recursion does understand that if they're watching these videos haskell know what is a recursive function

and they know factorial calculation algorithm, if you do not understand this is because maybe you are in a more basic level programming and you should first learn other languages ​​but hey there you, is with If this is the function with conditional. This second does the same but without conditional, is patterned, which means that instead of asking the If directly

are set for when the factor receives the number is 0, when receiving a zero here are formatted function, for when you get a zero return for a one and when it receives any other number called N non-zero for calls herself multiply with least one and there goes subtracting doing multiplication. This is the logical and these two is what all languages ​​do,

but here comes the power of haskell, third definition with predefined function using the product itself is integrated calculation has haskell multiply all the numbers by themselves from one to the number in question thus receives an integer and returns an integer, get the number that you want to calculate the factorial and you return the product of all the numbers from one to itself,

so good we will calculate if it works, if we INVOICE1 of one hundred twenty five gives me is a hundred and twenty, if I invoke the passing number two function gives me five hundred twenty and if I invoke the passing function number three gives me five hundred twenty but right now this if I here do to seven thousand, oops sorry, INVOICE1 three hundred have everything that number and if I call the three but does not seem

what I say but does it faster, takes less time to execute. Well I hope you enjoyed this video so you can give like, share and let us know if you have any suggestions for the next video, a greeting

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.