I am working on a project to get us used to the plai-typed language for use in a programming languages course. I have been stuck on one question that's really bugging me (finished all the rest with no problem). We are to take this datatype definition :
(define-type Tree
[leaf (val number?)]
[node (val number?)
(left Tree?)
(right Tree? ) ] )
and then "Implement the function 'scaled', which takes a tree and returns a tree that has the same shape, but with all the values multiplied by the given scale.
Example: (scaled (node -5 (leaf 6) (leaf -7)) 2) should produce (node -10 (leaf 12) (leaf -14))"
So basically I just need to muliply all those values by a given value, in this example, 2.
Here is what I have so far after trying out different ways:
(define (scaled [ t Tree?] [n number?] )
(type-case Tree t
[leaf (val) val]
[node (val left right) ( * val (scaled left) (scaled right) n ) ] ) )
I based it on previous code that I created for the other parts of the assignment that I got right. The issue is, it multiplies all the numbers and results "succesfully" with the solution of 420 instead of mutlplying each value individually. I know this has to do with my placement of 'n' but I tried so many different ways with no luck. If anyone has any helpful hints/tips/solution it would be greatly appreciated.
Thanks!