Search code examples
structracketpretty-print

How to print a list that is a type in a struct


How can the list-of-terms list be referenced for printing?

I can do this:

(~a (student-id a-student)

This gives me the student ID, but I want a list of the enrolled terms.

If I try:

(~a (student-list-of-terms a-student))

I get the error: student-list-of-terms: undefined; cannot reference an identifier before its definition

The definition of student is:

(define a-student (student pidm list-of-terms list-of-events list-of-withdrawals list-of-courses date-lda date-wdrl)

Solution

  • In order for Racket to know what a student is you need to use struct to define what a student means. If a student struct has a list-of-terms field, you can then use student-list-of-terms to access the list of terms of a student.

    Here is an example:

    #lang racket
    (struct student (pidm list-of-terms list-of-events list-of-withdrawals
                          list-of-courses date-lda date-wdrl))
    
    (define a-student
      (student 42
               (list 'term1 'term2)
               (list 'event1 'event2)
               (list 'withdrawal1 'withdrawal2)
               (list 'course1 'course2)
               "a date"
               "another date"))
    
    (student-list-of-terms a-student)