I am new to programming with scheme and I am trying to make a program in which I have a list of students with their names and their grades, my goal is to build two different lists, one that contains the students with passing grades and another that contains the students with failing grades, so far I have managed to build the list of approved students but I have not been able to build the list of failed students, I hope you can help me solve this.
At the moment it carries this code segment that builds the list of approved students:
#lang racket
(define students'((Walter 57)
(Susan 56)
(Dilan 99)
(Varenka 10)
(Tony 83)
(Joshua 68)
))
(define (status students)
(if(null? students)
'()
(if (>= (cadar students)70)
(approvedList (caar students) (status (cdr students)))
(status (cdr students))
)
)
)
(define (approvedList name students)
(cons name students)
)
CodePudding user response:
This seems like a homework- are you allowed to use filter
? If yes, you can just filter given list twice:
(define students '(("Walter" 57)
("Susan" 56)
("Dilan" 99)
("Varenka" 10)
("Tony" 83)
("Joshua" 68)))
> (filter (lambda (student) (>= (second student) 70))
students)
'(("Dilan" 99) ("Tony" 83))
> (filter (lambda (student) (< (second student) 70))
students)
'(("Walter" 57) ("Susan" 56) ("Varenka" 10) ("Joshua" 68))
or even use group-by
:
> (group-by (lambda (student) (>= (second student) 70))
students)
'((("Walter" 57) ("Susan" 56) ("Varenka" 10) ("Joshua" 68))
(("Dilan" 99) ("Tony" 83)))
Otherwise, you have to write some filter
variant, which can look like this (note that I used cond
instead of nested if
s):
(define (my-filter fn students)
(cond ((null? students) '())
((fn (car students))
(cons (car students)
(my-filter fn (cdr students))))
(else (my-filter fn (cdr students)))))
Tests:
(my-filter (lambda (student) (>= (second student) 70))
students)
(my-filter (lambda (student) (< (second student) 70))
students)
CodePudding user response:
#lang racket
(define students '((Walter 57)
(Susan 56)
(Dilan 99)
(Varenka 10)
(Tony 83)
(Joshua 68)))
;; no filter and group-by allowed to use
;; then you are only allowed to use recursion
;; well, no lambda allowed to use - can't be.
;; you use `define` for functions - and they contain a syntactically hidden lambda.
;; anyway, let's say you are not allowed to use functions as arguments
;; (which are basically the lambda in the other answer by @MartinPuda)
;; so lets use lambda's only by defining functions directly -
;; I would do:
(define (grade-greater-than? student-entry limit)
;; Is the grade of the student in the student-entry greater than the limit given?
(>= (second student-entry) limit))
(define (divide-students-by students limit (passed '()) (failed '()))
(cond ((empty? students) (list (reverse passed) (reverse failed)))
((grade-greater-than? (first students) limit)
(divide-students-by (rest students) limit (cons (first students) passed) failed))
(else (divide-students-by (rest students) limit passed (cons (first students) failed)))))
(divide-students-by students 70)
;; => '(((Dilan 99) (Tony 83))
;; ((Walter 57) (Susan 56) (Varenka 10) (Joshua 68)))
Consing the result into lists and then at the end reverse
-ing them to get them in order of their input is a very typical lisp idiom.