Message Boards Message Boards

0
|
3755 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to define a function with multiple expressions

Posted 2 years ago

I apologize for the simplicity of this question. The following statement works fine

For [ i = 1, i < 4 , i++, ( Print ["Fred"]; Print [ "harry"] )  ]
Fred
harry
Fred
harry
Fred
harry

So Mathematica easily evaluates compound statements. I would like to create a function called PrintFredAndHarry which takes no parameters and just evaluates the compound statement, so that whenever Mathematica sees PrintFredAndHarry it evaluates the compound statement ( Print ["Fred"]; Print [ "harry"] )

I have in mind a much more complex compound statement containing at least 40 or 50 expressions which I use to evaluate a list of strings which basically contains the data and metadata for a card from a HyperCard stack. Right now it is a sequence of isolated expressions in a Mathematica Notebook, and as I step through the expressions one by one it works fine.

This is an incredibly simple question, but I can't seem to be able to make the compound statement a function.

POSTED BY: Lewis Robinson
4 Replies
Posted 2 years ago

Rohit

Since you have been so helpful, here is some material I'm writing up to document the structure of the data and the metadata, so I'll always understand the software

(* This is an attempt to write Mathematica software to manipulate the \ data (and metadata) found in file Cards 4550 to 4555 which is \ found in the following file "/Users/lewisrobinson/Desktop/Hypercard Xref/ Cards 4550 to \ 4555" So I can write it back to a notebook in \ "/Users/lewisrobinson/Desktop/Hypercard Xref" in a useable form *)

(* The most important thing is to understand how the data and \ metadata are structured in the file "Cards xxxx to yyyy".
That was done by a HyperCard program in the Xref Stack called \ AllCardInfo which I think is correct.
It basically separated text lines by carriage returns. data = Import [ "/Users/lewisrobinson/Desktop/Hypercard Xref/ \ Cards 4550 to 4555", "lines" ] (* this gives the information in the text file as a list \ of strings in my variable called data. Note: "lines" is crucial *)

(* So here is the structure of Cards XXXX to YYYY

Line 1 is always "Total Number of Cards = 6", or someother positive \ integer (call it n)

The data for each card is stereotyped and will be described by lcln# -- local card line number

lcln1 "Begin card "X2383901" ; always these 3 words

lcln2 "Card ID 2580879" ; always these 3 words

lcln3 always a single string Xref Title "Differential effect of \ a transcription factor"" in the case of the first card here Always a \ single string even if the card didn't have a title

lcln4 contains a string representing an integer -- the number of \ buttons always 1 or greater even if there are no buttons

lcln5 a pointer to the list of buttons then multiply #buttons x 2 and add to get to the number of topics lcln? a string containing the number of topics

lcln?? a string containing the number of text lines

The last line in the dard is END card X2383901

*)

POSTED BY: Lewis Robinson
Posted 2 years ago

It sure is acceptable. Thanks Bill

POSTED BY: Lewis Robinson
Posted 2 years ago

No need for the DownValue

PrintFredAndHarry := (Print["Fred"]; Print["harry"]);
PrintFredAndHarry
(* Fred
   harry *)

Or

PrintFredAndHarryAndLewis := Module[{},
  Print["Fred"];
  Print["harry"];
  Print["lewis"];]
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Try

PrintFredAndHarry[]:=( Print ["Fred"]; Print [ "harry"] );

which creates a function with no arguments

If you evaluate

PrintFredAndHarry[]

it should print your two words.

Without the () the precedence of the ; would be high enough to make Mathematica think that was the end of your function definition. With the () it understands multiple semicolon separated statements inside the () are all included in the function definition.

Is that acceptable?

POSTED BY: Bill Nelson
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