In class we wrote an interpreter for a made up language (lanG) using the following racket structs.
(struct const (n))
(struct bool (b))
(struct join (e1 e2))
(struct if-then-else (b e1 e2))
(struct negate (e))
(struct add (e1 e2))
(struct multiply (e1 e2))
(struct head (e)) ;returns the head of the list
(struct tail (e)) ;returns the tail of the list
(struct biggerThan (e1 e2))
Macros for this language are defined as racket functions. A simple example would be:
(define (threeTimes x)
(add x (add x x)))
And using it would look like:
(lanG (threeTimes (const 3)))
which would produce an answer:
(const 9)
Now to my problem. There was a task on the exam where we had to write a macro sumAtEvenAndOdd, which would sum a list of lanG constants,
made with the join struct and return a pair of values consisting of the sum of elements at even positions and the sum of elements
at the odd positions.
An example of such a list would be:
(join (const 3) (join (const 2) (const 5))) ;lanG list with no null at the end
And its result would be:
(join (const 2) (const 8))
I tried solving this by converting the list into a racket list, ziping the positions with the elements, filtering the odd or even elements out of the list, and producing the pair using the sums of those lists. This works but I am overcomplicating. Professor said the solution is about 5 lines long.
I thank you in advance for all your help.