Re: a few questions
- To: mathgroup at smc.vnet.net
- Subject: [mg27936] Re: [mg27884] a few questions
- From: Jean-Marie THOMAS <jmt at agat.net>
- Date: Sat, 24 Mar 2001 00:49:00 -0500 (EST)
- References: <200103230931.EAA11537@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
> 1) Are the funkcions to tell whether the argument is a number/list, > NumberQ and ListQ? You can write such a function : f[a_, b_] := Print["Head of first argument : ", Head[a], " ; Head of second argument : ", Head[b]] > 2) How do you get the header ( a[b,c] ---> a ) ? Head[a[b,c]] will return a > 3) Does Join work like that: > Join[{1,2,3},4,{5},{6,{7,8}},9] -> {1,2,3,4,5,6,{7,8},9} > or do all arguments have to be lists? Join joins lists but also expressions that have the same head : Join[a[b,c],a[d,e]] will return a[b,c,d,e] Join[a[b,c],a[{d,e}]] will return a[b,c,{d,e}] > 4) If I define > f[x_,y_:Null] := g[x,y] > does it work like this: > f[1,2] -> g[1,2] > f[3] -> g[3] ? f[1,2] will return g[1,2] f[3] will return g[3,Null] > 5) Which function returns position of the element in the list? > Pos[list,element]? Position[{a,b,c},a] will return {{1}}, i.e. first position at level one ; Position[{a,b,c,a}] will return {{1},{4}}, i.e. first and fourth positions at level one ; Position[{a,b,c,{a}}] will return {{1},{4,1}}, i.e. first position at level one and fourth position at level two. > 6) How to replace a sub-list, e.g.: > {a,b,c,d,e,f,g,h} 3 {X,Y,Z} ---> {a,b,X,Y,Z,f,g,h} You must fiddle with Delete and Insert, because there is not built-in function. Such a function should spend too much time trying to check if sub-list, position of operation, etc. are coherent. Replacements in Mathematica are pattern-based, and, depending on the real problem, the use of replacements operators can be more appropriate. > 7) What are Array, Scan, Function, Take, FoldList, Append, NestList? > My guesses: > Array[f,n] -> {f[1],f[2],...,f[n]} Right > Scan[f,{a,b,c,d,e}] -> f[a];f[b];f[c];f[d];f[e] Wrong : Scan does not return anything, it is useful when using side effect functions, like in : Scan[Print,{a,b,c}] You can use Map : Map[f,{a,b,c}] will return {f[a],f[b],f[c]} Other questions : if you have web access, the Mathematica book is online : http://documents.wolfram.com
- References:
- a few questions
- From: Tomaz Cedilnik <tcedilnik@ntlworld.com>
- a few questions