tryfsharp The Age Computation Expression Builder problem

Answered tryfsharp The Age Computation Expression Builder problem

  • Monday, January 28, 2013 10:39 AM
     
     

    Hi,

    I am starting to learn f# (just for fun :) and I am going through the learn section on tryfsharp.com site. While doing the advanced programming course I got to The Age Computation Expression Builder.

    What is says there "If you play with the willBeThere function, you'll find out that you will only be considered PossiblyAlive if the total age is between 0 and 120."

    But when I try

    willBeThere 38 100

    the result is

    val it : Age = PossiblyAlive 138

    What am I missing here? And also how is the (+) function invoked through the builder?

    Regards,

    Jarek

All Replies

  • Monday, January 28, 2013 4:36 PM
    Moderator
     
     
    You're right - the text is wrong; the result will be PossiblyAlive as long as each age is between 0 and 120, not if the total is.  As to how the (+) operator is invoked, the code in the "de-sugared form" section shows how the computation expression is evaluated - is there some particular aspect that's confusing?
  • Monday, January 28, 2013 4:50 PM
     
     Answered

    You are right, the example simply checks the two arguments and not the sum, the proper version is:

    let willBeThere a y =
      age {
        let! current = PossiblyAlive a
        let! future = PossiblyAlive (y + a)

        return future
      }

    We will fix the sample ASAP.

    • Marked As Answer by Jarek Beczek Tuesday, January 29, 2013 2:35 PM
    •  
  • Tuesday, January 29, 2013 2:36 PM
     
     

    Thank you for the answer. Now the example makes perfect sense.