Now, I'm using Haskell for my school assignment, and I'm writing a code that multiplies all the numbers in the list as the title says.Example) [1,2,3,4,5] - > 120
At this stage, my code is as follows.
myproduct:::(Numa)=>[a]->a
myproduct[] = 0
myproduct(x:xs)=x*(myproductxs)
I tried writing about various sites the day after tomorrow, but none of them went well.
Also, I have to use the fold function, but I don't know what to do at all.
I would appreciate it if someone could tell me.Also, I would appreciate it if you could include explanations.
Thank you for your cooperation.
First of all, Try Haskell, so
For cumulative sum
foldl(+)0[1..10]
-- 55
-- ::(Enumb,Numb) =>b
For cumulative product
foldl(*)1[1..5]
-- 120
-- ::(Enumb,Numb) =>b
No matter what you multiply 0, it's zero, so
myproduct[]=0
Then the answer will always be zero.
myproduct[]=1
Then it should work.
© 2024 OneMinuteCode. All rights reserved.