Unable to define my primitive recursion definitions in haskell

I have a good idea that there are primitive recursive definitions , but I still can't seem to pat about it.

For example, I cannot explain to myself how to do the following (but it looks like I can do this):

Exercise:

Define a function productIt :: [Int] -> Intthat gives the product of a list of integers and returns 1 for an empty list; why is this value chosen as the result for an empty list?

I (of course) came up with a solution:

productIt :: [Int] -> Int
productIt [] = 1
productIt (x:xs) = x * productIt xs

which is great for the question in the exercise. However, it still seems to me that I am having trouble ending the line.

Any ideas on how to think about it would be most valuable.

+3
3

:

- , . , , 1.

, , . Haskell :

  productIt [2,3,4]
= 2 * (productIt [3,4])
= 2 * (3 * (productIt [4]))
= 2 * (3 * (4 * (productIt [])))
= 2 * (3 * (4 * (1)))
= 2 * (3 * (4))
= 2 * (12)
= 24
+5

, 1 [], , .

productIt (x:xs) = x * productIt xs

, , :

productIt [] = 2                      -- line 1
productIt (x:xs) = x * productIt xs   -- line 2

:

productIt [1] = productIt (1:[])
              = 1 * productIt []    -- by line 2
              = 1 * 2               -- by line 1
              = 2                   -- WRONG!!!

, [1] 1.

+2

, , . :

  • ?
  • ?

, , .

1: - - . , , , , .

Re question 2: [n0, n1, n2,.. nk], - ( p) , , n0 * p.

:

productIt [] = 1

, productIt [] 1. ( , 1.), . productIt , . :

productIt (x:xs) = ... something?

. (x: xs) . , x , xs - . , , x xs . , Haskell.

, x, (, ) xs, ? , (x) (xs). ...

productIt (x:xs) = x * productIt xs

, yjerem , Haskell .

+1

All Articles