Group Abstract Group Abstract

Message Boards Message Boards

0
|
5K Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Evaluate the following function f[x__Integer]:=x[[2]]; f[{7,8}]?

Doesn't x__Integer mean that the allowed arguments for f are just the lists of integers? (The Mathematica Book says so on p. 273 sect. 2.3 .8).

Or, am I really forced to write

f[x_ /; VectorQ[x, IntegerQ]] := x[[2]]

or for in a generalizable form:

f[x_ /; VectorQ[x, (Head[#] == Integer)& ]] := x[[2]] ?

As it looks to me, it would be useful if the Documentation Center would contain a Tutorial which collects the usual idioms for argument specifications of functions including user-defined heads and default arguments.

PS: My system is Mathematica 11.1 under Windows 10. See the attached file umTest1.nb for experimentation.

Attachments:
POSTED BY: Ulrich Mutze
2 Replies

Neil,

Thank you very much for your perfect answer.

And, also the Mathematica Book speaks of sequences and not of lists, in accordance with your answer.

Regards,

Ulrich

POSTED BY: Ulrich Mutze

Ulrich,

Your list of integers has Head of List, not Head of Integer so your function does not match a list of integers. Your definition should look like this:

f[{x__Integer}] := {x}[[2]]

or (a bit more cryptically:

f[{x__Integer}] :=  ##2 &[x]

The reason is that you have a list of integers which you can match with

{x__Integer}

x will take on the value of a Sequence of Integers (not a list). Since it is a sequence and not a list you can either make it a list by doing {x} and then process it. or you can treat it as a sequence (second example).

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard