Search code examples
lambdafunctional-programmingschemefunction-compositionmit-scheme

Getting error in scheme "The object (unquote f), passed as an argument to identifier->symbol, is not an identifier."


I am trying to implement a function composex that given a list of functions funcs returns a function that is a composition of all functions in funcs So,

input -> [f1 f2 f3 ...]
output -> f'(x) = f1(f2(f3( ... (x))))

The code snippet I have is:

(define (reducex initial f arr)
  (if (null? arr)
    initial
    (reducex (f initial (car arr))
      f
      (cdr arr)
    )
  )
)


(define (abst x)
  (if (< x 0)
    (- x)
    x
  )
)

(define (mult2 x)
  (* x 2))

(define (composex funcs)
  (lambda (x)
    (reducex '()
      (lambda (ini, f) (f x))
      funcs)
  )
)

(define absmult2 (composex (cons abst (cons mult2 '()))))
(absmult2 2)
(absmult2 -2)

The error I get is in composex

;The object (unquote f), passed as an argument to identifier->symbol, is not an identifier.
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.

I am using mit-scheme for execution.


Solution

  • Easy fix, IIUC: you put a comma in the parameter list for your inner lambda. Take it out.

    Comma has a special meaning in Scheme; it allows you to "escape" from a quasiquote. In this case, you're not using quasiquote, so a comma here doesn't make any sense in Scheme.

    This is a pretty terrible error message, though.