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