[racket-users] Question on test failures sorting a BTS

2020-04-26 Thread Kristina Marie
Hello,
I am trying to use HtDF to create a function to sort a Binary Search Tree. 
I am in an intro class so this may be a simple question. I did the 
following:

;define-struct a node
(define-struct node (left value right))
;BTS leafs
(define a-node(make-node null "A" null))
(define b-node (make-node null "B" null))
(define e-node (make-node null "E" null))
(define h-node (make-node null "H" null))
(define i-node (make-node null "I" null))

;BTS internal
(define c-node (make-node a-node "C" b-node))
(define j-node (make-node h-node "J" i-node))
(define k-node (make-node j-node "K" null))
(define f-node (make-node e-node "F" null))
(define g-node (make-node f-node "G" k-node))

;Root
(define root (make-node c-node "D" g-node))

; Purpose: sort a binary tree
; Signature: node -> list
; Examples:
(check-expect (bst-sort null)(list))
(check-expect (bst-sort a-node)(list "A"))
(check-expect (bst-sort j-node)(list "H" "I" "J"))
(check-expect (bst-sort root)(list "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" 
"K"))

;Code
(define (bst-sort nod)
  (cond
[(null? nod)(list)]
[(and (null? (node-left nod)) (null?(node-right nod))) (list(node-value 
nod))]
   [else
   (string-append (bst-sort (node-left nod) (list (node-value nod)) 
(bst-sort (node-right nod
   ]
   )
 )

I keep getting this test failure:
bst-sort: expects only 1 argument, but found 3

It has to due with the two last check-expects failing because of the 
highlighted line. 

I ran this same code with integers and removed the string from append and 
it passed. Any advice?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/e5e05ebe-87ed-4d49-8a18-d3b58bae4e38%40googlegroups.com.


Re: [racket-users] New to racket, help with coursework question

2020-04-19 Thread Kristina Marie
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