I want to use Haskell and write a code that multiplies all the numbers in the list.

Asked 1 years ago, Updated 1 years ago, 258 views

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.

haskell

2022-10-11 01:00

2 Answers

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


2022-10-11 01:00

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.


2022-10-11 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.