LispLang / lisplang.github.io

Common Lisp homepage
https://lisp-lang.org/
160 stars 30 forks source link

wrong association order in reduce example #73

Open bo-tato opened 1 year ago

bo-tato commented 1 year ago

thanks for making polished website with good material to get started. Sorry if this is a beginner misunderstanding and not a real issue, but I think the reduce example is the wrong order, on https://lisp-lang.org/learn/lists

(reduce #'(lambda (a b)
                     (* a b))
                 (list 10 20 30))

The above is equivalent to (* 10 (* 20 (* 30)))

Those parens would mean reduce is like a right fold, calling f (first element) (reduce f (rest of list)) where it seems it is a left fold, as the next example shows, calling f (first element) (second element), then f (result of that) (third element)... To double check I switched it to division instead of multiplication so that the associativity matters:

(reduce #'(lambda (a b)
                     (/ a b))
                 (list 10 20 30))

I get 1/60 like in (/ (/ 10 20) 30) so I'm pretty sure the explanation should be (* (* 10 20) 30) and not (* 10 (* 20 (* 30)))