@@ -290,6 +290,94 @@ This document is created and maintained by Hedgehog Lab Community. The markdown
290290\`)
291291
292292
293+ ` ;
294+
295+
296+ const moduleSource = `
297+ /*
298+ File: fibonacci.hs
299+ Location: https://gist.githubusercontent.com/lidangzzz/86c78163bf7838220224530d6e36aec9/raw/da89c75d4b6671dc0936240a62d483bf67e2b9ef/fibonacci.hs
300+
301+ function fibonacci(x){
302+ if (x<0) return 0;
303+ if (x==1 || x==0) return 1;
304+
305+ //elst x>=2
306+ let dp = [1,1]
307+ for (let i=2;i<=x;i++){ let val = dp[dp.length-1] + dp[dp.length-2]; dp.push(val)}
308+ return dp[x];
309+ }
310+ */
311+
312+
313+ // 1. import the function from URL...
314+ *import https://gist.githubusercontent.com/lidangzzz/86c78163bf7838220224530d6e36aec9/raw/da89c75d4b6671dc0936240a62d483bf67e2b9ef/fibonacci.hs
315+
316+
317+ // 2. then use it.
318+ for (let i=0;i<10;i++) {
319+ print("Fibonacci with index " + i + ": " + fibonacci(i) );
320+ }
321+
322+ //3. or import as a local function/class/variable
323+ myFibonacci = *import https://gist.githubusercontent.com/lidangzzz/86c78163bf7838220224530d6e36aec9/raw/da89c75d4b6671dc0936240a62d483bf67e2b9ef/fibonacci.hs
324+
325+ for (let i=0;i<10;i++) {
326+ print("myFibonacci with index " + i + ": " + myFibonacci(i));
327+ }
328+
329+ /*
330+ Comments:
331+
332+ 1. The fibonacci.hs file located at follow url will be imported
333+ automatically from the preprocessor by replacing the "import URL"
334+ part with the big chunk of raw string from the file into your script
335+ (just like C++ macro #include<your_header.h>).
336+
337+ 2. If you are a module/library/class/function developer, please make sure
338+ that every function/class/variable is imported inside your scope, for example:
339+ --
340+ my_function.hs
341+
342+ function my_function(x,y,z){
343+ let f1 = * import http://myWebsite.com/f1.hs
344+ let f2 = * import http://myWebsite.com/f2.hs
345+ let val = f1(x) + f2(y) + z*z;
346+ return val;
347+ }
348+ --
349+ which will guarantee that all imported functions/classes (f1 and f2)
350+ are defined and used only in your scope of "myfunction". This won't
351+ pollute any functions/classes/variables out of your scope.
352+
353+ 3. If you are a teacher or researcher sharing a piece of code with others,
354+ then it's a good choice that includes all necessary functions/classes/variables
355+ into a single hs file so that other people can import once and have all setup
356+ ready, for example
357+ --
358+ environment_setup.hs
359+
360+ let dataset_1 = getDataset1();
361+ let dataset_2 = getDataset2();
362+ function regression(x,y){...}
363+ class MyClass1{ ... }
364+ let ground_truth = [1,1,1,0,0,...]
365+ let test_dataset = getTestDataset();
366+ --
367+
368+ Then other people can includes all above by adding one line of code at beginning
369+ * import http://..../environment_setup.hs
370+
371+ doSomething(dataset_1);
372+ draw(regression(dataset_2));
373+ ...
374+
375+ 4. Do NOT put space between * and "import". Also even if *"import" is used in comments,
376+ it will still be used as a macro & string replacement. So do not use in comments at
377+ this version.
378+ */
379+
380+
293381` ;
294382
295383export const tutorials = [
@@ -324,5 +412,9 @@ export const tutorials = [
324412 {
325413 description : 'Markdown' ,
326414 source : markdownSource
415+ } ,
416+ {
417+ description : 'Module' ,
418+ source : moduleSource ,
327419 }
328420] ;
0 commit comments