Message Boards Message Boards

1
|
5348 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Easy progamming with Wolfram Language modules

Posted 9 years ago

Hello, I try tu use the software Mathematica for my studies. I do some exercices (really easy), because i loose the two first month of school. I try to implement two modules:

  • Exercise 5 : Implement a Mathematica module, whose input is a string x and an integer n, to compute x^n
  • Exercise 6 : Implement a Mathematica module that returns the segments of an input string x

And what I try :

(* Exercice 5 *)

puissance[x_, n_] := Module[{res, i},

  res = x;

  For[i = 1, i < n, i++, res = res*x;];

  Return res;  ]

(* Exercice 6, do the same thing that Characters *)

characters[string_] := Module[{s, i},

  s = {};
  For[i = 0, i < Length[string], i++, AppendTo[s, string[[i]]];   ];

  Return[s];  ]

I test it with puissance[2,3] and characters["A string."] but both doesn't work.

Hope someone will help me, and sorry for my english!

POSTED BY: manon guillot
3 Replies
Posted 9 years ago

You wrote: "Exercise 5 : Implement a Mathematica module, whose input is a string x and an integer n, to compute x^n" but your test was puissance[2,3]

So, I'm not exactly sure what the expected behavior is. However, I can give some feedback that might be helpful.

  • In puissance[x, n] := ..., x and n are symbols, not patterns, so you've effectively defined a function for exactly that one case.
  • Return res, is not how you apply functions to arguments in Mathematica. That expression will resolve to a multiplication of the symbol Return and the variable res.
  • There is really no need to use Return at all, as the result of Module will simply be the last expression in the body.
  • string[[i]] is not how you access characters in a string. An error would be produced, and your variable s won't be updated.

Since this is homework, I don't feel comfortable giving you corrected versions. I would suggest you find a tutorial on basic Mathematica syntax. There are many resources at the Wolfram website and in the help documentation from within Mathematica.

Your examples suggest that you're comfortable with an imperative style of code. One thing these exercises might be trying to teach you is to use a more functional style. Mathematica code can be written in many styles, but for these exercises there are very simple, small, functional solutions. I suggest you try to solve the problems without using Module, For, Return, or AppendTo. I know the exercises mentioned "Mathematica module", but I'm not sure if you were quoting exactly anyway, and I'm assuming that "module" was just being used to mean a "solution in code".

If that's too vague, maybe you can provide a bit more context and I can try to be more specific without just giving you the answers.

POSTED BY: Eric Rimbey
Posted 9 years ago

I think that you should look for in the documentation or help of Mathematica something of the topics that you are trying to programme, particularly as it relates to the text strings, which is what I see that you are having problems. You'll see that they are easy to resolve your exercises using the commands precise that Mathematica has already pre-built, suerte y cualquier duda no dudes en ponerla en este foro,saludos Luis Ledesma

POSTED BY: Luis Ledesma

Welcome to Wolfram Community!

We use special formatting for code that you should follow - see HERE how.

I think you need to approach this differently. But I fixed your code so at least it works as I think you intended it to:

puissance[x_, n_] := Module[{res, i},
  res = x;
  For[i = 1, i < n, i++, res = res*x];
  Return[res]]

puissance[3, 3]
(*27*)

characters[string_] := Module[{s = {}, i},
  For[i = 1, i <= StringLength[string], i++, AppendTo[s, StringTake[string, {i}]]];
  Return[s]]

characters["A string"]
(*{"A"," ","s","t","r","i","n","g"}*)

Now you have to understand that Wolfram Language or WL (Mathematica is programing environment) is a functional programming language. You seem to use a lot of procedural (loops, cycles) constructs, are you sure you need to? Here is my solutions to you problems if I understood them correctly.

power[s_String, n_Integer] := ToExpression[s]^n;
power["3", 3]
(*27*)

StringTake["123456789", {3, 5}]
(*"345"*)

This maybe not what you need, but at least it shows how simple is functional form comparing to procedural, because it is native for WL.

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

Group Abstract Group Abstract