To define apply in Python:
def apply(f, arg_list):
return f(*arg_list)
Scheme example:
(define (pythagoras a b)
(sqrt (+ (* a a) (* b b))))
(apply pythagoras '(3 4)) ; Returns: 5
Analogous Python example:
import math
def pythagoras(a, b):
return math.sqrt(a**2 + b**2)
apply(pythagoras, [3, 4]) # Returns: 5.0
Side notes (based on your apparent interest in such things)
Python's eval
:
eval('pythagoras(3, 4)')
Whereas in Scheme:
(eval '(pythagoras 3 4))
(eval "Hello")
(eval `(,(if (> x 0) + -) 100 ,(pythagoras 3 4)))
As you can see, Python's eval
requires you to fiddle with strings, bytes or "code objects". Scheme's eval
takes in Scheme. See: What exactly does homoiconicity mean?
You may be interested in reading about The Metacircular Evaluator (from Structure and Interpretation of Computer Programs, chapter 4, Metalinguistic Abstractions) to see the meaning of Scheme's eval
and apply
.
Note that the use of eval
in Python is heavily discouraged (and if you really need to use it, consider ast.literal_eval instead). In both languages, you are generally advised to avoid eval
if your problem can be solved using other methods.