Giter VIP home page Giter VIP logo

Comments (3)

milesfrain avatar milesfrain commented on August 28, 2024 1

Thanks a bunch for reporting this and providing an explanation of what's happening.
PR to fix in #97

from purescript-book.

JamieBallingall avatar JamieBallingall commented on August 28, 2024

So the example has been updated for better style and to remove warnings. However, this issue still occurs and the parenthesis still fix it. While this is a simple fix, I feel like it would be nice to provide an explanation. Sadly, I don't myself understand it.

from purescript-book.

thomashoneyman avatar thomashoneyman commented on August 28, 2024

The example isn't correct; it needs the parentheses because of precedence rules (the function is being applied to acc and then we add one to the result).

This code:

length' (fromMaybe [] $ tail arr) acc + 1

is parsed like this:

(length' (fromMaybe [] $ tail arr) acc) + 1

instead of the way it's intended from my reading of the example, which is:

length' (fromMaybe [] $ tail arr) (acc + 1)

Just to check this, I threw it into try.purescript.org and looked at the output, and indeed the version without parenthesis emits this JS:

return length$prime(Data_Maybe.fromMaybe([  ])(Data_Array.tail(arr$prime)))(acc) + 1 | 0;

which is adding 1 to the result of length' after it is called on acc, which isn't going to fire the TCO (or increment the accumulator).

So I'd suggest just fixing the example without further explanation -- it's not that this is a tricky case, it's just that the example is incorrect.

from purescript-book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.