packages feed

egison-tutorial 3.2.2 → 3.2.3

raw patch · 2 files changed

+57/−48 lines, 2 files

Files

Main.hs view
@@ -118,7 +118,7 @@ selectSectionHelper :: [(Int, String)] -> Tutorial -> IO [Content] selectSectionHelper hs (Sections secs)  = do   putStrLn "===================="-  putStrLn "Select a section to learn."+  putStrLn "List of tutorials."   foldM (\x sec -> do           putStr $ "" ++ show x ++ ": "           putStrLn (fst sec)@@ -134,7 +134,7 @@  readNumber :: Int -> IO Int readNumber m = do-  putStr $ "(1-" ++ show m ++ "): "+  putStr $ "Please select a section to learn.\n(1-" ++ show m ++ "): "   hFlush stdout   input <- getLine --  let n = (read input :: Int)@@ -249,63 +249,61 @@ tutorial :: Tutorial tutorial =   Sections [-    ("Lv1 - Buildin data in Egison",-      Contents [-        ("There are two boolean value '#t' and '#f'.", ["#t", "#f"]),-        ("You can do boolean operations with 'and', 'or', 'not'.", ["(and #t #f)", "(or #t #f)", "(not #t)"]),-        ("You can do arithmetic operations with `+', '-', '*'.", ["(+ 1 2)", "(* 10 20)"]),-        ("You can write nested expression as follow.", ["(+ (* 10 20) 2)", "(/ (* 10 20) (+ 10 20))"]),-        ("We have predicates for numbers.", ["(eq? 1 1)", "(gt? 1 1)", "(lt? 1 1)",  "(gte? 1 1)", "(lte? 1 1)"]),-        ("We are supporting rational numbers.", ["(+ 2/3 1/5)", "(/ 42 84)"]),-        ("We are supporting floats, too.", ["(+ 10.2 1.3)", "(rtof 1/5)"]),-        ("We have predicates for floats.", ["(eq? 1.0 1.0)", "(gt? 1.0 1.0)", "(lt? 1.0 1.0)",  "(gte? 1.0 1.0)", "(lte? 1.0 1.0)"]),-        ("You can construct a tuple with '[]'.", ["[1 2]", "[1 2 3]"]),-        ("A tuple which consists of only one elment is equal with that element itself.", ["[1]", "[[[1]]]"]),-        ("You can construct a collection with '{}'.", ["{1}", "{1 2 3}"]),-        ("The collection after '@' in a collection is called a subcollection.", ["{1 @{2 3}}", "{1 @{2 3} @{4 @{5}} 6}"]),-        ("You can destruct collections with 'car' and 'cdr'.", ["(car {1 2 3})", "(cdr {1 2 3})"]),-        ("We can define an array as follow. We can access the element of the array using '_'.", ["(define $a [| 11 22 33 |])", "a_2"]),-        ("We can define an hash as follow. We can access the element of the hash using '_' as arrays.", ["(define $h {| [1 11] [2 22] [3 33] |})", "h_2"]),-        ("There is a document that gather information of the specification of Egison at \"http://www.egison.org/manual/\".", [])-        ]),+    ("Lv1 - Calculate numbers",+     Contents [+       ("We can do arithmetic operations with `+', '-', '*'.", ["(+ 1 2)", "(* 10 20)"]),+       ("We can write nested expression as follow.", ["(+ (* 10 20) 2)", "(/ (* 10 20) (+ 10 20))"]),+       ("We are supporting rational numbers.", ["(+ 2/3 1/5)", "(/ 42 84)"]),+       ("We are supporting floats, too.", ["(+ 10.2 1.3)", "(* 10.2 1.3)"]),+       ("you can convert a rational number to a float number with 'rtof'.", ["(rtof 1/5)"]),+       ("We can handle collections of numbers.\n We construct then with '{}'.", ["{}", "{10}","{1 2 3 4 5}"]),+       ("With a '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})"]),+       ("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 100 nats)"]),+       ("With a 'map' function, we can operate each element of the collection at onece.", ["(map (* $ 2) (take 100 nats))", "(take 100 (map (* $ 2) nats))", "(take 100 (map (modulo $ 3) nats))"]),+       ("We can create a \"partial\" function using '$' as an argument.", ["((+ $ 10) 1)"]),+       ("With a 'foldl' function, we can gather together all elements of the collection using an operator you like.\nWould you try to get a sum of from 1 to 100?", ["(foldl + 0 {1 2 3 4 5})", "(foldl * 1 {1 2 3 4 5})"]),+       ("Try to create a sequce of numbers '{1 1/2 1/3 1/4 ... 1/100}'.", []),+       ("Try to calculate '1 + 1/2 + 1/3 + 1/4 + ... + 1/100'.\nPlease remember that you can convert a rational number to a float number with 'rtof'.", ["(rtof 2/3)"]),+       ("Try to calculate '1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ... + (1/100)^2'.", [])+       ]),     ("Lv2 - Basics of functional programming",      Contents [-       ("With a 'take' function, you can extract a head part of the collection.", ["(take 0 {1 2 3 4 5})", "(take 3 {1 2 3 4 5})"]),-       ("'nats' is an infinite list that contains all natural numbers. Get a collection of natural numbers of any length you like.", ["(take 100 nats)"]),-       ("With a 'map' function, you can operate each element of the collection at onece.", ["(map (* $ 2) (take 100 nats))", "(take 100 (map (* $ 2) nats))", "(take 100 (map (quotient $ 2) nats))"]),-       ("You can create a \"partial\" function using '$' as an argument.", ["((+ $ 10) 1)"]),-       ("With a 'map2' function, you can combine two lists as follow.", ["(map2 + (take 100 nats) (take 100 nats))", "(take 100 (map2 * nats nats))"]),-       ("With a 'foldl' function, you can gather together all elements of the collection using an operator you like.\nWould you try to get a sum of from 1 to 100?", ["(foldl + 0 {1 2 3 4 5})", "(foldl * 1 {1 2 3 4 5})"]),-       ("Try to create a sequce of number '{1 1/2 1/3 1/4 ... 1/100}'.", []),-       ("Try to calculate '1 + 1/2 + 1/3 + 1/4 + ... + 1/100'.", []),-       ("Try to calculate '1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ... + (1/100)^2'.", []),-       ("You can view all library functions on collection at \"http://www.egison.org/libraries/core/collection.html\".", [])+       ("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)"]),+       ("With a 'while' function, we can extract all head elements that satisfy the predicate.\n'primes' is a infinites list that contains all prime numbers.", ["(while (lt? $ 100) primes)", "(while (lt? $ 1000) primes)"]),+       ("With a '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))"]),+       ("We use 'lambda' expressions to create functions.\n Here are simple 'lambda' examples.", ["((lambda [$x] (+ x 1)) 10)", "((lambda [$x] (* x x)) 10)", "((lambda [$x $y] (* x y)) 10 20)"]),+       ("With a 'map2' function, we can combine two lists as follow.", ["(take 100 (map2 * nats nats))", "(take 100 (map2 (lambda [$n $p] [n p]) nats primes))"]),+       ("We combine numbers using '[]'.\nThese things are called 'tuples'.", ["[1 2]", "[1 2 3]"]),+       ("Please not that a tuple that consists of only one elment is equal with that element itself.", ["[1]", "[[[1]]]"]),+       ("Try to create a sequce of tuples '{[1 1] [1 2] [1 3] [1 4] [1 5] [1 6] [1 7] [1 8] [1 9]}'.", []),+       ("Try to create a collections of sequce of tuples as follow.\n{{[1 1] [1 2] ... [1 9]}\n {[2 1] [2 2] ... [2 9]}\n ...\n {[9 1] [9 2] ... [9 9]}}", []),+       ("Try to create the multiplication table.\n{{[[1 1 1] [1 2 2] ... [1 9 9]}\n {[2 1 2] [2 2 4] ... [2 9 18]}\n ...\n {[9 1 9] [9 2 18] ... [9 9 81]}}", [])        ]),     ("Lv3 - Define your own functions",      Contents [-       ("You can bind a value to a variable with a 'define' expression.\nYou can easily get the value you binded to the variable.", ["(define $x 10)", "x"]),-       ("You 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)"]),-       ("You can write a recursive definition. Let's try that.", ["(define $odds {1 @(map (+ $ 2) odds)})", "(take 10 odds)"]),+       ("We can bind a value to a variable with a 'define' expression.\nWe can easily get the value we binded to the variable.", ["(define $x 10)", "x"]),+       ("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)"]),+       ("We can write a recursive definition. Let's try that.", ["(define $odds {1 @(map (+ $ 2) odds)})", "(take 10 odds)"]),        ("Try to define 'evens' referring to 'odds' example above.", []),-       ("You can define local variables with a 'let' expression.", ["(let {[$x 10] [$y 20]} (+ x y))"]),+       ("We can define local variables with a 'let' expression.", ["(let {[$x 10] [$y 20]} (+ x y))"]),        ("Let's try 'if' expressions.", ["(if #t 1 2)", "(let {[$x 10]} (if (eq? x 10) 1 2))"]),-       ("Using 'define' and 'if', you can write recursive functions as follow.", ["(define $your-take (lambda [$n $xs] (if (eq? n 0) {} {(car xs) @(your-take (- n 1) (cdr xs))})))", "(your-take 10 nats)"]),-       ("Try to write a 'your-map' function.\nYou may need 'empty?' function inside 'your-map' function.", ["(empty? {})"]),-       ("You can view all library functions on collection at \"http://www.egison.org/libraries/core/collection.html\".", [])+       ("Using 'define' and 'if', we can write recursive functions as follow.", ["(define $your-take (lambda [$n $xs] (if (eq? n 0) {} {(car xs) @(your-take (- n 1) (cdr xs))})))", "(your-take 10 nats)"]),+       ("Try to write a 'your-map' function.\nWe may need 'empty?' function inside 'your-map' function.", ["(empty? {})"]),+       ("We can view all library functions on collections at \"http://www.egison.org/libraries/core/collection.html\".", [])        ]),     ("Lv4 - Basic of pattern-matching",      Contents [-       ("You can do pattern-matching against multisets.", ["(match-all {1 2 3} (multiset integer) [<cons $x $xs> [x xs]])"]),-       ("You can do non-linear pattern-matching.\nTry the following expression with various targets.", ["(match-all {1 2 1 3} (multiset integer) [<cons $x <cons ,x _>> x])"]),-       ("You 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]])"]),+       ("We can do pattern-matching against multisets.", ["(match-all {1 2 3} (multiset integer) [<cons $x $xs> [x xs]])"]),+       ("We can do non-linear pattern-matching.\nTry the following expression with various targets.", ["(match-all {1 2 1 3} (multiset integer) [<cons $x <cons ,x _>> x])"]),+       ("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]])"]),        ("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])"]),        ("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])"]),        ("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]])"]),        ("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])"]),        ("'list' has a special pattern-constructor 'join'.\n'join' divides a collection into two collections.\nTry the following expressions.", ["(match-all {1 2 3 4 5} (list integer) [<join $xs $ys> [xs ys]])"]),-       ("You can enumerate two combination of numbers as follow.\nTry to enumerate three combination of numbers.", ["(match-all {1 2 3 4 5} (list integer) [<join _ <cons $x <join _ <cons $y _>>>> [x y]])"]),-       ("Did you 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]])"]),-       ("You can view a lot of demonstration of pattern-matching at \"http://www.egison.org/demonstrations/\".", [])+       ("We can enumerate two combination of numbers as follow.\nTry to enumerate three combination of numbers.", ["(match-all {1 2 3 4 5} (list integer) [<join _ <cons $x <join _ <cons $y _>>>> [x y]])"]),+       ("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]])"]),+       ("We can view a lot of demonstration of pattern-matching at \"http://www.egison.org/demonstrations/\".", [])        ]),     ("Lv5 - Pattern-matching against infinite collections",      Contents [@@ -314,8 +312,8 @@        ("We can enumerate all pythagoras numbers as follow.", ["(define $pyths (map (lambda [$x $y] (+ (* x x) (* y y))) two-combs))", "(take 100 pyths)"]),        ("We have an infinite list of prime numers in 'primes'.\nPlease check it with a 'take' function.", ["(take 10 primes)"]),        ("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)]]))"]),-       ("We can enumurate all common elements between 'primes' and 'pyths' as follow.\nCan you find a pattern in these numbers.", ["(match-all [(take 100 pyths) (take 100 primes)] [(list integer) (list integer)] [[<join _ <cons $c _>> <join _ <cons ,c _>>] c])"]),-       ("Play freely with the sequences of natural numbers.\nYou can view a lot of demonstration of pattern-matching at \"http://www.egison.org/demonstrations/\".", [])+       ("We can enumurate all common elements between 'primes' and 'pyths' as follow.\nCan we find a pattern in these numbers.", ["(match-all [(take 100 pyths) (take 100 primes)] [(list integer) (list integer)] [[<join _ <cons $c _>> <join _ <cons ,c _>>] c])"]),+       ("Play freely with the sequences of natural numbers.\nWe can view a lot of demonstration of pattern-matching at \"http://www.egison.org/demonstrations/\".", [])        ]),     ("Lv6 (preparing) - Pattern-matching against graphs",      Contents [@@ -331,3 +329,11 @@        ])   ] ++--        ("The collection after '@' in a collection is called a subcollection.", ["{1 @{2 3}}", "{1 @{2 3} @{4 @{5}} 6}"]),+--        ("We can destruct collections with 'car' and 'cdr'.", ["(car {1 2 3})", "(cdr {1 2 3})"]),+        +--        ("We can define an array as follow. We can access the element of the array using '_'.", ["(define $a [| 11 22 33 |])", "a_2"]),+--        ("We can define an hash as follow. We can access the element of the hash using '_' as arrays.", ["(define $h {| [1 11] [2 22] [3 33] |})", "h_2"]),++--       ("We can do boolean operations with 'and', 'or', 'not'.", ["(and #t #f)", "(or #t #f)", "(not #t)"]),
egison-tutorial.cabal view
@@ -1,7 +1,10 @@ Name:                egison-tutorial-Version:             3.2.2-Synopsis:            A Tutorial Program for The Programming Language Egison+Version:             3.2.3+Synopsis:            A tutorial program for the programming language Egison Description:         A tutorial program for the programming language Egison.+                     Egison is the programming langugage that realized non-linear pattern-matching with unfree data types.+                     With Egison, you can represent pattern-matching with unfree data types intuitively, especially for collection data, such as lists, multisets, sets.+                     Please learn Egison with this tutorial program! Homepage:            http://www.egison.org License:             MIT License-file:        LICENSE