Kyle
Kyle

Reputation: 584

Handling undefined procedures in student Racket code

What's the best way to handle undefined procedures in student Racket code? For example, if I give them a homework assignment with a bunch of requested Racket procedures, and they don't do foo (or misspell it), what is the best way for me to deal with that in Racket? I want to be able to check and grade the rest of it. Is there any way for me to sub in a trivial procedure that returns a string indicating that it's not defined?

E.g. for the first file, I'm trying to load their code with (require "project0.rkt")

Upvotes: 0

Views: 54

Answers (1)

Kyle
Kyle

Reputation: 584

I think I got what I wanted. If I'm expecting student's file, project0.rkt to have:

(define (add5 x)
  (+ x 5))

But it might not include it or it may be misspelled.

We can test it with the following code:

#lang racket

(define stu-file-name "project0.rkt")

;(try-load-proc proc) returns the procedure from the student file, if it exists, 
;otherwise it displays the error that it doesn't exist and returns 0
(define (try-load-proc proc)
  (with-handlers ([exn:fail? (lambda (e) (begin
                                           (displayln "Procedure doesn't exist.  Misnamed?")
                                           (displayln exn)
                                           0))])
                 (dynamic-require stu-file-name proc)))

;use try-load-proc to test add5.
(if (equal? 7 (apply (try-load-proc 'add5) 2))
  (displayln "add5 correct!")
  (displayln "add5 incorrect!"))

This should print whether add5 is correct, incorrect, or doesn't exist.

Upvotes: 0

Related Questions