0
僅供參考我正在使用Scheme
使用DrRacket
進行編程。方案(DrRacket) - 將兩個高階函數與Lambda結合定義
我試圖代碼,消耗了城市名稱和航班的列表,並返回城市(串)的列表定義daily-to
到航空公司有從給定的城市每日航班。
從我的理解這個定義需要同時使用filter
和map
,與lambda
一起,因爲這是一個問題,關於higher order functions
和lambda
。雖然我似乎沒有正確地做到這一點。
希望我能得到我的daily-to
定義的幫助,因爲它運行不正確。
這裏是我的程序:
(define-struct flight (from to frequency))
;; Example: (make-flight "Boston" "Chicago" 「daily」))
;; The frequency is one of 「daily」, 「weekday」 or 「weekend」.
(define myflights
(list
(make-flight "Boston" "Chicago" "daily")
(make-flight "New York" "Providence" "weekend")
(make-flight "Paris" "Moscow" "weekday")))
;; Signature: service-between?: String String ListOfFlights -> Boolean
;; Purpose: consumes two city names and a list of flights and determines
;; whether (true or false) there is a flight from the first city to the second.
;; Tests:
(check-expect (service-between? "Boston" "Chicago" myflights) true)
(check-expect (service-between? "New York" "Providence" myflights) true)
(check-expect (service-between? "Paris" "Moscow" myflights) true)
(check-expect (service-between? "Boston" "Providence" myflights) false)
;; Definition: service-between?
(define (service-between? city1 city2 alof)
(ormap
(lambda (str)
(and (string=? city1 (flight-from str))
(string=? city2 (flight-to str)))) alof))
;; Signature: daily-to: String ListOfFlights -> ListOfString
;; Purpose: consumes a city name and a list of flights and returns a list
;; of cities (strings) to which the airline has a daily flight from the given city.
;; Tests:
(check-expect (daily-to "Boston" myflights) (list "Chicago"))
(check-expect (daily-to "New York" myflights) empty)
(check-expect (daily-to "Paris" myflights) empty)
;; Definition: daily-to
(define (daily-to city alof)
(filter (map
(lambda (str)
(cond
[(and (string=? city (flight-from str)) (string=? "daily" (flight-frequency str))) (flight-to str)]
[else empty])) alof)))
預先感謝您!
你的問題是什麼? – 2014-12-07 08:36:22
希望我可以在我的'daily-to'定義獲得幫助,因爲它運行不正確。 @WillNess – BBladem83 2014-12-07 08:46:51
波士頓沒有每日航班*,波士頓每日航班*。 – uselpa 2014-12-07 09:27:30