The code I am trying to execute:

hh[x_, y_] := x + y;

hh[{3, 4}]

The expected output:

7

However, on output I get hh[{3, 4}].

Why does my function not produce the expected output?

=================

=================

2 Answers

2

=================

Description

The reason why your function doesn’t execute as you’d expect is due to it expecting two arguments whilst you pass a single argument of type List containing two elements.

Your function views it as hh[{3,4},y_]. Although, x parameter is passed successfully; y parameter is not available.

Example:

hh[3,4]

Output:

7

This works:

hh @@ {3, 4}

7

This as well:

myList = {3, 4}

hh @@ myList

7

See:

How to | Work with Lists

Lists are at the core of the Wolfram Language. These “How tos” give step-by-step instructions for common tasks related to creating and manipulating lists.

Applying Functions to Lists

Many computations are conveniently specified in terms of applying functions in parallel to many elements in a list. The Wolfram Language provides a suite of elegant functional programming constructs for doing this. In the Wolfram Language, mathematical functions are automatically taken to be “listable”, so that they are always applied to every element in a list.

Operations on Scalars, Vectors, and Matrices

Most mathematical functions in the Wolfram Language are set up to apply themselves separately to each element in a list. This is true in particular of all functions that carry the attribute Listable.

A consequence is that most mathematical functions are applied element by element to matrices and vectors.

Listable

Listable

is an attribute that can be assigned to a symbol to indicate that the function should automatically be threaded over lists that appear as its arguments.