Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong association order in reduce example #73

Open
bo-tato opened this issue Feb 13, 2023 · 0 comments
Open

wrong association order in reduce example #73

bo-tato opened this issue Feb 13, 2023 · 0 comments

Comments

@bo-tato
Copy link

bo-tato commented Feb 13, 2023

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)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant