How can i use sets methods to sort sets and create sequences?

I am asking this question as i am struggling to use sets and i would like to better understand how to tackle this goal .My goal here is to to

  1. write a function which sorts the union of all my three sets in alphabetical order.
  2. using the same function,i want to define a count sequence - meaning as an example,i have 12 elements as whole in my union of the sets,so then using these set methods set-count, set-first, set-rest,i want the letters to be counted in this order,so "a" belongs to first and "b" to second.so essentially racket is reading each letter as a number e,g "a" as 1 and so on.
  3. lastly,depending on which two letters i choose from my function below - the set methods should be applied to check the number of each letter.e,g if i choose "a" and "d", then it should be 1 and 4.i also would like know how to create a sequence where there +5 is added depending on the user selection,so from "a" to "b" is 5 and from "a" to "c" is 10,this is really a sequence of adding +5 to reach the intended letter.As a final output (example),i would like to see something like this ("your journey between a and c is 10 mins")

(this function is a combination of two function applied in this one function called journey" this is where i want my function to run.)

 (define journey (λ (a b ) #this is the function where i want to compute the above.
  (exsists1 a) (exsists2 b)))


(printf "enter your current posistion")
(define exsists1 (λ (a)
                  (cond
                
                    ((empty? a) (error " you need to enter a starting location"))
                    ((not (or (set-member? line1 a) (set-member? line2 a) (set-member? line3 a))) (error "enter a location which exisits"))
                    (else "enter your final destintation"))))

(define exsists2 (λ (b)
                  (cond
                
                    ((empty? b) (error " you need to enter a final location"))
                    ((not (or (set-member? line1 b) (set-member? line2 b) (set-member? line3 b))) (error "enter a final location which exisits"))
                    (else "plan your journey"))))



here is my defined sets

(define line1 (set "a" "b" "c" "d" "e"))

(define line2 (set "f" "g" "c" "h" "i"))

(define line3 (set "k" "i" "l" "m" "e"))

Sets, by definition, are not sorted and they contain elements, not mappings from elements to values. You might instead try lists (which are sorted) or hashes (which are not sorted but do imply a mapping).

If you want to print the values of a hash in order, you can use hash-keys, sort the keys, and then use hash-ref to get the value, or you can use hash-values directly, although that loses the connection to the key.

If you want a list that contains mappings then you can use a nested list such as (list (cons "a" 1) (cons "b" 2)), which will give you '(("a" . 1) ("b" . 2)), thereby making it easy to get them in order and use the mapping. (Use car and cdr to get the key and value respectively. Do not use second on a cons pair.) Check the #:key argument on sort if you need to run the list in something other than front-to-back order.)

3 Likes

(Sorry to sound like clippy, here)

It sounds like you're trying to create a function. The design recipe in HtDP outlines a concrete set of steps for this process, starting with this:

  1. data definitions: definitions of new kinds of data required by your problems
  2. choose a name for your function, write down the kinds of arguments it accepts, and the kind of argument it returns. Write the first line of your function
  3. Write examples of how you might call your function, and what it might return.

Let's start with those. For more details, see www.htdp.org

4 Likes