hilbert 0.0.2700000

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +15 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +23 -0
  4. data/.rubocop.yml +25 -0
  5. data/.travis.yml +5 -0
  6. data/Gemfile +9 -0
  7. data/Guardfile +33 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +159 -0
  10. data/Rakefile +20 -0
  11. data/bin/qlang +29 -0
  12. data/core/Q/Lexer.hs +9 -0
  13. data/core/Q/Parser.hs +9 -0
  14. data/core/Q.hs +19 -0
  15. data/ext/qlang/QMatrix/q_matrix.c +0 -0
  16. data/ext/qlang/extconf.rb +3 -0
  17. data/ext/qlang/qlang.c +65 -0
  18. data/ext/qlang/qlang.h +6 -0
  19. data/legacy_rspec/langs/Haskell/ex1_after.hs +74 -0
  20. data/legacy_rspec/langs/Haskell/ex1_before.hs +74 -0
  21. data/legacy_rspec/langs/Python/ex1_after.py +93 -0
  22. data/legacy_rspec/langs/Python/ex1_before.py +95 -0
  23. data/legacy_rspec/langs/R/ex1_after.R +89 -0
  24. data/legacy_rspec/langs/R/ex1_before.R +89 -0
  25. data/legacy_rspec/objects/list_spec.rb +31 -0
  26. data/legacy_rspec/objects/matrix_spec.rb +55 -0
  27. data/legacy_rspec/objects/vector_spec.rb +47 -0
  28. data/lib/qlang/api/func_api.rb +17 -0
  29. data/lib/qlang/api/integral_api.rb +16 -0
  30. data/lib/qlang/api/limit_api.rb +22 -0
  31. data/lib/qlang/api/list_api.rb +16 -0
  32. data/lib/qlang/api/matrix_api.rb +20 -0
  33. data/lib/qlang/api/sigma_api.rb +16 -0
  34. data/lib/qlang/api/vector_api.rb +19 -0
  35. data/lib/qlang/api.rb +23 -0
  36. data/lib/qlang/exec.rb +64 -0
  37. data/lib/qlang/iq.rb +45 -0
  38. data/lib/qlang/lexer/base.rb +107 -0
  39. data/lib/qlang/lexer/formula_lexer.rb +20 -0
  40. data/lib/qlang/lexer/main_lexer.rb +34 -0
  41. data/lib/qlang/lexer/tokens.rb +94 -0
  42. data/lib/qlang/lexer.rb +11 -0
  43. data/lib/qlang/meta_info.rb +27 -0
  44. data/lib/qlang/parser/base.rb +7 -0
  45. data/lib/qlang/parser/formula_parser.rb +36 -0
  46. data/lib/qlang/parser/func_parser.rb +15 -0
  47. data/lib/qlang/parser/integral_parser.rb +15 -0
  48. data/lib/qlang/parser/limit_parser.rb +16 -0
  49. data/lib/qlang/parser/list_parser.rb +12 -0
  50. data/lib/qlang/parser/matrix_parser.rb +17 -0
  51. data/lib/qlang/parser/sigma_parser.rb +17 -0
  52. data/lib/qlang/parser/vector_parser.rb +13 -0
  53. data/lib/qlang/parser.rb +101 -0
  54. data/lib/qlang/utils/langs.yml +7 -0
  55. data/lib/qlang/utils/ruby_ext.rb +54 -0
  56. data/lib/qlang/version.rb +3 -0
  57. data/lib/qlang.rb +37 -0
  58. data/qlang.gemspec +28 -0
  59. data/test/internal/test_tokens.rb +35 -0
  60. data/test/interpreter/base.rb +17 -0
  61. data/test/interpreter/test_differential.rb +43 -0
  62. data/test/interpreter/test_function.rb +44 -0
  63. data/test/interpreter/test_general.rb +12 -0
  64. data/test/interpreter/test_integral.rb +38 -0
  65. data/test/interpreter/test_limit.rb +37 -0
  66. data/test/interpreter/test_matrix.rb +70 -0
  67. data/test/interpreter/test_sigma.rb +25 -0
  68. data/test/interpreter/test_vector.rb +28 -0
  69. data/test/langs/test_r.rb +32 -0
  70. data/test/langs/test_ruby.rb +9 -0
  71. data/test/minitest_helper.rb +8 -0
  72. data/test/q_matrix/test_q_matrix.rb +13 -0
  73. data/test/test_qlang.rb +24 -0
  74. metadata +203 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWNmNzJhODczNmNmYTAzZTEwZDMzMzAwMzY3YjYyODk1MGZkZTQzYg==
