To find the product of elements in a list in scheme

Asked 2 years ago, Updated 2 years ago, 237 views

I would like to define a list like ((item number a price a number a number a purchase a) (item number b price b number b purchase b) ...) in scheme and create a function to find the total price of the item such as a * purchase a + price b * purchase b + ...
How should I write a function?

>(display(count-price'(((110010)(22505)(35003))))
#langracket
(define(seki xy)(* xy))

(define(count-price item)
  (if(null?item)
      `()
      (apply+seki(map cadritem)(map last item)))

scheme

2022-09-30 21:54

1 Answers

Change the parameters of the function seki to a list.

入力 The input value assumes a list of positive integers with 3 elements, so you will need to add an error check.

(define(sekilst)(apply*(cdrlst)))

(define(count-price item)
  (if(null?item)
      `()
      (apply+(map seki item)))

(display(count-price'((110010)(22505)(35003))))
=>
3750


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.