packages feed

egison-tutorial 4.0.0 → 4.0.1

raw patch · 2 files changed

+44/−46 lines, 2 filesdep ~egison

Dependency ranges changed: egison

Files

Main.hs view
@@ -166,17 +166,17 @@       getNumber n  -- |Get Egison expression from the prompt. We can handle multiline input.-getEgisonExprOrNewLine :: Options -> InputT IO (Either (Maybe String) (String, EgisonTopExpr))+getEgisonExprOrNewLine :: Options -> InputT IO (Either Bool (String, EgisonTopExpr)) getEgisonExprOrNewLine opts = getEgisonExprOrNewLine' opts "" -getEgisonExprOrNewLine' :: Options -> String -> InputT IO (Either (Maybe String) (String, EgisonTopExpr))+getEgisonExprOrNewLine' :: Options -> String -> InputT IO (Either Bool (String, EgisonTopExpr)) getEgisonExprOrNewLine' opts prev = do   mLine <- case prev of              "" -> getInputLine $ optPrompt opts              _  -> getInputLine $ replicate (length $ optPrompt opts) ' '   case mLine of-    Nothing -> return $ Left Nothing-    Just [] -> return $ Left $ Just ""+    Nothing -> return $ Left False -- The user's input is 'Control-D'.+    Just [] -> return $ Left True  -- The user's input is 'Enter'.     Just line -> do       let input = prev ++ line       let parsedExpr = Parser.parseTopExpr input@@ -219,7 +219,8 @@     home <- getHomeDirectory     input <- liftIO $ runInputT (replSettings home) $ getEgisonExprOrNewLine defaultOptions     case input of-      Left Nothing -> do+      -- The user input 'Control-D'.+      Left False -> do         b <- yesOrNo "Do you want to quit?"         if b           then return ()@@ -228,7 +229,8 @@             if b               then loop env contents True               else loop env (content:contents) False-      Left (Just "") -> do+      -- The user input just 'Enter'.+      Left True -> do         b <- yesOrNo "Do you want to proceed next?"         if b           then loop env contents True@@ -292,49 +294,43 @@     Content "We can do arithmetic operations with \"+\", \"-\", \"*\", \"/\", \"^\" and \"modulo\"."      ["1 + 2", "30 - 15", "10 * 20", "20 / 5", "2 ^ 10", "modulo 17 4"]      [],-    Content "We can nest expressions."-     ["10 * 20 + 2", "10 * 20 / (10 + 20)"]-     ["Try calculating \"(100 - 1) * (100 + 1)\"."],     Content "We support rational numbers."      ["2 / 3 + 1 / 5", "42 / 84"]      [],-    Content "We support floats, too."+    Content "We support floating-point numbers, too."      ["10.2 + 1.3", "10.2 + 1"]      [],-    Content "We can convert a rational number to a float number with \"rtof\"."+    Content "We can convert a rational number to a floating-point number using \"rtof\"."      ["rtof (1 / 5)", "rtof (1 / 100)"]      [],-    Content "We can handle collections of numbers.\nWe construct collections with \"[]\"."+    Content "We can handle lists of numbers.\nWe construct a list by enclosing its elements with \"[]\"."      ["[]", "[10]", "[1, 2, 3, 4, 5]"]      [],-    Content "We can decompose a collection using the \"head\" and \"tail\" function."+    Content "We can decompose a list using the \"head\" and \"tail\" function."      ["head [1, 2, 3, 4, 5]", "tail [1, 2, 3, 4, 5]", "head (tail [1, 2, 3, 4, 5])"]-     ["Try to extract the third element of the collection \"[1, 2, 3, 4, 5]\" with \"head\" and \"tail\"."],-    Content "With the \"take\" function, we can extract a head part of a collection."-     ["take 0 [1, 2, 3, 4, 5]", "take 3 [1, 2, 3, 4, 5]"]+     ["Try to extract the third element of the list \"[1, 2, 3, 4, 5]\" with \"head\" and \"tail\"."],+    Content "Using the \"take\" function, we can extract a head part of a list."+     ["take 3 [1, 2, 3, 4, 5]", "take 0 [1, 2, 3, 4, 5]"]      [],     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 "Functions are written as \"\\x -> ... \"."-     ["(\\x -> x + 1) 2", "(\\x y -> x + y) 2 3"]+    Content "We can change an infix operator to a prefix operator by enclosing the operator by \"()\".\nThis notation is similar to the section notation in Haskell."+     ["(+) 2 3", "(/ 2) 3", "(2 /) 3"]      [],-    Content "We can use section notation of Haskell."-     ["(+) 2 3", "(* 2) 3", "(4 -) 3"]+    Content "We can create functions using the \"lambda\" notation.\nFunctions are written like \"\\x -> ... \".\n\"(\\x -> x + 2)\" is equal to \"(+ 2)\".\n\"(\\x y -> x + y)\" is equal to \"(+)\"."+     ["(\\x -> x + 2) 10", "(\\x y -> x + y) 2 3", "(\\x y -> (x + y) / 2) 10 20"]      [],-    Content "With the \"map\" function, we can operate each element of the collection at once."+    Content "The \"map\" function applies the first argument function to each element of the second argument list.\nThe \"map\" function is one of the most important function in functional programming."      ["take 100 (map (* 2) nats)", "take 100 (map (\\x -> modulo x 3) nats)"]-     [],-    Content "With the \"foldl\" function, we can gather together all elements of the collection using an operator you like."+     ["Try to create a sequence of numbers \"[1, 1/2, 1/3, 1/4, ..., 1/100]\"."],+    Content "The \"foldl\" function gathers together all elements of the third argument list using the operator specified by the first argument.\nThe second argument is an initial value.\nThe \"foldl\" function is also important in functional programming."      ["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 sequence of numbers \"[1, 1/2, 1/3, 1/4, ..., 1/100]\"."-     []-     [],-    Content "Try calculating \"1 + 1/2 + 1/3 + 1/4 + ... + 1/100\".\nRemember that we can convert a rational number to a float number with \"rtof\"."+    Content "Try to calculate \"1 + 1/2 + 1/3 + 1/4 + ... + 1/100\".\nRemember that we can convert a rational number to a floating-point number with \"rtof\"."      ["rtof (2 / 3)"]      [],-    Content "Try calculating \"1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ... + (1/100)^2\".\nIn fact, \"1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ...\" converges to \"f./ (f.* f.pi f.pi) 6.0\"."+    Content "Try to calculate \"1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ... + (1/100)^2\".\nIn fact, \"1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ...\" converges to \"pi * pi / 6\"."      []      [],     Content "This is the end of this section.\nPlease play freely or proceed to the next section.\nThank you for enjoying our tutorial!"@@ -343,25 +339,25 @@     ],   Section "Basics of functional programming"    [-    Content "We can bind a value to a variable with \":=\" (not \"=\").\nWe can easily get the value we bound to a variable."+    Content "We can bind a value to a variable using \":=\" (not \"=\")."      ["x := 10", "x", "y := 1 + x", "y"]      [],-    Content "We support recursive definitions. It enables us to define a collection with infinite elements."+    Content "We support recursive definitions.\nRecursive definitions enable us to define a list with infinitely many elements."      ["ones := 1 :: ones", "take 100 ones", "nats := 1 :: map (\\n -> n + 1) nats", "take 100 nats", "odds := 1 :: map (\\n -> n + 2) odds", "take 100 odds"]      ["Try to define the infinite list of even numbers like [2, 4, 6, 8, 10, ...]."],     Content "Let's define functions and test them."      ["increment x := x + 1", "increment 10", "multiply x y := x * y", "multiply 10 20", "fact n := foldl (*) 1 (take n nats)", "fact 10"]      [],-    Content "We can compare numbers using functions that return boolean values, \"True\" and \"False\".\nFunctions that return boolean values are called \"predicates\"."+    Content "We can compare numbers using functions, \"=\", \"<\", \"<=\", \">\", \">=\".\nThese functions return boolean values, \"True\" and \"False\".\nFunctions that return boolean values are called \"predicates\"."      ["1 = 1", "1 < 1", "1 <= 1",  "1 > 1", "1 >= 1"]      [],     Content "Using the \"takeWhile\" function, we can get the prefix of the second argument list whose elements satisfy the predicate of the first argument.\n\"primes\" is a infinite list that contains all prime numbers."      ["takeWhile (\\n -> n < 100) primes", "takeWhile (\\n -> n < 1000) primes"]      [],-    Content "With the \"filter\" function, we can extract all elements that satisfy the predicate."-     ["take 100 (filter isEven nats)", "take 100 (filter isPrime nats)", "take 100 (filter (\\p -> (modulo p 4) == 1) primes)"]+    Content "Using the \"filter\" function, we can extract all elements that satisfy the given predicate."+     ["take 100 (filter isEven nats)", "take 100 (filter isPrime nats)", "take 100 (filter (\\p -> (modulo p 4) = 1) primes)"]      ["Try to enumerate the first 100 primes that are congruent to 3 modulo 4."],-    Content "We combine objects by enclosing them by \"(\", and \")\".\nThese things are called \"tuples\".\nNote that a tuple that consists of only one element is equal to that element itself."+    Content "We can create a tuple by enclosing objects by \"()\".\n\nNote that a tuple that consists of only one element is equal to that element itself."      ["(1, 2)", "(1, 2, 3)", "(1)", "((1))"]      [],     Content "Using the \"zip\" function, we can combine two lists as follows."@@ -376,11 +372,11 @@     ],   Section "Basics of pattern matching"    [-    Content "Let's try pattern-matching for a collection.\nThe \"join\" pattern (++) divides a collection into two collections.\nNote that the matchAll expression enumerates all the decompositions."+    Content "Let's try pattern matching for a list.\nThe \"join\" pattern (++) divides a list into two lists.\nNote that the matchAll expression enumerates all the decompositions."      ["matchAll [1, 2, 3]       as list integer with $hs ++ $ts -> (hs, ts)",       "matchAll [1, 2, 3, 4, 5] as list integer with $hs ++ $ts -> (hs, ts)"]      [],-    Content "Try another pattern constructor \"cons\" (::).\nThe \"cons\" pattern (::) divides a collection into the head element and the rest collection.\n"+    Content "Try another pattern constructor \"cons\" (::).\nThe \"cons\" pattern (::) divides a list into the head element and the rest.\n"      ["matchAll [1, 2, 3]       as list integer with $x :: $xs -> (x ,xs)",       "matchAll [1, 2, 3, 4, 5] as list integer with $x :: $xs -> (x, xs)"]      [],@@ -388,7 +384,7 @@      ["matchAll [1, 2, 3]       as list integer with $x :: _ -> x",       "matchAll [1, 2, 3, 4, 5] as list integer with $hs ++ _ -> hs"]      [],-    Content "We can write non-linear patterns.\nA non-linear pattern is a pattern that allows multiple occurrences of the same variables in a pattern.\nA pattern that begins with \"#\" matches the target when it is equal with the expression after \"#\"."+    Content "We can write non-linear patterns.\nA non-linear pattern is a pattern that allows multiple occurrences of the same variables in a pattern.\nA pattern that begins with \"#\" matches the target when it is equal with the evaluation result of the expression after \"#\"."      ["matchAll [1, 1, 2, 3, 3, 2] as list integer with _ ++ $x :: #x :: _ -> x",       "matchAll [1, 1, 2, 3, 3, 2] as list integer with _ ++ $x :: #(x + 1) :: _ -> x"]      [],@@ -398,14 +394,14 @@     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 "A pattern that begins with \"!\" is called not-pattern.\nA not-pattern matches when the pattern does not match the target."+    Content "A pattern that begins with \"!\" is called not-pattern.\nA not-pattern matches when the content of the not-pattern does not match the target."      ["matchAll [1, 1, 2, 3, 3, 2] as list integer with _ ++ $x :: #x :: _ -> x",       "matchAll [1, 1, 2, 3, 3, 2] as list integer with _ ++ $x :: !#x :: _ -> x"]      [],-    Content "A pattern whose form is \"p1 & p2\" is called and-pattern.\nAn and-pattern is a pattern that matches the target if and only if both p1 and p2 matches.\nThe and-pattern in the following sample is used like an as-pattern."+    Content "A pattern whose form is \"p1 & p2\" is called and-pattern.\nAn and-pattern is a pattern that matches the target if and only if both \"p1\" and \"p2\" matches.\nThe and-pattern in the following sample is used like an as-pattern."      ["take 10 (matchAll primes as list integer with _ ++ $p :: (!#(p + 2) & $q) :: _ -> (p, q))"]      [],-    Content "A pattern whose form is \"p1 | p2\" is called or-pattern.\nAn or-pattern matches with the target, if p1 or p2 matches the target.\nIn the following sample, we enumerate prime triplets."+    Content "A pattern whose form is \"p1 | p2\" is called or-pattern.\nAn or-pattern matches with the target, if \"p1\" or \"p2\" matches the target.\nIn the following sample, we enumerate prime triplets."      ["take 10 (matchAll primes as list integer with _ ++ $p :: ($m & (#(p + 2) | #(p + 4))) :: #(p + 6) :: _ -> (p, m, (p + 6)))"]      ["What is the 20th prime triplet?"],     Content "Try to enumerate the first 4 prime quadruples whose form is (p, p + 2, p + 6, p + 8) like \"[(5, 7, 11, 13), (11, 13, 17, 19), ...]\"."@@ -417,7 +413,7 @@     ],   Section "Pattern matching for multisets and sets"    [-    Content "We can describe pattern matching for multisets and sets.\nWe can change the interpretation of patterns by changing the matcher, the second argument of the matchAll expression).\nThe meaning of the cons pattern (::) is generalized to divide a collection into \"an\" element and the rest."+    Content "We can describe pattern matching for multisets and sets.\nWe can change the interpretation of patterns by changing the matcher, the second argument of the matchAll expression.\nThe meaning of the cons pattern (::) is generalized to divide a collection into \"an\" element and the rest."      ["matchAll [1, 2, 3] as list integer     with $x :: $xs -> (x, xs)",       "matchAll [1, 2, 3] as multiset integer with $x :: $xs -> (x, xs)",       "matchAll [1, 2, 3] as set integer      with $x :: $xs -> (x, xs)"]@@ -427,7 +423,7 @@       "matchAll [1, 2, 3, 4, 5] as multiset integer with $xs ++ $ys -> (xs, ys)",       "matchAll [1, 2, 3, 4, 5] as set integer      with $xs ++ $ys -> (xs, ys)"]      [],-    Content "Try non-linear pattern matching against multiset."+    Content "Try non-linear pattern matching for multiset."      ["matchAll [1, 2, 1, 3, 2] as multiset integer with $x :: #x :: _ -> x",       "matchAll [1, 2, 1, 3, 2] as multiset integer with $x :: #(x + 2) :: _ -> x",       "matchAll [1, 2, 1, 3, 2] as multiset integer with $x :: !(#(x + 2) :: _) -> x"]@@ -437,7 +433,7 @@       "matchAll [1..30] as multiset integer with $x :: #x :: #x :: _ -> x",       "matchAll [1..30] as multiset integer with $x :: #x :: #x :: #x _ -> x"]      [],-    Content "Egison is designed to enumerate all the infinitely many pattern-matching results.\nThe following samples enumerate pairs and triplets of natural numbers."+    Content "Egison is designed to enumerate all the infinitely many pattern-matching results.\nThe following samples enumerate all the pairs and triplets of natural numbers."      ["matchAll nats as set integer with $x :: $y :: _ -> (x, y)",       "matchAll nats as set integer with $x :: $y :: $z :: _ -> (x, y, z)"]      [],
egison-tutorial.cabal view
@@ -1,5 +1,5 @@ Name:                egison-tutorial-Version:             4.0.0+Version:             4.0.1 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.@@ -12,15 +12,16 @@ Maintainer:          Satoshi Egi <egi@egison.org> Category:            Compilers/Interpreters Build-type:          Simple-Cabal-version:       >=1.8+Cabal-version:       2.0  source-repository head   type: git   location: https://github.com/egison/egison-tutorial.git  Executable egison-tutorial+  default-language:    Haskell2010   Main-is:             Main.hs-  Build-depends: egison >= 4.0.0+  Build-depends: egison >= 4.0.3                , base >= 4.0 && < 5                , haskeline                , transformers@@ -30,3 +31,4 @@                , regex-posix   ghc-options:  -Wall -Wno-name-shadowing   Other-modules:   Paths_egison_tutorial+  autogen-modules: Paths_egison_tutorial