5
+ data.tar.gz: !binary |-
6
+ NzY5Mjc1OWIyMjcyNDczZGYzZTljN2UyMDNjMzViMTVjZWQzN2U4Mw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjUzYzk2MWFjNzk5NmJkMzExNTk0YzM2ZDU1N2QzODZiZGNkOTQ3OWQwNTg3
10
+ YWY4MDA4YzZiYmQ4NjZkYmM1NmY2MzM4Y2I3NmQyZmE4MWJhZjlmYTZiZDU2
11
+ Y2Y1NWYxODdiMjFjMjZkMWQ2YWQ0Njc1NTUzNGVjYmNkODQ1OGY=
12
+ data.tar.gz: !binary |-
13
+ ZjNmMTBlZGMxNzc5OGQzNTVmOTg3MzAwYWNlOWJjMzNjMjkzZWYwN2U1Zjkz
14
+ NDVmZDQxODFlYzU1YzJjMmI5NjliOTI0MzM3YTM3NjdmZmFhYjU4M2NmZDc5
15
+ YTEzYzRmMWQxMjY3YzFjMGI5YzY2ZDA3MjdhYTIwNTgwMGVkZjI=
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ repo_token: ZdXKADwEPNW5PJkIaC76yjqq4w04dIpuZ
2
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .ruby-version
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ Eval:
2
+ Enabled: false
3
+
4
+ LineLength:
5
+ Enabled: false
6
+
7
+ # Configuration parameters: AllowedVariables.
8
+ GlobalVars:
9
+ Enabled: false
10
+
11
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
12
+ MethodName:
13
+ Enabled: false
14
+
15
+ OpMethod:
16
+ Enabled: false
17
+
18
+ Void:
19
+ Enabled: false
20
+
21
+ SpaceInsideParens:
22
+ Enabled: false
23
+
24
+ SpaceAroundOperators:
25
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.0.0
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rubocop'
4
+ gem 'coveralls', require: false
5
+ gem 'pry'
6
+ gem 'guard'
7
+ gem 'guard-rspec', require: false
8
+
9
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,33 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ # Note: The cmd option is now required due to the increasing number of ways
5
+ # rspec may be run, below are examples of the most common uses.
6
+ # * bundler: 'bundle exec rspec'
7
+ # * bundler binstubs: 'bin/rspec'
8
+ # * spring: 'bin/rsspec' (This will use spring if running and you have
9
+ # installed the spring binstubs per the docs)
10
+ # * zeus: 'zeus rspec' (requires the server to be started separetly)
11
+ # * 'just' rspec: 'rspec'
12
+ guard :rspec, cmd: 'bundle exec rspec' do
13
+ watch(%r{^spec/.+_spec\.rb$})
14
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
15
+ watch('spec/spec_helper.rb') { "spec" }
16
+
17
+ # Rails example
18
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
19
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
20
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
21
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
22
+ watch('config/routes.rb') { "spec/routing" }
23
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
24
+ watch('spec/rails_helper.rb') { "spec" }
25
+
26
+ # Capybara features specs
27
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
28
+
29
+ # Turnip features and steps
30
+ watch(%r{^spec/acceptance/(.+)\.feature$})
31
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
32
+ end
33
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 gogotanaka
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # Qlang
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/qlang.svg)](http://badge.fury.io/rb/qlang) [![Build Status](https://travis-ci.org/gogotanaka/Q.svg?branch=master)](https://travis-ci.org/gogotanaka/Q) [![Coverage Status](https://coveralls.io/repos/gogotanaka/Q/badge.png?branch=master)](https://coveralls.io/r/gogotanaka/Q?branch=master) [![Code Climate](https://codeclimate.com/github/gogotanaka/Q/badges/gpa.svg)](https://codeclimate.com/github/gogotanaka/Q) [![Dependency Status](https://gemnasium.com/gogotanaka/Q.svg)](https://gemnasium.com/gogotanaka/Q)
4
+
5
+ ## Do you know the one best language in this world?
6
+
7
+ #### I believe mathematics is absolutely that one.
8
+
9
+ ## How can we deal something as great as mathematics in a discrete world?
10
+
11
+ #### Q-language is the answer.
12
+
13
+ Q lets you have a sense of mathematics using a keyboard, the same as you would with a pen.
14
+
15
+ ```
16
+ +---Discrete world---+ +------Mathematics-------+
17
+ | Ruby | | axiom |
18
+ | TeX |<------ Q ------>| Uncountable noun |
19
+ | Python | | real number topology |
20
+ +--------------------+ +------------------------+
21
+ ```
22
+
23
+ ## Demo
24
+
25
+ The code below is input and output for the q-lang interpreter
26
+
27
+ (you can try it by `qlang -i`)
28
+
29
+ ### Differentiate
30
+
31
+ ```
32
+ d/dx(cos(x))
33
+ => ( - sin( x ) )
34
+
35
+ # You can omit parentheses
36
+
37
+ d/dx log(x)
38
+ => ( 1 / x )
39
+
40
+ d/dy xy
41
+ => ( x )
42
+
43
+ d/dx e^x
44
+ => e ^ x
45
+ ```
46
+
47
+ ### Integrate
48
+
49
+ ```
50
+ S(log(x)dx)[0..1]
51
+ => - oo
52
+
53
+ S(sin(x)dx)[0..pi]
54
+ => 2.0
55
+
56
+ S(cos(x)dx)[0..pi]
57
+ => 0.0
58
+ ```
59
+
60
+ ### Limit
61
+
62
+ ```
63
+ lim[x->oo] (1 + 1/x)^x
64
+ => 2.7182682371744895
65
+
66
+ lim[x->0] 1/x
67
+ => oo
68
+ ```
69
+
70
+ ### Sigma
71
+ ```
72
+ ∑[x=0,10] x
73
+ => 55.0
74
+ ```
75
+
76
+ ### Matrix
77
+
78
+ ```
79
+ (1 2 3; 4 5 6)
80
+ => (1 2 3; 4 5 6)
81
+
82
+ (1 2 3; 4 5 6) + (1 2 3; 4 5 6)
83
+ => (2 4 6; 8 10 12)
84
+
85
+ (1 2 3; 4 5 6) * (1 2 3)
86
+ => (14 32)
87
+ ```
88
+
89
+ ### Function
90
+ ```
91
+ f(x, y) = xy
92
+ f(1, 2)
93
+ => 2
94
+ ```
95
+
96
+
97
+ ## How to use
98
+
99
+ Install qlang gem.
100
+
101
+ $ gem install qlang
102
+
103
+ ## Interpreter
104
+
105
+ $ qlang -i
106
+ Q:->
107
+
108
+ ## Use as native language
109
+
110
+ ### Compile into R
111
+
112
+ $ qlang -r foo.q
113
+
114
+ ### Compile into Ruby
115
+
116
+ $ qlang -rb foo.q
117
+
118
+ ### Compile into Python
119
+
120
+ $ qlang -py foo.q
121
+
122
+
123
+ ## Use as math template within other langs
124
+
125
+
126
+ ```rb
127
+ class ExampleClass
128
+ def example_method
129
+ #your Ruby codes
130
+ ......
131
+
132
+ I love mathematics.
133
+ a = (1 3 4)
134
+ # your Q codes
135
+ Q.E.D
136
+
137
+ end
138
+ end
139
+ ```
140
+
141
+ $ qlang -rb example.rb
142
+
143
+
144
+ ```rb
145
+ class ExampleClass
146
+ def example_method
147
+ #your Ruby codes
148
+ ......
149
+
150
+ a = Vector[1, 3, 4]
151
+
152
+ end
153
+ end
154
+ ```
155
+
156
+ ## Contributing
157
+
158
+ Any PRs or issues are welcome.
159
+ You can become a commiter, even if you only commit once.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/test_*.rb']
7
+ end
8
+
9
+ require 'rake/extensiontask'
10
+
11
+ Rake::ExtensionTask.new('qlang') do |ext|
12
+ ext.lib_dir = 'lib/qlang'
13
+ end
14
+
15
+ task :compile_and_test do
16
+ Rake::Task['compile'].invoke
17
+ Rake::Task['test'].invoke
18
+ end
19
+
20
+ task default: :compile_and_test
data/bin/qlang ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'qlang'
4
+
5
+ # Q command line
6
+ require 'qlang/exec'
7
+
8
+ # Q interpreter
9
+ require 'qlang/iq'
10
+
11
+ # TODO: There are vanch of todo ..
12
+ case ARGV.first
13
+ when '-i'
14
+ loop do
15
+ print 'Q:-> '
16
+ begin
17
+ input = $stdin.gets
18
+ output = Qlang::Iq.execute(input)
19
+ $stdout.puts output
20
+ rescue => e
21
+ puts e
22
+ end
23
+ end
24
+ when '-v'
25
+ puts Qlang::VERSION
26
+ when '-r', '-rb', '-py'
27
+ exec = Qlang::Exec::Compiler.new(ARGV)
28
+ exec.output!
29
+ end
data/core/Q/Lexer.hs ADDED
@@ -0,0 +1,9 @@
1
+ {- |
2
+ Module : Language.Q
3
+ Copyright : Kazuki Tanaka (a.k.a gogotanaka)
4
+ Licence : MIT
5
+
6
+ q-language.org.
7
+ -}
8
+
9
+ module Q.Lexer where
data/core/Q/Parser.hs ADDED
@@ -0,0 +1,9 @@
1
+ {- |
2
+ Module : Language.Q
3
+ Copyright : Kazuki Tanaka (a.k.a gogotanaka)
4
+ Licence : MIT
5
+
6
+ q-language.org.
7
+ -}
8
+
9
+ module Q.Parser where
data/core/Q.hs ADDED
@@ -0,0 +1,19 @@
1
+ {- |
2
+ Module : Language.Q
3
+ Copyright : Kazuki Tanaka (a.k.a gogotanaka)
4
+ Licence : MIT
5
+
6
+ q-language.org.
7
+ -}
8
+
9
+ module Q
10
+ ( module Q.Lexer
11
+ , module Q.Parser
12
+ , version
13
+ ) where
14
+
15
+
16
+ import Q.Lexer
17
+ import Q.Parser
18
+
19
+ import Data.Version
File without changes
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('qlang/qlang')
data/ext/qlang/qlang.c ADDED
@@ -0,0 +1,65 @@
1
+ #include "qlang.h"
2
+ #include <stdio.h>
3
+ #define Need_Float(x) do {if (!RB_TYPE_P(x, T_FLOAT)) {(x) = rb_to_float(x);}} while(0)
4
+ #define GET_FLOAT(x) Need_Float(x);(x) = RFLOAT_VALUE(x)
5
+
6
+ double func(double x)
7
+ {
8
+ return((x * x) * 3);
9
+ }
10
+
11
+ static VALUE
12
+ rb_func(VALUE self, VALUE x)
13
+ {
14
+ GET_FLOAT(x);
15
+ return(DBL2NUM((x * x)/2));
16
+ }
17
+
18
+
19
+ static VALUE
20
+ execute(VALUE self, VALUE a, VALUE b, VALUE n)
21
+ {
22
+ GET_FLOAT(a);
23
+ GET_FLOAT(b);
24
+ GET_FLOAT(n);
25
+
26
+ int i;
27
+ double s1=0.0, s2=0.0, d;
28
+ double x, y[n+1];
29
+ d=(b-a)/(double)n;
30
+ for(i=0; i<=n; i++)
31
+ {
32
+ x=(double)i*d+a;
33
+ y[i]=func(x);
34
+ }
35
+ for(i=1; i<=n-1; i+=2)
36
+ {
37
+ s1+=y[i];
38
+ }
39
+ for(i=2; i<=n-2; i+=2)
40
+ {
41
+ s2+=y[i];
42
+ }
43
+
44
+ double s=(y[0]+4.0*s1+2.0*s2+y[n])*d/3.0;
45
+
46
+ return DBL2NUM(s);
47
+ }
48
+
49
+
50
+
51
+ void
52
+ Init_qlang(void)
53
+ {
54
+ VALUE rb_mQMatrix = rb_define_class("QMatrix", rb_cObject);
55
+ rb_define_method(rb_mQMatrix, "execute", execute, 3);
56
+ rb_define_method(rb_mQMatrix, "func", rb_func, 1);
57
+ }
58
+
59
+ // VALUE rb_mQMatrix;
60
+
61
+ // void
62
+ // Init_q_matrix(void)
63
+ // {
64
+ // rb_mQMatrix = rb_define_module("QMatrix");
65
+ // }
data/ext/qlang/qlang.h ADDED
@@ -0,0 +1,6 @@
1
+ #ifndef QLANG_H
2
+ #define QLANG_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* QLANG_H */
@@ -0,0 +1,74 @@
1
+ module Main where
2
+
3
+ filter' :: (a -> Bool) -> [a] -> [a]
4
+ filter' _ [] = []
5
+ filter' p (x:xs) | p x = x : filter' p xs
6
+ | otherwise = filter' p xs
7
+
8
+
9
+ -- pattern match for Algebraic data type
10
+ data Node = Leaf Integer | Branch Node Node deriving (Show)
11
+
12
+ depth :: Node -> Integer
13
+ depth node = case node of
14
+ Leaf _ -> 1
15
+ Branch a b -> 1 + max (depth a) (depth b)
16
+
17
+ sumLeaf :: Node -> Integer
18
+ sumLeaf node = case node of
19
+ Leaf x -> x
20
+ Branch a b -> (sumLeaf a) + (sumLeaf b)
21
+
22
+ -- Maybe monado
23
+
24
+ --data Maybee a = Justt a | Nothingg deriving (Show)
25
+
26
+ --return :: a -> Maybee a
27
+ --return a = Maybee a
28
+
29
+ --(>>=) :: Maybee a -> (a -> Maybee b) -> Maybee b
30
+ --Nothingg >>= _ = Nothingg
31
+ --Justt a >>= f = f a
32
+
33
+ add1 :: Maybe Int -> Maybe Int -> Maybe Int
34
+ add1 mx my =
35
+ case mx of
36
+ Nothing -> Nothing
37
+ Just x -> case my of
38
+ Nothing -> Nothing
39
+ Just y -> Just (x + y)
40
+
41
+ add2 :: Maybe Int -> Maybe Int -> Maybe Int
42
+ add2 mx my =
43
+ mx >>= (\x ->
44
+ my >>= (\y ->
45
+ return (x + y)))
46
+
47
+ add3 :: Maybe Int -> Maybe Int -> Maybe Int
48
+ add3 mx my = do
49
+ x <- mx
50
+ y <- my
51
+ return (x + y)
52
+
53
+
54
+ putResult x = putStrLn $ show x
55
+
56
+
57
+ main = do putResult $ filter' (> 2) [1..10]
58
+
59
+ let tree = Branch (Leaf 3) (Branch (Leaf 2) (Leaf 4))
60
+ putResult $ depth tree
61
+ putResult $ sumLeaf tree
62
+
63
+ putResult $ (Just 1) `add1` Nothing
64
+ putResult $ Nothing `add1` (Just 3)
65
+ putResult $ (Just 1) `add1` (Just 3)
66
+ putResult $ Nothing `add1` Nothing
67
+ putResult $ (Just 1) `add2` Nothing
68
+ putResult $ Nothing `add2` (Just 3)
69
+ putResult $ (Just 1) `add2` (Just 3)
70
+ putResult $ Nothing `add2` Nothing
71
+ putResult $ (Just 1) `add3` Nothing
72
+ putResult $ Nothing `add3` (Just 3)
73
+ putResult $ (Just 1) `add3` (Just 3)
74
+ putResult $ Nothing `add3` Nothing
@@ -0,0 +1,74 @@
1
+ module Main where
2
+
3
+ filter' :: (a -> Bool) -> [a] -> [a]
4
+ filter' _ [] = []
5
+ filter' p (x:xs) | p x = x : filter' p xs
6
+ | otherwise = filter' p xs
7
+
8
+
9
+ -- pattern match for Algebraic data type
10
+ data Node = Leaf Integer | Branch Node Node deriving (Show)
11
+
12
+ depth :: Node -> Integer
13
+ depth node = case node of
14
+ Leaf _ -> 1
15
+ Branch a b -> 1 + max (depth a) (depth b)
16
+
17
+ sumLeaf :: Node -> Integer
18
+ sumLeaf node = case node of
19
+ Leaf x -> x
20
+ Branch a b -> (sumLeaf a) + (sumLeaf b)
21
+
22
+ -- Maybe monado
23
+
24
+ --data Maybee a = Justt a | Nothingg deriving (Show)
25
+
26
+ --return :: a -> Maybee a
27
+ --return a = Maybee a
28
+
29
+ --(>>=) :: Maybee a -> (a -> Maybee b) -> Maybee b
30
+ --Nothingg >>= _ = Nothingg
31
+ --Justt a >>= f = f a
32
+
33
+ add1 :: Maybe Int -> Maybe Int -> Maybe Int
34
+ add1 mx my =
35
+ case mx of
36
+ Nothing -> Nothing
37
+ Just x -> case my of
38
+ Nothing -> Nothing
39
+ Just y -> Just (x + y)
40
+
41
+ add2 :: Maybe Int -> Maybe Int -> Maybe Int
42
+ add2 mx my =
43
+ mx >>= (\x ->
44
+ my >>= (\y ->
45
+ return (x + y)))
46
+
47
+ add3 :: Maybe Int -> Maybe Int -> Maybe Int
48
+ add3 mx my = do
49
+ x <- mx
50
+ y <- my
51
+ return (x + y)
52
+
53
+
54
+ putResult x = putStrLn $ show x
55
+
56
+
57
+ main = do putResult $ filter' (> 2) [1..10]
58
+
59
+ let tree = Branch (Leaf 3) (Branch (Leaf 2) (Leaf 4))
60
+ putResult $ depth tree
61
+ putResult $ sumLeaf tree
62
+
63
+ putResult $ (Just 1) `add1` Nothing
64
+ putResult $ Nothing `add1` (Just 3)
65
+ putResult $ (Just 1) `add1` (Just 3)
66
+ putResult $ Nothing `add1` Nothing
67
+ putResult $ (Just 1) `add2` Nothing
68
+ putResult $ Nothing `add2` (Just 3)
69
+ putResult $ (Just 1) `add2` (Just 3)
70
+ putResult $ Nothing `add2` Nothing
71
+ putResult $ (Just 1) `add3` Nothing
72
+ putResult $ Nothing `add3` (Just 3)
73
+ putResult $ (Just 1) `add3` (Just 3)
74
+ putResult $ Nothing `add3` Nothing