B
B
BranchInCode2019-09-14 23:22:03
Lisp
BranchInCode, 2019-09-14 23:22:03

How to implement multiplication of integer list elements in Lisp?

The task is: "Set up a recursive function that calculates the product of integers from a list." , i.e. as I understand it, if the list does not contain integers (any letters and left characters do not count), then they do not count as a product ...
How would I implement this in JS. but this language is too heavy... help to implement this task in lisp

function Almost(arr){
    if(arr.length === 0) return 1;
    else if(arr[arr.length - 1] % 1 !== 0){
      arr.splice(arr.length - 1, 1); 
      return Almost(arr);
    }
    else {
        return arr.pop() * Almost(arr);
    }
}
console.log(Almost([1,2,3,1.5,4,5,11.2,6])) // 720, т,е 1,5 - не учитывается

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BranchInCode, 2019-09-17
@Vetka_in_code

(defun len (x)
(cond ((= '0 (list-length x)) '1)
((/= 0 (rem (first x) 1)) (* 1 (len (cdr x))))
( t (* (first x) (len (cdr x))))))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question