2

For fun I've created a bunch of functions in Racket that create and combine other functions.

Now I've defined a recursive function in terms of them:

(define (my-flatten2 struct)
  ((<?> pair?
       (<+> append <m>
            (</> map my-flatten2)
            (<Y> car cdr))
       list)
   struct))

I tried this first, but it didn't work (it gave me a can't reference identifier before its definition error):

(define my-flatten-error
  (<?> pair?
       (<+> append <m>
            (</> map my-flatten-error)
            (<Y> car cdr))
       list))

Can anyone explain why it didn't work, and whether or not there is a way to fix it.

For the record

  • <+> is compose
  • <Y> is create a function that applies a list of functions to one arg
  • </> is partially apply
  • <?> is choice

1 Answer 1

3

Racket is an eager language; it evaluates arguments before passing them. So, your code does not work for the same reason that

(define p (add1 p))

doesn't work.

In this case, assuming that my-flatten error does turn out to be a function, it might be fairly easy to solve your problem just by delaying the evaluation of my-flatten-error:

(define my-flatten-error
  (<?> pair?
       (<+> append <m>
            (</> map (lambda args 
                       (apply my-flatten-error args)))
            (<Y> car cdr))
       list))

You can also hide the lambda using a macro:

(define-syntax delay-fn
  (syntax-rules ()
    [(delay-fn f) (lambda args (apply f args))]))


(define my-flatten-error
  (<?> pair?
       (<+> append <m>
            (</> map (delay-fn my-flatten-error))
            (<Y> car cdr))
       list))

... if you find that visually more pleasing.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, that makes a lot of sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.