如果谓词返回true,我试图在列表中执行一些操作。但谓词在命令行中作为输入给出,它是一个函数。让我举个例子。
(define (delete-rows table predicate)
do_something)
命令行看起来像这样。
(delete-rows student-table
(lambda (table row)
(eq? (get table row 'name) 'ali)))
=> '(students (name id gpa) (ayse 2 3.7))
提前感谢您的帮助。
答案 0 :(得分:1)
这是一个非常天真和低效的Racket实现,只是为了让你走上正轨:
(define (list-index e lst)
(- (length lst) (length (memq e lst))))
(define (get table row col)
(list-ref row (list-index col (second table))))
(define (delete-rows table pred)
(list* (first table)
(second table)
(filter (lambda (r) (not (pred table r))) (cddr table))))
然后
(define student-table '(students (name id gpa) (ali 1 2) (ayse 2 3.7) (zalde 3 5)))
(delete-rows student-table (lambda (table row) (eq? (get table row 'name) 'ali)))
=> '(students (name id gpa) (ayse 2 3.7) (zalde 3 5))