-1

Let's say that we have the following expression (band 'x (bor 'y 'z)), where band and bor boolean structs that have arg1 and arg2.

If I want to change the variables 'x and 'y to 'a and 'b by deep recursion on the expression, how can I do that?

2
  • 2
    Please post the code you've written so far, including a sample input and the expected output, and pointing the specific parts that are giving you trouble. Don't forget to make the example executable, so we can test and fix it. Commented Mar 17, 2016 at 20:32
  • 1
    You might be interested in the answers to this question. There are many good ways to do this, including struct-copy, the lens library. Commented Mar 18, 2016 at 4:12

1 Answer 1

2

There is a special form for functionally updating just some fields in a struct which is very nice to use where you have a lot of fields:

(struct person (name age occupation) #:transparent) (define p (person "Per" 19 "Painter")) (define (change-occupation p new-occupation) (struct-copy person p [occupation new-occupation])) (change-occupation p "Programmer") ; ==> (person "Per" 19 "Programmer") 

Of course this is just a fancy way of writing:

(define (change-occupation p new-occupation) (person (person-name p) (person-age p) new-occupation)) 

Now I don't know the names of your two structs but you may need to make a generic accessor unless one is subtype of the other:

(define (change-first obj new-value) (if (band? obj) (band new-value (band-second obj)) (bor new-value (bor-arg2 obj)))) 

Or you can just have similar case analysis in your procedure.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.