Simply Scheme: Chapter 6, Exercise 6.13


This is a free sample of one Scheme exercise that I did so that you can see what some of my solutions look like.

You can find most answers to Simply Scheme on the web for free. A reason why you should pay $2 to get my solutions is that I document my thinking process, my mistakes and how I solved them.

From chapter 4 and onwards I write out my thinking process and mistakes.
For chapter 1-3 there are only my solutions.

This post has been published on my blog as well. The formatting on this post differs from my other Simply Scheme posts found under $2 tier.

From blogpost:
---

My main goal right now is just to learn to code. My main goal right now is not to write the best code that I possibly can.

By first focusing on just learning to code in Scheme I can focus on only that and not on writing the best possible code. This helps me to avoid overreaching (learn to code and write awesome code at the same time) and lets me improve one step at a time. Once I get better at fundamental coding in Scheme, I can focus on writing better code.

Below is how I dealt with exercise 6.13, ch 6, in Simply Scheme. My thinking process, my mistakes and how I solved them.

All my Scheme exercises can be found on Subscribestar in the $2 tier.
From chapter 4 and onwards I write out my thinking process and mistakes.
For chapter 1-3 there are only my solutions.


6.13 Write a better greet procedure that understands as many different kinds of names as you can think of:

> (greet ‘(john lennon))
(HELLO JOHN)

> (greet ‘(dr marie curie))
(HELLO DR CURIE)

> (greet ‘(dr martin luther king jr))
(HELLO DR KING)

> (greet ‘(queen elizabeth))
(HELLO YOUR MAJESTY)

(greet ‘(david livingstone))
(DR LIVINGSTONE I PRESUME?)
I will just focus on the examples given in the exercise. I will not come up with new kinds of names.

Thinking process:
1. A title will always be in front of a persons actual name – eg “dr“, “queen“, “king“.
2. For “dr” greet as “dr” followed by last name. But only if last name is not “jr” / “sr“. If last name is equal to “jr” / “sr” then use the second last name.
3. For “queen” / “king” greet as “your majesty“.
4. Don’t know how to presume if someone is a dr without making a list of all ppl that are dr.


Step 1:

Test the core concept.

(define (greet name)
    (if (equal? (first name) 'dr)
        (se '(hello dr) (last name))
        #f))
Works.



Step 2:

Add more “if” procedures. Add “king” / “queen“.

(define (greet name)
    (if (equal? (first name) 'dr)
        (se '(hello dr) (last name))
        (if (equal? (first name) (member? '(king queen)))
            (se '(hello your majesty))
            (se 'hello (first name)))))
Did not work.
member?” lacking one of its two arguments.

(define (greet name)
    (if (equal? (first name) 'dr)
        (se '(hello dr) (last name))
        (if (equal? (first name) (member? (first name) '(king queen)))
            (se '(hello your majesty))
            (se 'hello (first name)))))
Did not work.
Prints “(hello queen)


Test 1:

Effort to find the problem.

(define (greet name)
    (if (equal? (first name) (member? (first name) '(king queen)))
            (se '(hello your majesty))
            #f))
Prints “#f


Test 2:

(member? 'queen '(king queen))
Prints “#t

I think my mistake is that I am mixing the wrong domains.
member?” is a predicate (a function that returns either #t or #f). Thus “(equal? (first name) (member? (first name) ‘(king queen)))” would be evaluated as (step by step evaluation):
1. (equal? (first name) (member? (first name) ‘(king queen)))
2. (equal? ‘queen (member? ‘queen ‘(king queen)))
3. (equal? ‘queen #t)
4. #f

My “king” / “queen” code is unnecessarily confusing. I can just eliminate the “equal?” part.

(define (greet name)
    (if (equal? (first name) 'dr)
        (se '(hello dr) (last name))
        (if (member? (first name) '(king queen))
            (se '(hello your majesty))
            (se 'hello (first name)))))
Works.



Step 3:

Fix “jr” / “sr“.
jr” / “sr” only matters for ppl that are “dr” as “jr” / “sr” is at the end of the name.

(define (greet name)
    (if (equal? (first name) 'dr)
        (se '(hello dr) (last name))
        (if (and (equal? (first name) 'dr) (member? (last name) '(sr jr)))
            (se '(hello dr) (last (bl name)))
            (if (member? (first name) '(king queen))
                (se '(hello your majesty))
                (se 'hello (first name))))))
Did not work.
Prints “(hello dr jr)” for “> (greet ‘(dr martin luther king jr))“.

It is because the first “if” procedure is true. I need to change places with the first and the second procedure. That should fix it.

(define (greet name)
    (if (and (equal? (first name) 'dr) (member? (last name) '(sr jr)))
            (se '(hello dr) (last (bl name)))
            (if (equal? (first name) 'dr)
                (se '(hello dr) (last name))
                (if (member? (first name) '(king queen))
                    (se '(hello your majesty))
                    (se 'hello (first name))))))
Works.


I don’t know how to do the following without making a list of all ppl who could be considered doctors in advance.

> (greet ‘(david livingstone))
(DR LIVINGSTONE I PRESUME?)
A list of that would be something like:

(if (equal? name '(david livingstone))
        (se 'dr (last name) '(I presume?))
        ...))
Then it would look something like this:

(define (greet name)
    (if (and (equal? (first name) 'dr) (member? (last name) '(sr jr)))
            (se '(hello dr) (last (bl name)))
            (if (equal? (first name) 'dr)
                (se '(hello dr) (last name))
                (if (member? (first name) '(king queen))
                    (se '(hello your majesty))
                    (if (equal? name '(david livingstone))
                        (se 'dr (last name) '(I presume?))
                        (se 'hello (first name)))))))
Works.