Message Boards Message Boards

0
|
10775 Views
|
9 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Write If-Else Statements using a Module?

Posted 8 years ago

Although I've set up some actions for when z=-1 and when z=1, Only the actions that correspond with z=1 perform. Where am I going wrong?

eighthnote[x_, y_, z_] :=
     Module[{},
      If[
       z == -1,
       {
        Disk[{x, y}, {1, 0.8}],
        Line[{{x - 1, y}, {x - 1, y - 5}}],
        Line[{{x - 2, y - .7}, {x + 2, y - .7}}],
        FilledCurve[
         {BezierCurve[{{x - 1, y - 4}, {x - .4, y - 4.7}, {x + 2.3, 
             y + .7}, {x - .15, y - 1.1}}],
          BezierCurve[{{x + 1.5, y + .14}, {x + .85, y - 2.5}, {x - .95, 
             y - 5.2}, {x - 1.05, y - 5.27}}]
          }
         ] (* Filled Curve *)
        }
       ] (*If Statement *);
      If[
       z == 1,
       {
        Disk[{x, y}, {1, 0.8}],
        Line[{{x - 1, y}, {x - 1, y - 5}}],
        Line[{{x - 2, y - .7}, {x + 2, y - .7}}],
        FilledCurve[
         {BezierCurve[{{x - 1, y - 4}, {x - .4, y - 4.7}, {x + 2.3, 
             y + .7}, {x - .15, y - 1.1}}],
          BezierCurve[{{x + 1.5, y + .14}, {x + .85, y - 2.5}, {x - .95, 
             y - 5.2}, {x - 1.05, y - 5.27}}]
          }
         ] (* Filled Curve *)
        }
       ] (*If Statement *)
      ]
    Graphics[{staff, eighthnote[4, 11, 1]}]
    Graphics[{staff, eighthnote[4, 11, -1]}]
POSTED BY: Alicia Young
9 Replies

Actually, what Bill said doesn't apply to your case. You're not using the curly braces {} to combine multiple things to happen in the "then" of your If (if that were the case, he'd be right). In Wolfram Language, the {} always mean lists (or iterators). In your code, the "then" part of your If really does return a list of things as the actual result of the computation (Graphics primitives, in this case). So the list is exactly what you want, and that's why his suggestion didn't work for you. The problem in your original code is that the first If never returns anything, it generates a list of stuff (not writing it to any variable or anything like that), and then has its output suppressed inside a CompoundExpression (that's what the semicolon does). The second If either returns the things you want (if the condition is True), or nothing at all (if the condition is False).

In general, I second David's suggestion to use Which. If you want some other choices, here are some ideas (I'll use a dummy example to shorten the code). You could for instance still use separate If functions (although that's not very idiomatic, and there are several potential sources of trouble, but I wanted to mention this solution since it's close to your original approach):

If[z == 1, plotcontent = {Disk[], Line[{{0, 0}, {1, 0}}]}];
If[z == -1, plotcontent = {Disk[], Line[{{0, 0}, {1, 0}}]}];
(*etc. for more cases*)
Plot[plotcontent]

Or, if there's only a handful of well-defined values or patterns that z is ever going to assume, it might be more readable to just overload the function:

eightnote[x_, y_, 1] :=(*code goes here*);
eightnote[x_, y_, -1] :=(*code goes here*);
POSTED BY: Bianca Eifert
Posted 8 years ago

Which ended up working, but I didn't realize that plotcontent functions similarly to Return. Thank you for passing along the information. I tried this and it also worked.

POSTED BY: Alicia Young

I'm sorry, my explanation may have been a bit confusing. There's nothing special about "plotcontent", it's just a random variable name I picked. All that's happening is that the list of things is assigned to a variable, and you can then use that later on. Anyway, Which is your best bet here, so I'm happy it worked out for you! I like the way your notes turned out, looks like you've got an interesting project going on.

POSTED BY: Bianca Eifert
Posted 8 years ago

This helped. Thanks!

POSTED BY: Alicia Young
Posted 8 years ago

I apologize for not looking carefully enough at your code and giving incorrect advice.

POSTED BY: Bill Simpson
Posted 8 years ago

Thank you for responding. I'm still having not having success with the function, but it's great to have that syntax tip.

POSTED BY: Alicia Young
Posted 8 years ago

Can you show me one or two example uses of your eighthnote[], what your output is, what the output should be and perhaps why?

POSTED BY: Bill Simpson
Posted 8 years ago

I went ahead used Which instead of If, and here is my output. Depending on which numbers are inputed, the eighth note will be stem up or stem down.

enter image description here

enter image description here

POSTED BY: Alicia Young
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