[plt-scheme] Re: Why would the order of functions matter?

From: Marco Morazan (morazanm at gmail.com)
Date: Thu Nov 20 09:20:07 EST 2008

Here's little more of the puzzle after some more investigating:

Student's file:

<some previous code>
...
;;; insert-everywhere/in-all-words: symbol a-low -> low
;;; to insert a symbol (s) between all letters and at the beginning
;;; and the end of all words of a-low
(define (insert-everywhere/in-all-words s a-low)
;;; (s) is a symbol representing the letter to be inserted in a-low
;;; (empty? a-low) is true if a-low is empty
;;; otherwise,
;;; (cons? a-low) is true if a-low is a cons
;;; (first a-low) is the first word in a-low
;;; (rest a-low) is the list containing the a-low without (first a-low)
;;; (insert-everywhere/in-all-words s (rest a-low)) is the low
;;; with s inserted in (rest a-low) according to the contract
  (cond
    [(empty? a-low) (list s empty)]
    [else (append (insert-everywhere-in-a-word s (first a-low))
                  (insert-everywhere/in-all-words s (rest a-low)))]))

;;; EXAMPLE
(= (insert-everywhere/in-all-words 'd (list (list 'l  'i  'k  'e)
                                            (list 'p 'o)
                                                  (list 'a 'n)))
  (list (list 'd 'l 'i 'k 'e)
        (list 'l 'd 'i 'k 'e)
        (list 'l 'i 'd 'k 'e)
        (list 'l 'i 'k 'd 'e)
        (list 'l 'i 'k 'e 'd)
        (list 'd 'p 'o)
        (list 'p 'd 'o)
        (list 'p 'o 'd)
        (list 'd 'a 'n)
        (list 'a 'd 'n)
        (list 'a 'n 'd))true)
...
<some post code>

Produces this error:

Welcome to DrScheme, version 4.1.1 [3m].
Language: Beginning Student with List Abbreviations; memory limit: 128
megabytes.
reference to an identifier before its definition: insert-everywhere-in-a-word
>

If the following is commented out:

;;; EXAMPLE
(= (insert-everywhere/in-all-words 'd (list (list 'l  'i  'k  'e)
                                            (list 'p 'o)
                                                  (list 'a 'n)))
  (list (list 'd 'l 'i 'k 'e)
        (list 'l 'd 'i 'k 'e)
        (list 'l 'i 'd 'k 'e)
        (list 'l 'i 'k 'd 'e)
        (list 'l 'i 'k 'e 'd)
        (list 'd 'p 'o)
        (list 'p 'd 'o)
        (list 'p 'o 'd)
        (list 'd 'a 'n)
        (list 'a 'd 'n)
        (list 'a 'n 'd))true)

The error disappears. Should the above example (that does not use
check-expect and that makes improper use of =) cause such an error to
appear? I can see that insert-everywhere/in-all-words is being called
before insert-everywhere-in-a-word has been defined in the file. Is
that the expected/wanted behavior for a student language?

It may be useful to collect all definitions before evaluating any
application expressions in the student languages. Is something to
think about or is there a reason this would be undesirable?

-- 

Cheers,

Marco


Posted on the users mailing list.