egison-tutorial 3.3.6 → 3.5.0
raw patch · 2 files changed
+81/−71 lines, 2 filesdep ~egison
Dependency ranges changed: egison
Files
- Main.hs +79/−69
- egison-tutorial.cabal +2/−2
Main.hs view
@@ -30,10 +30,10 @@ let ssn' = (read ssn) :: Int let ret = case tutorial of Tutorial ss ->- if 0 < sn' && sn' <= length ss + if 0 < sn' && sn' <= length ss then case nth sn' ss of Section _ cs ->- if 0 < ssn' && ssn' <= length cs + if 0 < ssn' && ssn' <= length cs then showContent $ nth ssn' cs else "error: content out of range" else "error: section out of range"@@ -103,7 +103,7 @@ printVersionNumber :: IO () printVersionNumber = do- putStrLn $ showVersion P.version + putStrLn $ showVersion P.version exitWith ExitSuccess showBanner :: IO ()@@ -143,7 +143,7 @@ selectSection :: Tutorial -> IO Section selectSection tutorial@(Tutorial sections) = do putStrLn $ take 30 $ repeat '='- putStrLn $ "List of sections in the tutorial"+ putStrLn $ "List of sections in the tutorial." putStrLn $ show tutorial putStrLn $ take 30 $ repeat '=' putStrLn $ "Choose a section to learn."@@ -176,7 +176,7 @@ where settings :: MonadIO m => FilePath -> Settings m settings home = setComplete completeEgison $ defaultSettings { historyFile = Just (home </> ".egison_history") }- + loop :: Env -> [Content] -> Bool -> IO () loop env [] _ = do -- liftIO $ showFinishMessage@@ -255,9 +255,9 @@ "====================" tutorial :: Tutorial-tutorial = Tutorial - [Section "Calculate numbers"- [ +tutorial = Tutorial+ [Section "Calculate numbers (10 minutes)"+ [ Content "We can do arithmetic operations with '+', '-', '*', '/', 'modulo' and 'power'." ["(+ 1 2)", "(- 30 15)", "(* 10 20)", "(/ 20 5)", "(modulo 17 4)", "(power 2 10)"] [],@@ -279,19 +279,19 @@ Content "With the 'take' function, we can extract a head part of the collection.\nWe can construct a collection with '{}'." ["(take 0 {1 2 3 4 5})", "(take 3 {1 2 3 4 5})"] [],- Content "We can handle infinite lists.\nFor example, 'nats' is an infinite list that contains all natural numbers.\nGet a collection of natural numbers of any length you like."- ["(take 10 nats)", "(take 100 nats)"]- ["Get first 1000 numbers from nats."],+ Content "We can handle infinite lists.\nFor example, 'nats' and 'primes' are an infinite list that contains all natural numbers and prime numbers respectively.\nTry to extract a head part from them."+ ["(take 10 nats)", "(take 30 nats)", "(take 10 primes)", "(take 30 primes)"]+ ["What is the 100th prime number."], Content "We can create a \"partial\" function using '$' as an argument." ["((* $ 2) 10)", "((modulo $ 3) 10)"] [],- Content "With the 'map' function, we can operate each element of the collection at onece."+ Content "With the 'map' function, we can operate each element of the collection at once." ["(take 100 (map (* $ 2) nats))", "(take 100 (map (modulo $ 3) nats))"] [], Content "With the 'foldl' function, we can gather together all elements of the collection using an operator you like." ["(foldl + 0 {1 2 3 4 5})", "(foldl * 1 {1 2 3 4 5})"] ["Try to get the sum of from 1 to 100?"],- Content "Try to create a sequce of numbers '{1 1/2 1/3 1/4 ... 1/100}'."+ Content "Try to create a sequence of numbers '{1 1/2 1/3 1/4 ... 1/100}'." [] [], Content "Try to calculate '1 + 1/2 + 1/3 + 1/4 + ... + 1/100'.\nRemember that we can convert a rational number to a float number with 'rtof'."@@ -304,38 +304,35 @@ [] [] ],- Section "Basics of functional programming"+ Section "Basics of functional programming (10 minutes)" [- Content "We can define a function. Let's define a function and test it."- ["(define $f (lambda [$x] (+ x 1)))", "(f 10)", "(define $g (lambda [$x $y] (* x y)))", "(g 10 20)", "(define $sum (lambda [$n] (foldl + 0 (take n nats))))", "(sum 10)"]- ["Try to define a 'fact' function."],+ Content "We can bind a value to a variable with a 'define' expression.\nWe can easily get the value we bound to a variable."+ ["(define $x 10)", "x", "(define $y (+ 1 x))", "y"]+ [],+ Content "We support recursive definitions. It enables us to define an collection with infinite elements."+ ["(define $ones {1 @ones})", "(take 100 ones)", "(define $nats {1 @(map (+ $ 1) nats)})", "(take 100 nats)", "(define $odds {1 @(map (+ $ 2) odds)})", "(take 100 odds)"]+ ["Try to define the infinite list of even numbers that is like {2 4 6 8 10 ...}."],+ Content "We can create a function with a 'lambda' expression. Let's define functions and test them."+ ["(define $increment (lambda [$x] (+ x 1)))", "(increment 10)", "(define $multiply (lambda [$x $y] (* x y)))", "(multiply 10 20)", "(define $sum (lambda [$n] (foldl + 0 (take n nats))))", "(sum 10)"]+ ["Try to define a 'fact' function, which obtains an natural number 'n' and returns 'n * (n - 1) * ... * 2 * 1'."], Content "We can compare numbers using functions that return '#t' or '#f'.\n'#t' means the true.\n'#f' means the false.\nFunctions that return '#t' or '#f' are called \"predicates\"." ["(eq? 1 1)", "(gt? 1 1)", "(lt? 1 1)", "(gte? 1 1)", "(lte? 1 1)"] [],- Content "With the 'while' function, we can extract all head elements that satisfy the predicate.\n'primes' is a infinites list that contains all prime numbers."+ Content "With the 'while' function, we can extract all head elements that satisfy the predicate.\n'primes' is a infinite list that contains all prime numbers." ["(while (lt? $ 100) primes)", "(while (lt? $ 1000) primes)"] [],- Content "We use 'lambda' expressions to create functions and predicates.\nHere are simple 'lambda' examples."- ["((lambda [$x] (+ x 1)) 10)", "((lambda [$x $y] (* x y)) 10 20)", "((lambda [$p] (eq? (modulo p 4) 1)) 29)"]- [],- Content "With the 'filter' function, we can extract all elements that satisfy the predicate.\n'We extract all prime numbers that are congruent to 1 modulo 4."- ["(take 100 (filter (lambda [$p] (eq? (modulo p 4) 1)) primes))", "(take 200 (filter (lambda [$p] (eq? (modulo p 4) 1)) primes))"]+ Content "With the 'filter' function, we can extract all elements that satisfy the predicate."+ ["(take 100 (filter even? nats))", "(take 100 (filter prime? nats))", "(take 100 (filter (lambda [$p] (eq? (modulo p 4) 1)) primes))"] ["Try to enumerate the first 100 primes that are congruent to 3 modulo 4."], Content "We combine numbers using '[]'.\nThese things are called 'tuples'." ["[1 2]", "[1 2 3]"] [],- Content "Note that a tuple that consists of only one elment is equal with that element itself."+ Content "Note that a tuple that consists of only one element is equal with that element itself." ["[1]", "[[[1]]]"] [], Content "With the 'zip' function, we can combine two lists as follow." ["(take 100 (zip nats nats))", "(take 100 (zip primes primes))"]- ["Try to create the prime table that is like '{[1 2] [2 3] [3 5] [4 7] [5 11] ...}'"],- Content "We can bind a value to a variable with a 'define' expression.\nWe can easily get the value we bound to the variable."- ["(define $x 10)", "x", "(define $ps (zip nats primes))", "(take 100 ps)"]- [],- Content "We can write a recursive definition. Let's try that."- ["(define $odds {1 @(map (+ $ 2) odds)})", "(take 10 odds)"]- ["Try to define 'evens' that is like {2 4 6 8 10 ...}."],+ ["Try to generate the prime table that is like '{[1 2] [2 3] [3 5] [4 7] [5 11] ...}'"], Content "Try to create a fibonacci sequence that is like '{1 1 2 3 5 8 13 21 34 55 ...}'.\n\nHint:\n At first try to create the following sequence.\n {[1 1] [1 2] [2 3] [3 5] [5 8] [8 13] [13 21] [21 34] ...}" [] [],@@ -343,63 +340,76 @@ [] [] ],- Section "Basics of pattern-matching"+ Section "Basics of pattern-matching (10 minutes)" [- Content "We can do pattern-matching against multisets."- ["(match-all {1 2 3} (multiset integer) [<cons $x $xs> [x xs]])"]- [],- Content "We can change the way of pattern-matching by changing \"matcher\".\nTry the following expressions."- ["(match-all {1 2 3} (list integer) [<cons $x $xs> [x xs]])", "(match-all {1 2 3} (multiset integer) [<cons $x $xs> [x xs]])", "(match-all {1 2 3} (set integer) [<cons $x $xs> [x xs]])"]- [],- Content "We can do non-linear pattern-matching.\nTry the following expression."- ["(match-all {1 1 2 3 2} (list integer) [<cons $x <cons ,x _>> x])", "(match-all {1 1 2 3 2} (multiset integer) [<cons $x <cons ,x _>> x])", "(match-all {1 1 2 3 2} (multiset integer) [<cons $x <cons ,(+ x 2) _>> x])"]- [],- Content "We can do pattern-matching against a collection of collections as follow."- ["(match-all {{1 2 3 4 5} {4 5 1} {6 1 7 4}} (list (multiset integer)) [<cons <cons $n _> <cons <cons ,n _> <cons <cons ,n _> _>>> n])"]- [],- Content "A pattern that has '^' ahead of which is called a not-pattern.\nA not-pattern matches when the target does not match against the pattern."- ["(match-all {1 2 1 3} (multiset integer) [<cons $x ^<cons ,x _>> x])"]+ Content "We can pattern-match against a collection.\nThe 'join' pattern divides a collection into two collections.\nPlease note that the 'match-all' expression enumerates all results of pattern-matching."+ ["(match-all {1 2 3} (list integer) [<join $hs $ts> [hs ts]])",+ "(match-all {1 2 3 4 5} (list integer) [<join $hs $ts> [hs ts]])"] [],- Content "An and-pattern matches when the all patterns matches the target.\nIt can be used like an as-pattern."- ["(match-all {1 2 1 3} (multiset integer) [<cons $x (& ^<cons ,x _> $xs)> [x xs]])"]+ Content "Try another pattern-constructor 'cons'.\nThe 'cons' pattern divides a collection into the head element and the rest collection.\n"+ ["(match-all {1 2 3} (list integer) [<cons $x $xs> [x xs]])",+ "(match-all {1 2 3 4 5} (list integer) [<cons $x $xs> [x xs]])"] [],- Content "An or-pattern matches when one of the patterns matches the target."- ["(match-all {1 2 1 3} (multiset integer) [<cons $x (| <cons ,x _> ^<cons ,x _>)> x])"]+ Content "'_' is a wildcard and matches with any objects."+ ["(match-all {1 2 3} (list integer) [<cons $x _> x])",+ "(match-all {1 2 3 4 5} (list integer) [<join $hs _> hs])"] [],- Content "Try another pattern-constructor 'join'.\n'join' divides a collection into two collections."- ["(match-all {1 2 3 4 5} (list integer) [<join $xs $ys> [xs ys]])", "(match-all {1 2 3 4 5} (multiset integer) [<join $xs $ys> [xs ys]])", "(match-all {1 2 3 4 5} (set integer) [<join $xs $ys> [xs ys]])"]+ Content "We can write non-linear patterns.\nIn the case of 'cons' and 'join' patterns, patterns that begins with ',' matches the object only if the object is equal with the expression after ','.\nPlease try the following expression."+ ["(match-all {1 1 2 3 2} (list integer) [<cons $x <cons ,x _>> x])"] [],- Content "We can enumerate two combination of numbers as follow."- ["(match-all {1 2 3 4 5} (list integer) [<join _ <cons $x <join _ <cons $y _>>>> [x y]])"]- ["Try to enumerate three combination of numbers."],- Content "Did we think how about \"n\" comination of the elements of the collection?\nWe already have a solution.\nWe can write a pattern that include '...' as the following demonstrations."- ["(match-all {1 2 3 4 5} (list integer) [(loop $i [1 4] <join _ <cons $a_i ...>> _) a])", "(match-all {1 2 3 4 5} (list integer) [(loop $i [1 5] <join _ <cons $a_i ...>> _) a])", "(match-all {1 2 3 4 5} (list integer) [(loop $i [1 $n] <join _ <cons $a_i ...>> _) [n a]])"]+ Content "We can express various things using 'cons' and 'join'.\nThe most of functions in the collection library of Egison are written using pattern-matching!"+ ["(match-all {1 1 2 3 2} (list integer) [<join _ <cons $x <join _ <cons $y _>>>> [x y]])",+ "(match-all {1 1 2 3 2} (list integer) [<join _ <cons $x <join _ <cons ,x _>>>> [x x]])"]+ ["Try to enumerate three combinations of numbers."],+ Content "We can pattern-match against infinite collections.\nWe can enumerate twin primes using pattern-matching as follow.\nNote that we can write any expression after ','."+ ["(take 10 (match-all primes (list integer) [<join _ <cons $p <cons ,(+ p 2) _>>> [p (+ p 2)]]))"]+ ["What is the 100th twin prime?"],+ Content "Try to enumerate the first 10 prime pairs whose form is (p, p+6) like '{{[5 11] [7 13] [11 17] [13 19] [17 23] ...}'."+ [] [], Content "This is the end of this section.\nPlease play freely or proceed to the next section.\nThank you for enjoying our tutorial!" [] [] ],- Section "Pattern-matching against infinite collections"+ Section "Pattern-matching against various data types (5 minutes)" [- Content "We can write a pattern-matching against infinite lists even if that has infinite results.\nNote that Egison really enumurate all pairs of two natural numbers in the following example."- ["(take 10 (match-all nats (set integer) [<cons $m <cons $n _>> [m n]]))"]+ Content "We can pattern-match even against multisets and sets.\nWe can change the way of pattern-matching by changing the \"matcher\".\nPlease try the following expressions."+ ["(match-all {1 2 3} (list integer) [<cons $x $xs> [x xs]])",+ "(match-all {1 2 3} (multiset integer) [<cons $x $xs> [x xs]])",+ "(match-all {1 2 3} (set integer) [<cons $x $xs> [x xs]])"] [],- Content "We can enumerate all two combinations of natural numbers as follow."- ["(define $two-combs (match-all nats (list integer) [<join _ (& <cons $x _> <join _ <cons $y _>>)> [x y]]))", "(take 100 two-combs)"]+ Content "Try another pattern-constructor 'join'.\nThe 'join' pattern divides a collection into two collections."+ ["(match-all {1 2 3 4 5} (list integer) [<join $xs $ys> [xs ys]])",+ "(match-all {1 2 3 4 5} (multiset integer) [<join $xs $ys> [xs ys]])",+ "(match-all {1 2 3 4 5} (set integer) [<join $xs $ys> [xs ys]])"] [],- Content "We can get twin primes or triplet primes using pattern-matching as follow."- ["(take 10 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 2) _>>> [n (+ n 2)]]))", "(take 10 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 2) <cons ,(+ n 6) _>>>> [n (+ n 2) (+ n 6)]]))", "(take 10 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 4) <cons ,(+ n 6) _>>>> [n (+ n 2) (+ n 6)]]))"]- ["What are the 100th twin primes?"],- Content "We prepared the 'p-f' function that prime-factorize a number.\nWe can play freely with numbers a lot of time."- ["(take 100 (map p-f nats))"]- ["Are there three successive natural numbers all of whose prime-factorization contain three primes? For example, '27=3*3*3' and '28=2*2*7' but '29=29', so the sequence '27', '28' and '29' is not that."],- Content "This is the end of our tutorial.\nThank you for enjoying our tutorial!"+ Content "We can write non-linear patterns.\nTry the following expression."+ ["(match-all {1 1 2 3 2} (multiset integer) [<cons $x <cons ,x _>> x])",+ "(match-all {1 1 2 3 2} (multiset integer) [<cons $x <cons ,(+ x 2) _>> x])"]+ [],+ Content "A pattern that has '^' ahead of which is called a not-pattern.\nA not-pattern matches when the target does not match against the pattern."+ ["(match-all {1 2 1 3 2} (multiset integer) [<cons $x <cons ,x _>> x])",+ "(match-all {1 2 1 3 2} (multiset integer) [<cons $x ^<cons ,x _>> x])"]+ [],+ Content "We can pattern-match against infinite collections with infinite results.\nNote that Egison really enumerates all pairs of two natural numbers in the following example."+ ["(take 10 (match-all nats (set integer) [<cons $m <cons $n _>> [m n]]))"]+ [],+ Content "This is the end of our tutorial.\nThank you for enjoying our tutorial!\nPlease check our paper, manual and code for further reference!" [] [] ] ]+-- Content "We support \"and-patterns\" and \"or-patterns\".\nWe can enumerate prime triplets using them as follow.\n\"And-patterns\" and \"or-patterns\" are represented using '&' and '|' respectively."+-- ["(take 10 (match-all primes (list integer) [<join _ <cons $p <cons (& $m (| ,(+ p 2) ,(+ p 4))) <cons ,(+ p 6) _>>>> [p m (+ p 6)]]))"]+-- ["What is the 20th prime triplet?"],+-- Content "Try to enumerate the first 8 prime quadruplets whose form is (p, p+2, p+6, p+8) like '{{[5 7 11 13] [11 13 17 19] ...}'."+-- []+-- [], -- Section "Define your own functions" -- [+-- Content "Did we think how about \"n\" combinations of the elements of the collection?\nWe already have a solution.\nWe can write a pattern that include '...' as the following demonstrations."+-- ["(match-all {1 2 3 4 5} (list integer) [(loop $i [1 3] <join _ <cons $a_i ...>> _) a])", "(match-all {1 2 3 4 5} (list integer) [(loop $i [1 4] <join _ <cons $a_i ...>> _) a])"]+-- [], -- Content "Let's try 'if' expressions." -- ["(if #t 1 2)", "(if #f 1 2)", "(let {[$x 10]} (if (eq? x 10) 1 2))"] -- [],
egison-tutorial.cabal view
@@ -1,5 +1,5 @@ Name: egison-tutorial-Version: 3.3.6+Version: 3.5.0 Synopsis: A tutorial program for the Egison programming language Description: A tutorial program for the Egison programming language. Egison is the programming langugage that realized non-linear pattern-matching against unfree data types.@@ -20,4 +20,4 @@ Executable egison-tutorial Main-is: Main.hs- Build-depends: egison >= 3.3.7, base >= 4.0 && < 5, array, containers, unordered-containers, haskeline, transformers, mtl, parsec >= 3.0, directory, ghc, ghc-paths, filepath, regex-posix, strict-io, bytestring+ Build-depends: egison >= 3.5.1, base >= 4.0 && < 5, array, containers, unordered-containers, haskeline, transformers, mtl, parsec >= 3.0, directory, ghc, ghc-paths, filepath, regex-posix, strict-io, bytestring