2

With some test procedures:

#lang racket

(define (foo x) (+ x 1))
(define (bar x) (* x 2))
(define (baz x) (+ x 3))

I can "manually" use compose to get the right result:

((compose foo bar baz) 1)    ;; works

...but is there a way to use compose with a list? The closest I can get is a quoted list, and I'd prefer not to use eval if I don't have to.

(define test-funcs '(foo bar baz))
((compose test-funcs) 1)     ;; expected: procedure? given: '(#<procedure:foo> #<procedure:bar> #<procedure:baz>)
((compose . test-funcs) 1)   ;; #%app: bad syntax
`((compose . ,test-funcs) 1) ;; almost: '((compose foo bar baz) 1)
Alex Shroyer
  • 3,499
  • 2
  • 28
  • 54
  • 4
    Yes, with `(apply compose test-funcs)`. However, you also have another problem, with using `quote` to create a list, where you should use `list` instead. So `(define test-funcs (list foo bar baz))` and `(apply compose test-funcs)`. If you're confused about why, see [_What is the difference between quote and list?_](https://stackoverflow.com/questions/34984552/what-is-the-difference-between-quote-and-list) – Alex Knauth Sep 22 '19 at 17:14
  • That's perfect, I'd accept it as an answer if I could. Thanks for the other help, very enlightening. – Alex Shroyer Sep 22 '19 at 17:33

1 Answers1

1

I know two options to solve such problem:

1) ((apply compose (list foo bar baz)) 1)

2) ((eval `(compose foo bar baz)) 1)

Nicklaus Brain
  • 884
  • 6
  • 15