0
votes

I am new to racket, and have recently started learning abstract list functions. I have to write a function (same-country airports code)that consumes a list of airports airports and a three-letter code that represents an airport. The function produces a list of all airport codes that has the same country as the given airport, sorted in ascending alphabetical order. If the code is not in the association list, the function would produce empty.

For example:

(define alist (list (list "YYZ" (list "Toronto" "Canada"))
                    (list "YWG" (list "Winnipeg" "Canada"))
                    (list "BGO" (list "Bergen" "Norway"))
                    (list "TRF" (list "Sandefjord" "Norway"))
                    (list "YUL" (list "Montreal" "Canada"))
                    (list "LHR" (list "London" "England"))
                    (list "YVR" (list "Vancouver" "Canada"))))

Then

(same-country alist "YUL") => (list "YUL" "YVR" "YWG" "YYZ")

Can someone please show me how to do this problem/give me a template? I'm not allowed to use any named helper functions in this problem, only lambda...I knew how to do it while using explicit recursion, but I don't understand abstract list functions well enough

1

1 Answers

0
votes

version 3

; please tell us which language you use (in front of code)
; please take effort before adding comment
; language:intermediate student with lambda
(define same-country-v2
  (λ (airport-ls airport-name)
    ((λ (ls name1 name2)
       ; result
       (sort
        (map (λ (p) (first p))
             (filter (λ (x)
                       (or (equal? name1 (first (second x)))
                           (equal? name2 (second (second x)))))
                     ls))
        string<=?))
     airport-ls
     ; city name
     (first ((λ (airports airport-name) (second (first (filter (λ (x) (equal? (first x) airport-name)) airports)))) airport-ls airport-name))
     (second ((λ (airports airport-name) (second (first (filter (λ (x) (equal? (first x) airport-name)) airports)))) airport-ls airport-name)))))

;;; TEST
(same-country-v2 airports "YUL")
(same-country-v2 airports "BGO")