-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorld.wls
More file actions
267 lines (168 loc) · 4.09 KB
/
HelloWorld.wls
File metadata and controls
267 lines (168 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env wolframscript
(* ::Package:: *)
(* ::Subsubsection:: *)
(*This mathematica notebook is based upon the book : Getting Started with Wolfram Language & Mathematica for Raspberry Pi by Agus Kurniawan*)
(* ::Subsubsection:: *)
(*The notebook exercises are based upon chapters 1 and 2*)
(* ::Input:: *)
(*str ="hello world"*)
(* ::Subsubsection:: *)
(*Examples of variables, addition, subtraction, multiplication, division, and exponentiation*)
(* ::Input:: *)
(*a=3*)
(*b=5*)
(*c=a+b*)
(*d=a-b*)
(*e=a*b*)
(*f=a/b*)
(*g=a^b*)
(* ::Subsubsection:: *)
(*Examples of logical operations, less than (<), greater than (>), less than or equal (<=), greater than or equal(>=), equal to (==), not equal to (!=), and (&&), or (||)*)
(* ::Input:: *)
(*a<b*)
(* ::Input:: *)
(*a>b*)
(* ::Input:: *)
(*a<=b*)
(* ::Input:: *)
(*a>=b*)
(* ::Input:: *)
(*a==b*)
(* ::Input:: *)
(*a!=b*)
(* ::Input:: *)
(*(a<b)&&(a+b>7)*)
(* ::Input:: *)
(*(a<b)||(a<8)*)
(* ::Subsection:: *)
(*Example of an if statement (conditional) *)
(**)
(*If[statement, _if_true, do_if_false]*)
(* ::Input:: *)
(*x= 3;*)
(*y= 0;*)
(*If[x>1, y=Sqrt[x], y=x^2];*)
(*Print[y]*)
(* ::Subsection:: *)
(*Example of another if statement (conditional)*)
(* ::Input:: *)
(*x= 3;*)
(*y= 0;*)
(*If[x>1, y=x^2, y=Sqrt[x]];*)
(*Print[y]*)
(* ::Subsection:: *)
(*Example of another if statement (conditional)*)
(* ::Input:: *)
(*x= 0.5;*)
(*y= 0;*)
(*If[x>1, y=x^2, y=Sqrt[x]];*)
(*Print[y]*)
(* ::Subsection:: *)
(*Example of another if statement (conditional)*)
(* ::Input:: *)
(*z=6;*)
(*m :=If[z>5,0,1];*)
(*Print[m]*)
(* ::Subsection:: *)
(*Example of another if statement (conditional)*)
(* ::Input:: *)
(*z=6;*)
(*m :=If[z>5,1,0];*)
(*Print[m]*)
(* ::Subsection:: *)
(*Example of another if statement (conditional)*)
(* ::Input:: *)
(*z=4;*)
(*m :=If[z>5,1,0];*)
(*Print[m]*)
(* ::Subsubsection:: *)
(*Example of a switch statement (conditional) *)
(**)
(*Switch[statement,case_1, do_case_1...,case_n, do_case_n, do_default_case]*)
(* ::Input:: *)
(*k=1;*)
(*n=0;*)
(*Switch[k,1,n=k+10,2,n=k^2+3,_,n=-1];*)
(*Print[n];*)
(* ::Subsubsection:: *)
(*Example of another switch statement (conditional) *)
(* ::Input:: *)
(*k=2;*)
(*n=0;*)
(*Switch[k,1,n=k+10,2,n=k^2+3,_,n=-1];*)
(*Print[n];*)
(* ::Subsubsection:: *)
(*Example of another switch statement (conditional) *)
(* ::Input:: *)
(*k=3;*)
(*n=0;*)
(*Switch[k,1,n=k+10,2,n=k^2+3,_,n=-1];*)
(*Print[n];*)
(* ::Subsubsection:: *)
(*Example of another switch statement (conditional) *)
(* ::Input:: *)
(*k=4;*)
(*n=0;*)
(*Switch[k,1,n=k+10,2,n=k^2+3,_,n=-1];*)
(*Print[n];*)
(* ::Subsubsection:: *)
(*Example of a do loop*)
(**)
(*Do[do_something, {index}]*)
(* ::Input:: *)
(*Do[Print["Hello World"],{3}]*)
(* ::Subsubsection:: *)
(*Example of a for loop*)
(**)
(*For[initial, conditional, increment/decrement, do_something]*)
(* ::Input:: *)
(*For[i=0, i<3, i++, Print["index",i]]*)
(* ::Input:: *)
(**)
(* ::Subsubsection:: *)
(*Example of a while loop*)
(**)
(*While[conditional, do_something]*)
(* ::Input:: *)
(*j=0;*)
(*While[j<5, Print["j=",j]; j++;]*)
(* ::Text:: *)
(*Example of break *)
(**)
(*Used to exit the nearest enclosing loop*)
(* ::Input:: *)
(*For[i=0, i<5, i++, Print[i]; If[i==2, Break[]]]*)
(* ::Subsubsection:: *)
(*Example of continue*)
(**)
(*Used to go to the next step in the current loop*)
(* ::Input:: *)
(*For[i=0, i<5, i++,*)
(*Print[i];*)
(*If[i==2,Continue[]];*)
(*If[i==5, Break[]]*)
(*]*)
(* ::Subsubsection:: *)
(*Example of functions*)
(**)
(*Used to avoid redundant script*)
(**)
(*function[params]:=do_something*)
(* ::Input:: *)
(*Clear[f,x,y];*)
(*f[x_]:=(x-2)*Sqrt[x];*)
(*Print[f[15]];*)
(*Print[f[8]];*)
(* ::Input:: *)
(*Clear[f,x,y];*)
(*f[x_,y_]:=x^2 +y;*)
(*Print[f[5,8]];*)
(*Print[f[2,3]];*)
(* ::Subsubsection:: *)
(*Example of recursive functions, annotated with comments*)
(**)
(*Recusive functions allow for more efficient code and can use a single iterated process*)
(* ::Input:: *)
(*(*Recursive factorial function*)*)
(*factorial[n_]:=If[n==0,1,factorial[n-1]*n];*)
(*For[i=0, i<=5, i++, Print[i, "!=", factorial[i]]];*)