I wanted to thank you all. I did it! My head hurts, but now seeing how
simple it was, it hurts that it hurts.
;leap year
(define (leap-year? year)
(and (zero? (modulo year 4))
(or (not (zero? (modulo year 100)))
(zero? (modulo year 400))
)
)
)
;Months with days vector, beginning at an index of 0 since there is not 0th
month
(define leap-year-months (vector 0 31 29 31 30 31 30 31 31 30 31 30 31))
(define ordinary-year-months (vector 0 31 28 31 30 31 30 31 31 30 31 30 31))
(check-expect(days-in-month 2016 1)31)
(check-expect(days-in-month 2016 11)30)
(check-expect(days-in-month 2016 12)31)
(check-expect(days-in-month 1900 2)28)
(check-expect(days-in-month 2000 2)29)
(check-expect(days-in-month 2016 2)29)
(check-expect(days-in-month 2200 2)28)
(define (days-in-month x y)
(cond
[(leap-year? x) (vector-ref leap-year-months y)]
[else (vector-ref ordinary-year-months y)]
)
)
On Sun, Apr 19, 2020 at 2:39 PM Matt Jadud wrote:
> This is good. Your questions are good, and while the frustration/confusion
> is real, don't let it get you down. It's just part of the process. (That
> is, learning often involves confusion and frustration.)
>
> This might step it back too far, but see if this helps a bit. Your
> question about what you can put in the definition suggests that you're in a
> good place, but you've got a lot of ideas swimming around all at once.
> Let's try this.
>
> A function definition has a pattern to it.
>
> (define (__A__ __B__)
> __C__)
>
> When you look at a definition, you want to look for that pattern. Use
> DrRacket's highlighting to help you see the pieces if you need to. I find
> it handy all the time.
>
> "A" is the name of the thing you are defining, "B" is a parameter, and "C"
> is the body. There can, of course, be multiple parameters. Then, the
> pattern looks like:
>
> (define (__A__ __B1__ __B2__)
> __C__)
>
> assuming you have a function definition with two parameters.
>
> Making it a bit more concrete, you could have a function like this:
>
> ;; CONTRACT
> ;; number -> number
> (define (add-one n)
> (+ n 1))
>
> which, if you paste that into the "Interactions" area in DrRacket, and hit
> return, you will define the function. If you then type:
>
> (add-one 3)
>
> you will see that it evaluates to 4. That is because the value of '3' is
> bound to the parameter 'n', and then the body is evaluated (or "run") with
> n having the value 3. When you add one to three, you get four. Or, (+ 1 3)
> evaluates to 4.
>
> In the second pattern, you might have:
>
> ;; CONTRACT
> ;; number number -> number
> (define (add-nums a b)
> (+ a b))
>
> and then you could invoke or use that function as:
>
> (add-nums 3 5)
>
> Here, the value '3' is bound to 'a', 5 is bound to 'b', and then 'a' and
> 'b' are summed, giving you 8.
>
> Coming back around to your question, you're wondering "but where do I put
> the vector-ref?" Hopefully, along with the excellent questions you've been
> asked so far, the great answers that have been provided, and my potentially
> confusing message here, you're starting to think "perhaps I don't put the
> vector-ref in the parameters..."
>
> The contract for 'number-of-days-in-month' was given to you as
>
> number number -> number
>
> which means that 'number-of-days-in-month' takes two parameters. You need
> to give each of those parameters names. Or, in a pattern:
>
> ;; CONTRACT
> ;; number number -> number
> (define (number-of-days-in-month __B1__ __B2__)
> __C__)
>
> You need to give names to the two parameters (which you know what they
> have to be... and you've already said what those two parameters are, so
> naming them is something I suspect you can do). Then, you need to *do
> stuff* in __C__. Or, as John suggested, this is where you do the
> calculation that needs to be done... if you can write it out, you're a long
> way along to filling in __C__. When you're writing the body of the function
> (or __C__ in my pattern), I suspect you're going to need to use those two
> parameters---which represent a year and month---to do your calculation.
>
> Then, you would run that function this way:
>
> (number-days-in-month 2016 1)
>
> (which I just copy-pasted from your first email) and 2016 would be bound
> to the first parameter (whatever you called it), and 1 would be bound to
> the second. It would then run the body of the function, and the parameters
> would be bound to the values 2016 and 1.
>
> That may, or may not, have helped. But hopefully it helps you step back
> from the code, which may be looking like a whole bunch of symbols right
> now, and encourage you to look for the patterns that are there, and what
> the parts of the patterns mean.
>
> Keep asking questions,
> Matt
>
>
>
>
>
> On Sun, Apr 19, 2020 at 4:21 PM Suz Renae wrote:
>
>> The months vector is representing each month and how many days are in
>> that month.
>> (define months (vector 0 31 28 31 30 31 30 31 31 30 31 30 31))
>> 31 would be 1