B. Pym
2024-09-13 01:31:52 UTC
Here's a more realistic example that illustrates exactly the
opposite point. Take the task of finding the sum of the square of a
bunch of numbers.
Umm first make a variable called sum
then set that variable sum to zero.
Then get the next number in the list,
square it,
add it to the old value of sum,
store the resulting value into sum,
then get the next variable,etc....
No they they think: sum up the square of a bunch of numbers.
(apply '+ (mapcar #'(lambda(x)(* x x)) numlist)).
Well..opposite point. Take the task of finding the sum of the square of a
bunch of numbers.
Umm first make a variable called sum
then set that variable sum to zero.
Then get the next number in the list,
square it,
add it to the old value of sum,
store the resulting value into sum,
then get the next variable,etc....
No they they think: sum up the square of a bunch of numbers.
(apply '+ (mapcar #'(lambda(x)(* x x)) numlist)).
sum (map (\x -> x ** 2) [1..10])
in Haskell, or
sum (map ((lambda x: x ** 2), range(1,11)))
sum([x*x for x in range(1,11)])
(use srfi-42) ;; sum-ec
(sum-ec (: n 11) (* n n))
===>
385
Less condensed:
(sum-ec (:range n 1 11) (* n n))
(loop for number in numberlist sum (expt number power))
Gauche Scheme(use srfi-42) ;; sum-ec
(sum-ec (:list n '(3 4 5 6)) (* n n))
===>
86