cohi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/License.txt +1 -0
- data/Manifest.txt +13 -0
- data/README.txt +39 -0
- data/Rakefile +123 -0
- data/example/fact.rb +45 -0
- data/example/fib.rb +42 -0
- data/lib/cohi.rb +95 -0
- data/lib/cohi/prelude.rb +1011 -0
- data/lib/cohi/version.rb +9 -0
- data/setup.rb +1585 -0
- data/test/test_cohi.rb +403 -0
- data/test/test_helper.rb +3 -0
- metadata +63 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Ruby's License
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
Cohi - ���^�v���O���~���O�x�����C�u����
|
3
|
+
===========================================
|
4
|
+
|
5
|
+
������ĉ��H
|
6
|
+
-----------
|
7
|
+
Ruby�Ŋ��^�v���O���~���O�����邽�߂֗̕����C�u�����ł��B
|
8
|
+
Haskell�̉e���𑽕��ɎĂ��܂��B
|
9
|
+
|
10
|
+
�l��Haskell(��Ruby)������邽�߂ɍ��܂����B�Ƃ�����
|
11
|
+
Ruby�łǂ��܂�Haskell�ɋߕt���邩�Ȃ��Ǝv���Ȃ�����܂����B
|
12
|
+
���\�͓�̎��ł��B
|
13
|
+
|
14
|
+
Cohi�̌ꌹ�͈ȉ��ł��B
|
15
|
+
lambda �� lamb �� ���Ђ� �� Cohi
|
16
|
+
|
17
|
+
�ŋߗ��s���MDD(Mascot Driven Development)���ӎ����Ȃ��疽�����܂����B
|
18
|
+
���A�}�X�R�b�g�͂܂�����܂���B��W���ł��B
|
19
|
+
|
20
|
+
�C���X�g�[��
|
21
|
+
------------
|
22
|
+
Gem�ŃC���X�g�[�����Ă��������B
|
23
|
+
|
24
|
+
$ gem install cohi
|
25
|
+
|
26
|
+
�T���v��
|
27
|
+
--------
|
28
|
+
example�f�B���N�g���������������B
|
29
|
+
|
30
|
+
�T�|�[�g
|
31
|
+
--------
|
32
|
+
��������܂����牺�L�܂ł��肢���܂��B
|
33
|
+
<tanaka.shinya@gmail.com>
|
34
|
+
|
35
|
+
�ӎ�
|
36
|
+
----
|
37
|
+
���̃��C�u�����́A�т킱�J�����h(2007/06/30�`2007/07/01)��
|
38
|
+
�����ɂ܂����ĊJ�����܂����B������yhara���܁A��������������
|
39
|
+
�Q���҂̊F���܂Ɋ��ӂ��܂��B
|
data/Rakefile
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'fileutils'
|
10
|
+
require 'hoe'
|
11
|
+
|
12
|
+
include FileUtils
|
13
|
+
require File.join(File.dirname(__FILE__), 'lib', 'cohi', 'version')
|
14
|
+
|
15
|
+
AUTHOR = 'TANAKA Shin-ya(id:ha-tan)' # can also be an array of Authors
|
16
|
+
EMAIL = "tanaka.shinya@gmail.com"
|
17
|
+
DESCRIPTION = "Cohi is a tiny library for aiding functional programming."
|
18
|
+
GEM_NAME = 'cohi' # what ppl will type to install your gem
|
19
|
+
|
20
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
21
|
+
@config = nil
|
22
|
+
def rubyforge_username
|
23
|
+
unless @config
|
24
|
+
begin
|
25
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
26
|
+
rescue
|
27
|
+
puts <<-EOS
|
28
|
+
ERROR: No rubyforge config file found: #{@config_file}"
|
29
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
30
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
31
|
+
EOS
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@rubyforge_username ||= @config["username"]
|
36
|
+
end
|
37
|
+
|
38
|
+
RUBYFORGE_PROJECT = 'cohi' # The unix name for your project
|
39
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
40
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
41
|
+
|
42
|
+
NAME = "cohi"
|
43
|
+
REV = nil
|
44
|
+
# UNCOMMENT IF REQUIRED:
|
45
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
46
|
+
VERS = Cohi::VERSION::STRING + (REV ? ".#{REV}" : "")
|
47
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
|
48
|
+
RDOC_OPTS = ['--quiet', '--title', 'cohi documentation',
|
49
|
+
"--opname", "index.html",
|
50
|
+
"--line-numbers",
|
51
|
+
"--main", "README",
|
52
|
+
"--inline-source"]
|
53
|
+
|
54
|
+
class Hoe
|
55
|
+
def extra_deps
|
56
|
+
@extra_deps.reject { |x| Array(x).first == 'hoe' }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Generate all the Rake tasks
|
61
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
62
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
63
|
+
p.author = AUTHOR
|
64
|
+
p.description = DESCRIPTION
|
65
|
+
p.email = EMAIL
|
66
|
+
p.summary = DESCRIPTION
|
67
|
+
p.url = HOMEPATH
|
68
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
69
|
+
p.test_globs = ["test/**/test_*.rb"]
|
70
|
+
p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
|
71
|
+
|
72
|
+
# == Optional
|
73
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
74
|
+
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
75
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
76
|
+
end
|
77
|
+
|
78
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
|
79
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
80
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
81
|
+
|
82
|
+
desc 'Generate website files'
|
83
|
+
task :website_generate do
|
84
|
+
Dir['website/**/*.txt'].each do |txt|
|
85
|
+
sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
desc 'Upload website files to rubyforge'
|
90
|
+
task :website_upload do
|
91
|
+
host = "#{rubyforge_username}@rubyforge.org"
|
92
|
+
remote_dir = "/var/www/gforge-projects/#{PATH}/"
|
93
|
+
local_dir = 'website'
|
94
|
+
sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
|
95
|
+
end
|
96
|
+
|
97
|
+
desc 'Generate and upload website files'
|
98
|
+
task :website => [:website_generate, :website_upload, :publish_docs]
|
99
|
+
|
100
|
+
desc 'Release the website and new gem version'
|
101
|
+
task :deploy => [:check_version, :website, :release] do
|
102
|
+
puts "Remember to create SVN tag:"
|
103
|
+
puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
|
104
|
+
"svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
|
105
|
+
puts "Suggested comment:"
|
106
|
+
puts "Tagging release #{CHANGES}"
|
107
|
+
end
|
108
|
+
|
109
|
+
desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
|
110
|
+
task :local_deploy => [:website_generate, :install_gem]
|
111
|
+
|
112
|
+
task :check_version do
|
113
|
+
unless ENV['VERSION']
|
114
|
+
puts 'Must pass a VERSION=x.y.z release version'
|
115
|
+
exit
|
116
|
+
end
|
117
|
+
unless ENV['VERSION'] == VERS
|
118
|
+
puts "Please update your version.rb to match the release version, currently #{VERS}"
|
119
|
+
exit
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
data/example/fact.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- compile-command: "ruby fact.rb" -*-
|
3
|
+
|
4
|
+
$LOAD_PATH << '../lib'
|
5
|
+
require 'cohi'
|
6
|
+
require 'cohi/prelude'
|
7
|
+
|
8
|
+
include Cohi
|
9
|
+
|
10
|
+
# (1)
|
11
|
+
define(:fact1) do |n|
|
12
|
+
if n == 0
|
13
|
+
1
|
14
|
+
else
|
15
|
+
n * fact1[n - 1]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
p fact1[5] # => 120
|
19
|
+
|
20
|
+
# (2)
|
21
|
+
define(:fact2, [0]) do
|
22
|
+
1
|
23
|
+
end
|
24
|
+
define(:fact2, [X]) do |n|
|
25
|
+
n * fact2[n - 1]
|
26
|
+
end
|
27
|
+
p fact2[5] # => 120
|
28
|
+
|
29
|
+
# (3)
|
30
|
+
define(:fact3) do |n|
|
31
|
+
foldr[mult, 1, (1 .. n).to_a]
|
32
|
+
end
|
33
|
+
p fact3[5] # => 120
|
34
|
+
|
35
|
+
# (4)
|
36
|
+
define(:fact4) do |n|
|
37
|
+
foldl1[mult, (1 .. n).to_a]
|
38
|
+
end
|
39
|
+
p fact4[5] # => 120
|
40
|
+
|
41
|
+
# (5)
|
42
|
+
define(:fact5) do |n|
|
43
|
+
last[scanl1[mult, (1 .. n).to_a]]
|
44
|
+
end
|
45
|
+
p fact5[5] # => 120
|
data/example/fib.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- compile-command: "ruby fib.rb" -*-
|
3
|
+
|
4
|
+
$LOAD_PATH << '../lib'
|
5
|
+
require 'cohi'
|
6
|
+
require 'cohi/prelude'
|
7
|
+
require 'benchmark'
|
8
|
+
|
9
|
+
include Cohi
|
10
|
+
|
11
|
+
define(:fib, [0]) { 0 }
|
12
|
+
define(:fib, [1]) { 1 }
|
13
|
+
define(:fib, [2]) { 1 }
|
14
|
+
define(:fib) {|n| fib[n - 1] + fib[n - 2] }
|
15
|
+
|
16
|
+
# ref. http://kerolin.jspeed.jp/Computer/Linux/fib060810.html
|
17
|
+
define(:fib_step, [[X, X]]) do |u, v|
|
18
|
+
[v, u + v]
|
19
|
+
end
|
20
|
+
|
21
|
+
define(:fib_pair, [0]) do
|
22
|
+
[0, 1]
|
23
|
+
end
|
24
|
+
define(:fib_pair, [X]) do |n|
|
25
|
+
fib_step[fib_pair[n - 1]]
|
26
|
+
end
|
27
|
+
|
28
|
+
define(:fast_fib) do |n|
|
29
|
+
fst[fib_pair[n]]
|
30
|
+
end
|
31
|
+
|
32
|
+
n = 20
|
33
|
+
Benchmark.bm do |x|
|
34
|
+
x.report { fib[n] }
|
35
|
+
x.report { fast_fib[n] }
|
36
|
+
end
|
37
|
+
# user system total real
|
38
|
+
# 0.510000 0.000000 0.510000 ( 0.591000)
|
39
|
+
# 0.000000 0.000000 0.000000 ( 0.000000)
|
40
|
+
|
41
|
+
|
42
|
+
|
data/lib/cohi.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
module Cohi
|
2
|
+
FUNCS = {}
|
3
|
+
|
4
|
+
X = Object.new
|
5
|
+
XS = Object.new
|
6
|
+
X_XS = Object.new
|
7
|
+
|
8
|
+
class Function
|
9
|
+
def initialize(sym)
|
10
|
+
@sym = sym
|
11
|
+
@func = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(pat, &f)
|
15
|
+
@func.push([pat, f])
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](*xs)
|
19
|
+
@func.each do |func|
|
20
|
+
pat, f = *func
|
21
|
+
xs2 = match_pattern?(pat, xs)
|
22
|
+
return f[*xs2] if xs2
|
23
|
+
end
|
24
|
+
raise(ArgumentError, "no match pattern. `#{@sym}' #{xs.inspect}")
|
25
|
+
end
|
26
|
+
|
27
|
+
def match_pattern?(pat, xs)
|
28
|
+
return xs unless pat
|
29
|
+
return nil if not pat.include?(XS) and pat.size != xs.size
|
30
|
+
rs = []
|
31
|
+
pat.each_with_index do |pt, i|
|
32
|
+
x = xs[i]
|
33
|
+
case pt
|
34
|
+
when Array
|
35
|
+
xs3 = match_pattern?(pt, x)
|
36
|
+
return nil unless xs3
|
37
|
+
rs += xs3
|
38
|
+
when X
|
39
|
+
rs << x
|
40
|
+
next
|
41
|
+
when XS
|
42
|
+
rs << [x, *xs[i + 1 .. -1]]
|
43
|
+
xs = []
|
44
|
+
next
|
45
|
+
when X_XS
|
46
|
+
y, *ys = *x
|
47
|
+
rs << y
|
48
|
+
rs << ys
|
49
|
+
next
|
50
|
+
when x
|
51
|
+
next
|
52
|
+
else
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
rs
|
57
|
+
end
|
58
|
+
|
59
|
+
def *(f)
|
60
|
+
lambda {|*xs| self[f[*xs]] }
|
61
|
+
end
|
62
|
+
|
63
|
+
def **(*xs)
|
64
|
+
self[*xs]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def define(sym, pat = nil, &f)
|
69
|
+
FUNCS[sym] ||= Function.new(sym)
|
70
|
+
FUNCS[sym].add(pat, &f)
|
71
|
+
unless self.class.method_defined?(sym)
|
72
|
+
self.class.module_eval("def #{sym}; fun(:#{sym}); end")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def curry(f, *vs)
|
77
|
+
lambda {|v| f[*(vs + [v])] }
|
78
|
+
end
|
79
|
+
|
80
|
+
def fun(sym)
|
81
|
+
FUNCS[sym]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class Proc
|
86
|
+
def *(f)
|
87
|
+
lambda {|*xs| self[f[*xs]] }
|
88
|
+
end
|
89
|
+
|
90
|
+
def **(xs)
|
91
|
+
self[xs]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
require 'cohi/version'
|
data/lib/cohi/prelude.rb
ADDED
@@ -0,0 +1,1011 @@
|
|
1
|
+
require 'rational'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
include Cohi
|
5
|
+
|
6
|
+
# The Meaning of Mark
|
7
|
+
# F ... Finished
|
8
|
+
# N ... Not finished
|
9
|
+
# I ... Impossible
|
10
|
+
|
11
|
+
# data Bool = False | True
|
12
|
+
# ###F### (&&) :: Bool -> Bool -> Bool
|
13
|
+
define(:and_) do |a, b|
|
14
|
+
a and b
|
15
|
+
end
|
16
|
+
|
17
|
+
# ###F### (||) :: Bool -> Bool -> Bool
|
18
|
+
define(:or_) do |a, b|
|
19
|
+
a or b
|
20
|
+
end
|
21
|
+
|
22
|
+
# ###F### not :: Bool -> Bool
|
23
|
+
define(:not_) do |x|
|
24
|
+
not x
|
25
|
+
end
|
26
|
+
|
27
|
+
# ###F### otherwise :: Bool
|
28
|
+
define(:otherwise) do
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
# data Maybe a = Nothing | Just a
|
33
|
+
# ###I### maybe :: b -> (a -> b) -> Maybe a -> b
|
34
|
+
|
35
|
+
# data Either a b = Left a | Right b
|
36
|
+
# ###I### either :: (a -> c) -> (b -> c) -> Either a b -> c
|
37
|
+
|
38
|
+
# data Ordering = LT | EQ | GT
|
39
|
+
# data Char
|
40
|
+
# type String = [Char]
|
41
|
+
|
42
|
+
# ###F### fst :: (a, b) -> a
|
43
|
+
define(:fst) do |x|
|
44
|
+
x.first
|
45
|
+
end
|
46
|
+
|
47
|
+
# ###F### snd :: (a, b) -> b
|
48
|
+
define(:snd) do |x|
|
49
|
+
x.last
|
50
|
+
end
|
51
|
+
|
52
|
+
# ###F### curry :: ((a, b) -> c) -> a -> b -> c
|
53
|
+
define(:curry_) do |f, x, y|
|
54
|
+
f[[x, y]]
|
55
|
+
end
|
56
|
+
|
57
|
+
# ###F### uncurry :: (a -> b -> c) -> (a, b) -> c
|
58
|
+
define(:uncurry) do |f, x|
|
59
|
+
f[fst[x], snd[x]]
|
60
|
+
end
|
61
|
+
|
62
|
+
# class Eq a where
|
63
|
+
# ###F### (==) :: a -> a -> Bool
|
64
|
+
define(:eq) do |a, b|
|
65
|
+
a == b
|
66
|
+
end
|
67
|
+
|
68
|
+
# ###F### (/=) :: a -> a -> Bool
|
69
|
+
define(:ne) do |a, b|
|
70
|
+
not_[eq[a, b]]
|
71
|
+
end
|
72
|
+
|
73
|
+
# class Eq a => Ord a where
|
74
|
+
# ###N### compare :: a -> a -> Ordering
|
75
|
+
# ###F### (<) :: a -> a -> Bool
|
76
|
+
define(:lt) do |a, b|
|
77
|
+
a < b
|
78
|
+
end
|
79
|
+
|
80
|
+
# ###F### (<=) :: a -> a -> Bool
|
81
|
+
define(:le) do |a, b|
|
82
|
+
a <= b
|
83
|
+
end
|
84
|
+
|
85
|
+
# ###F### (>) :: a -> a -> Bool
|
86
|
+
define(:gt) do |a, b|
|
87
|
+
a > b
|
88
|
+
end
|
89
|
+
|
90
|
+
# ###F### (>=) :: a -> a -> Bool
|
91
|
+
define(:ge) do |a, b|
|
92
|
+
a >= b
|
93
|
+
end
|
94
|
+
|
95
|
+
# ###F### max :: a -> a -> a
|
96
|
+
define(:max) do |a, b|
|
97
|
+
gt[a, b] ? a : b
|
98
|
+
end
|
99
|
+
|
100
|
+
# ###F### min :: a -> a -> a
|
101
|
+
define(:min) do |a, b|
|
102
|
+
lt[a, b] ? a : b
|
103
|
+
end
|
104
|
+
|
105
|
+
# class Enum a where
|
106
|
+
# ###F### succ :: a -> a
|
107
|
+
define(:succ) do |x|
|
108
|
+
x.succ
|
109
|
+
end
|
110
|
+
|
111
|
+
# ###I### pred :: a -> a
|
112
|
+
# not exist suitable method in ruby...
|
113
|
+
|
114
|
+
# ###I### toEnum :: Int -> a
|
115
|
+
# not exist suitable method in ruby...
|
116
|
+
|
117
|
+
# ###F### fromEnum :: a -> Int
|
118
|
+
define(:from_enum) do |x|
|
119
|
+
x.to_i
|
120
|
+
end
|
121
|
+
|
122
|
+
# ###F### enumFrom :: a -> [a]
|
123
|
+
define(:from_enum) do |x|
|
124
|
+
x.to_i
|
125
|
+
end
|
126
|
+
|
127
|
+
# ###I### enumFromThen :: a -> a -> [a]
|
128
|
+
# ###I### enumFromTo :: a -> a -> [a]
|
129
|
+
# ###I### enumFromThenTo :: a -> a -> a -> [a]
|
130
|
+
# not exist suitable method in ruby...
|
131
|
+
|
132
|
+
# class Bounded a where
|
133
|
+
# ###I### minBound :: a
|
134
|
+
# ###I### maxBound :: a
|
135
|
+
# not exist suitable method in ruby...
|
136
|
+
|
137
|
+
# data Int
|
138
|
+
# data Integer
|
139
|
+
# data Float
|
140
|
+
# data Double
|
141
|
+
# type Rational = Ratio Integer
|
142
|
+
# class (Eq a, Show a) => Num a where
|
143
|
+
# ###F### (+) :: a -> a -> a
|
144
|
+
define(:add) do |a, b|
|
145
|
+
a + b
|
146
|
+
end
|
147
|
+
|
148
|
+
# ###F### (-) :: a -> a -> a
|
149
|
+
define(:minus) do |a, b|
|
150
|
+
a - b
|
151
|
+
end
|
152
|
+
|
153
|
+
# ###F### (*) :: a -> a -> a
|
154
|
+
define(:mult) do |a, b|
|
155
|
+
a * b
|
156
|
+
end
|
157
|
+
|
158
|
+
# ###F### negate :: a -> a
|
159
|
+
define(:negate) do |a|
|
160
|
+
-a
|
161
|
+
end
|
162
|
+
|
163
|
+
# ###F### abs :: a -> a
|
164
|
+
define(:abs) do |a|
|
165
|
+
gt[a, 0] ? a : negate[a]
|
166
|
+
end
|
167
|
+
|
168
|
+
# ###F### signum :: a -> a
|
169
|
+
define(:signum) do |a|
|
170
|
+
if eq[a, 0]
|
171
|
+
0
|
172
|
+
else
|
173
|
+
gt[a, 0] ? 1 : -1
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# ###I### fromInteger :: Integer -> a
|
178
|
+
# not exist suitable method in ruby...
|
179
|
+
|
180
|
+
# class (Num a, Ord a) => Real a where
|
181
|
+
# ###F### toRational :: a -> Rational
|
182
|
+
define(:to_rational) do |x|
|
183
|
+
x.to_r
|
184
|
+
end
|
185
|
+
|
186
|
+
# class (Real a, Enum a) => Integral a where
|
187
|
+
# ###F### quot :: a -> a -> a
|
188
|
+
define(:quot) do |a, b|
|
189
|
+
if ge[a, 0]
|
190
|
+
a / b
|
191
|
+
else
|
192
|
+
negate[a.abs / b]
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# ###F### rem :: a -> a -> a
|
197
|
+
define(:rem) do |a, b|
|
198
|
+
if ge[a, 0]
|
199
|
+
a % b
|
200
|
+
else
|
201
|
+
negate[a.abs % b]
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# ###F### div :: a -> a -> a
|
206
|
+
define(:div) do |a, b|
|
207
|
+
a / b
|
208
|
+
end
|
209
|
+
|
210
|
+
# ###F### mod :: a -> a -> a
|
211
|
+
define(:mod) do |a, b|
|
212
|
+
a % b
|
213
|
+
end
|
214
|
+
|
215
|
+
# ###F### quotRem :: a -> a -> (a, a)
|
216
|
+
define(:quot_rem) do |a, b|
|
217
|
+
[quot[a, b], rem[a, b]]
|
218
|
+
end
|
219
|
+
|
220
|
+
# ###F### divMod :: a -> a -> (a, a)
|
221
|
+
define(:div_mod) do |a, b|
|
222
|
+
a.divmod b
|
223
|
+
end
|
224
|
+
|
225
|
+
# ###F### toInteger :: a -> Integer
|
226
|
+
define(:to_integer) do |x|
|
227
|
+
x.to_i
|
228
|
+
end
|
229
|
+
|
230
|
+
# class Num a => Fractional a where
|
231
|
+
# ###F### (/) :: a -> a -> a
|
232
|
+
define(:divr) do |a, b|
|
233
|
+
a / b # fractional division
|
234
|
+
end
|
235
|
+
|
236
|
+
# ###F### recip :: a -> a
|
237
|
+
define(:recip) do |a|
|
238
|
+
Rational(a.denominator, a.numerator)
|
239
|
+
end
|
240
|
+
|
241
|
+
# fromRational :: Rational -> a
|
242
|
+
# class Fractional a => Floating a where
|
243
|
+
# ###F### pi :: a
|
244
|
+
define(:pi) do
|
245
|
+
Math::PI
|
246
|
+
end
|
247
|
+
|
248
|
+
# ###F### exp :: a -> a
|
249
|
+
define(:exp) do |a|
|
250
|
+
Math::exp(a)
|
251
|
+
end
|
252
|
+
|
253
|
+
# ###F### log :: a -> a
|
254
|
+
define(:log) do |a|
|
255
|
+
Math::log(a)
|
256
|
+
end
|
257
|
+
|
258
|
+
# ###F### sqrt :: a -> a
|
259
|
+
define(:sqrt) do |a|
|
260
|
+
Math::sqrt(a)
|
261
|
+
end
|
262
|
+
|
263
|
+
# ###F### (**) :: a -> a -> a
|
264
|
+
define(:power) do |a, b|
|
265
|
+
a ** b
|
266
|
+
end
|
267
|
+
|
268
|
+
# ###F### logBase :: a -> a -> a
|
269
|
+
define(:log_base) do |a, b|
|
270
|
+
Math::log(b) / Math::log(a)
|
271
|
+
end
|
272
|
+
|
273
|
+
# ###F### sin :: a -> a
|
274
|
+
define(:sin) do |a|
|
275
|
+
Math::sin(a)
|
276
|
+
end
|
277
|
+
|
278
|
+
# ###F### cos :: a -> a
|
279
|
+
define(:cos) do |a|
|
280
|
+
Math::cos(a)
|
281
|
+
end
|
282
|
+
|
283
|
+
# ###F### tan :: a -> a
|
284
|
+
define(:tan) do |a|
|
285
|
+
Math::tan(a)
|
286
|
+
end
|
287
|
+
|
288
|
+
# ###F### asin :: a -> a
|
289
|
+
define(:asin) do |a|
|
290
|
+
Math::asin(a)
|
291
|
+
end
|
292
|
+
|
293
|
+
# ###F### acos :: a -> a
|
294
|
+
define(:acos) do |a|
|
295
|
+
Math::acos(a)
|
296
|
+
end
|
297
|
+
|
298
|
+
# ###F### atan :: a -> a
|
299
|
+
define(:atan) do |a|
|
300
|
+
Math::atan(a)
|
301
|
+
end
|
302
|
+
|
303
|
+
# ###F### sinh :: a -> a
|
304
|
+
define(:sinh) do |a|
|
305
|
+
Math::sinh(a)
|
306
|
+
end
|
307
|
+
|
308
|
+
# ###F### cosh :: a -> a
|
309
|
+
define(:cosh) do |a|
|
310
|
+
Math::cosh(a)
|
311
|
+
end
|
312
|
+
|
313
|
+
# ###F### tanh :: a -> a
|
314
|
+
define(:tanh) do |a|
|
315
|
+
Math::tanh(a)
|
316
|
+
end
|
317
|
+
|
318
|
+
# ###F### asinh :: a -> a
|
319
|
+
define(:asinh) do |a|
|
320
|
+
Math::asinh(a)
|
321
|
+
end
|
322
|
+
|
323
|
+
# ###F### acosh :: a -> a
|
324
|
+
define(:acosh) do |a|
|
325
|
+
Math::acosh(a)
|
326
|
+
end
|
327
|
+
|
328
|
+
# ###F### atanh :: a -> a
|
329
|
+
define(:atanh) do |a|
|
330
|
+
Math::atanh(a)
|
331
|
+
end
|
332
|
+
|
333
|
+
# class (Real a, Fractional a) => RealFrac a where
|
334
|
+
# ###I### properFraction :: Integral b => a -> (b, a)
|
335
|
+
# not exist suitable method in ruby...
|
336
|
+
|
337
|
+
# ###F### truncate :: Integral b => a -> b
|
338
|
+
define(:truncate) do |a|
|
339
|
+
a.truncate
|
340
|
+
end
|
341
|
+
|
342
|
+
# ###N### round :: Integral b => a -> b
|
343
|
+
define(:round) do |a|
|
344
|
+
a.round
|
345
|
+
end
|
346
|
+
|
347
|
+
# ###N### ceiling :: Integral b => a -> b
|
348
|
+
define(:ceil) do |a|
|
349
|
+
a.ceil
|
350
|
+
end
|
351
|
+
|
352
|
+
# ###N### floor :: Integral b => a -> b
|
353
|
+
define(:floor) do |a|
|
354
|
+
a.floor
|
355
|
+
end
|
356
|
+
|
357
|
+
# class (RealFrac a, Floating a) => RealFloat
|
358
|
+
# ###I### floatRadix :: a -> Integer
|
359
|
+
# ###I### floatDigits :: a -> Int
|
360
|
+
# ###I### floatRange :: a -> (Int, Int)
|
361
|
+
# ###I### decodeFloat :: a -> (Integer, Int)
|
362
|
+
# ###I### encodeFloat :: Integer -> Int -> a
|
363
|
+
# ###I### exponent :: a -> Int
|
364
|
+
# ###I### significand :: a -> a
|
365
|
+
# ###I### scaleFloat :: Int -> a -> a
|
366
|
+
# ###I### isNaN :: a -> Bool
|
367
|
+
# ###I### isInfinite :: a -> Bool
|
368
|
+
# ###I### isDenormalized :: a -> Bool
|
369
|
+
# ###I### isNegativeZero :: a -> Bool
|
370
|
+
# ###I### isIEEE :: a -> Bool
|
371
|
+
# not exist suitable method in ruby...
|
372
|
+
|
373
|
+
# ###I### atan2 :: a -> a -> a
|
374
|
+
define(:atan2) do |a, b|
|
375
|
+
Math::atan2(a, b)
|
376
|
+
end
|
377
|
+
|
378
|
+
# ###F### subtract :: Num a => a -> a -> a
|
379
|
+
define(:subtract) do |a, b|
|
380
|
+
a - b
|
381
|
+
end
|
382
|
+
|
383
|
+
# ###F### even :: Integral a => a -> Bool
|
384
|
+
define(:even) do |a|
|
385
|
+
mod[a, 2] == 0
|
386
|
+
end
|
387
|
+
|
388
|
+
# ###F### odd :: Integral a => a -> Bool
|
389
|
+
define(:odd) do |a|
|
390
|
+
mod[a, 2] == 1
|
391
|
+
end
|
392
|
+
|
393
|
+
# ###F### gcd :: Integral a => a -> a -> a
|
394
|
+
define(:gcd, [X, 0]) do |a|
|
395
|
+
a
|
396
|
+
end
|
397
|
+
define(:gcd, [X, X]) do |a, b|
|
398
|
+
gcd[b, mod[a, b]]
|
399
|
+
end
|
400
|
+
|
401
|
+
# ###F### lcm :: Integral a => a -> a -> a
|
402
|
+
define(:lcm) do |a, b|
|
403
|
+
a * b / gcd[a, b]
|
404
|
+
end
|
405
|
+
|
406
|
+
# ###I### (^) :: (Num a, Integral b) => a -> b -> a
|
407
|
+
# ###I### (^^) :: (Fractional a, Integral b) => a -> b -> a
|
408
|
+
# ###I### fromIntegral :: (Integral a, Num b) => a -> b
|
409
|
+
# ###I### realToFrac :: (Real a, Fractional b) => a -> b
|
410
|
+
# not exist suitable method in ruby...
|
411
|
+
|
412
|
+
# class Monad m where
|
413
|
+
# ###I### (>>=) :: forall a b . m a -> (a -> m b) -> m b
|
414
|
+
# ###I### (>>) :: forall a b . m a -> m b -> m b
|
415
|
+
# ###I### return :: a -> m a
|
416
|
+
# not exist suitable method in ruby...
|
417
|
+
|
418
|
+
# fail :: String -> m a
|
419
|
+
define(:fail) do |a|
|
420
|
+
raise("### Exception: user error (#{a})")
|
421
|
+
end
|
422
|
+
|
423
|
+
# class Functor f where
|
424
|
+
# ###I### fmap :: (a -> b) -> f a -> f b
|
425
|
+
# ###I### mapM :: Monad m => (a -> m b) -> [a] -> m [b]
|
426
|
+
# ###I### mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
|
427
|
+
# ###I### sequence :: Monad m => [m a] -> m [a]
|
428
|
+
# ###I### sequence_ :: Monad m => [m a] -> m ()
|
429
|
+
# ###I### (=<<) :: Monad m => (a -> m b) -> m a -> m b
|
430
|
+
# not exist suitable method in ruby...
|
431
|
+
|
432
|
+
# ###F### id :: a -> a
|
433
|
+
define(:id_) do |a|
|
434
|
+
a
|
435
|
+
end
|
436
|
+
|
437
|
+
# ###F### const :: a -> b -> a
|
438
|
+
define(:const) do |a, b|
|
439
|
+
a
|
440
|
+
end
|
441
|
+
|
442
|
+
# ###I### (.) :: (b -> c) -> (a -> b) -> a -> c
|
443
|
+
# not exist suitable method in ruby...
|
444
|
+
|
445
|
+
# ###F### flip :: (a -> b -> c) -> b -> a -> c
|
446
|
+
define(:flip) do |f, b, a|
|
447
|
+
f[a, b]
|
448
|
+
end
|
449
|
+
|
450
|
+
# ###I### ($) :: (a -> b) -> a -> b
|
451
|
+
# not exist suitable method in ruby...
|
452
|
+
|
453
|
+
# ###F### until :: (a -> Bool) -> (a -> a) -> a -> a
|
454
|
+
define(:until_) do |pr, f, x|
|
455
|
+
if pr[x]
|
456
|
+
x
|
457
|
+
else
|
458
|
+
until_[pr, f, f[x]]
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
# ###F### asTypeOf :: a -> a -> a
|
463
|
+
define(:as_type_of) do |a, b|
|
464
|
+
const[a, b]
|
465
|
+
end
|
466
|
+
|
467
|
+
# ###F### error :: String -> a
|
468
|
+
define(:error) do |a|
|
469
|
+
raise("### Exception: #{a}")
|
470
|
+
end
|
471
|
+
|
472
|
+
# ###F### undefined :: a
|
473
|
+
define(:undefined) do |a|
|
474
|
+
error['Prelude.undefined']
|
475
|
+
end
|
476
|
+
|
477
|
+
# ###I### seq :: a -> b -> b
|
478
|
+
# ###I### ($!) :: (a -> b) -> a -> b
|
479
|
+
# not exist suitable method in ruby...
|
480
|
+
|
481
|
+
# ###F### map :: (a -> b) -> [a] -> [b]
|
482
|
+
define(:map, [X, []]) do
|
483
|
+
[]
|
484
|
+
end
|
485
|
+
define(:map, [X, X_XS]) do |f, x, xs|
|
486
|
+
[f[x]] + map[f, xs]
|
487
|
+
end
|
488
|
+
|
489
|
+
# ###F### (++) :: [a] -> [a] -> [a]
|
490
|
+
define(:append) do |a, b|
|
491
|
+
a + b
|
492
|
+
end
|
493
|
+
|
494
|
+
# ###F### filter :: (a -> Bool) -> [a] -> [a]
|
495
|
+
define(:filter, [X, []]) do
|
496
|
+
[]
|
497
|
+
end
|
498
|
+
define(:filter, [X, X_XS]) do |f, x, xs|
|
499
|
+
if f[x]
|
500
|
+
[x] + filter[f, xs]
|
501
|
+
else
|
502
|
+
filter[f, xs]
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
# ###F### head :: [a] -> a
|
507
|
+
define(:head, [[]]) do
|
508
|
+
error['Prelude.head: empty list']
|
509
|
+
end
|
510
|
+
define(:head, [X_XS]) do |x, xs|
|
511
|
+
x
|
512
|
+
end
|
513
|
+
|
514
|
+
# ###F### last :: [a] -> a
|
515
|
+
define(:last, [[]]) do
|
516
|
+
error['Prelude.last: empty list']
|
517
|
+
end
|
518
|
+
define(:last, [[X]]) do |x|
|
519
|
+
x
|
520
|
+
end
|
521
|
+
define(:last, [X_XS]) do |x, xs|
|
522
|
+
last[xs]
|
523
|
+
end
|
524
|
+
|
525
|
+
# ###F### tail :: [a] -> [a]
|
526
|
+
define(:tail, [[]]) do
|
527
|
+
error['Prelude.tail: empty list']
|
528
|
+
end
|
529
|
+
define(:tail, [X_XS]) do |x, xs|
|
530
|
+
xs
|
531
|
+
end
|
532
|
+
|
533
|
+
# ###F### init :: [a] -> [a]
|
534
|
+
define(:init, [[]]) do
|
535
|
+
error['Prelude.init: empty list']
|
536
|
+
end
|
537
|
+
define(:init, [[X]]) do |x|
|
538
|
+
[]
|
539
|
+
end
|
540
|
+
define(:init, [X_XS]) do |x, xs|
|
541
|
+
[x] + init[xs]
|
542
|
+
end
|
543
|
+
|
544
|
+
# ###F### null :: [a] -> Bool
|
545
|
+
define(:null, [[]]) do
|
546
|
+
true
|
547
|
+
end
|
548
|
+
define(:null, [X]) do |x|
|
549
|
+
false
|
550
|
+
end
|
551
|
+
|
552
|
+
# ###F### length :: [a] -> Int
|
553
|
+
define(:length) do |a|
|
554
|
+
length2[a, 0]
|
555
|
+
end
|
556
|
+
|
557
|
+
define(:length2, [[], X]) do |a|
|
558
|
+
a
|
559
|
+
end
|
560
|
+
define(:length2, [X_XS, X]) do |x, xs, a|
|
561
|
+
length2[xs, a + 1]
|
562
|
+
end
|
563
|
+
|
564
|
+
# ###F### (!!) :: [a] -> Int -> a
|
565
|
+
define(:at) do |a, i|
|
566
|
+
a[i]
|
567
|
+
end
|
568
|
+
|
569
|
+
# ###F### reverse :: [a] -> [a]
|
570
|
+
define(:reverse) do |a|
|
571
|
+
reverse2[a, []]
|
572
|
+
end
|
573
|
+
|
574
|
+
define(:reverse2, [[], X]) do |a|
|
575
|
+
a
|
576
|
+
end
|
577
|
+
define(:reverse2, [X_XS, X]) do |x, xs, a|
|
578
|
+
reverse2[xs, [x] + a]
|
579
|
+
end
|
580
|
+
|
581
|
+
# ###F### foldl :: (a -> b -> a) -> a -> [b] -> a
|
582
|
+
define(:foldl, [X, X, []]) do |f, z|
|
583
|
+
z
|
584
|
+
end
|
585
|
+
define(:foldl, [X, X, X_XS]) do |f, z, x, xs|
|
586
|
+
foldl[f, f[z, x], xs]
|
587
|
+
end
|
588
|
+
|
589
|
+
# ###F### foldl1 :: (a -> a -> a) -> [a] -> a
|
590
|
+
define(:foldl1, [X, []]) do |f|
|
591
|
+
error['Prelude.foldl1: empty list']
|
592
|
+
end
|
593
|
+
define(:foldl1, [X, X_XS]) do |f, x, xs|
|
594
|
+
foldl[f, x, xs]
|
595
|
+
end
|
596
|
+
|
597
|
+
# ###F### foldr :: (a -> b -> b) -> b -> [a] -> b
|
598
|
+
define(:foldr, [X, X, []]) do |f, z|
|
599
|
+
z
|
600
|
+
end
|
601
|
+
define(:foldr, [X, X, X_XS]) do |f, z, x, xs|
|
602
|
+
f[x, foldr[f, z, xs]]
|
603
|
+
end
|
604
|
+
|
605
|
+
# ###F### foldr1 :: (a -> a -> a) -> [a] -> a
|
606
|
+
define(:foldr1, [X, []]) do |f|
|
607
|
+
error['Prelude.foldr1: empty list']
|
608
|
+
end
|
609
|
+
define(:foldr1, [X, [X]]) do |f, x|
|
610
|
+
x
|
611
|
+
end
|
612
|
+
define(:foldr1, [X, X_XS]) do |f, x, xs|
|
613
|
+
f[x, foldr1[f, xs]]
|
614
|
+
end
|
615
|
+
|
616
|
+
# ###F### and :: [Bool] -> Bool
|
617
|
+
define(:anda, [[]]) do
|
618
|
+
true
|
619
|
+
end
|
620
|
+
define(:anda, [X_XS]) do |x, xs|
|
621
|
+
and_[x, anda[xs]]
|
622
|
+
end
|
623
|
+
|
624
|
+
# ###F### or :: [Bool] -> Bool
|
625
|
+
define(:ora, [[]]) do
|
626
|
+
false
|
627
|
+
end
|
628
|
+
define(:ora, [X_XS]) do |x, xs|
|
629
|
+
or_[x, ora[xs]]
|
630
|
+
end
|
631
|
+
|
632
|
+
# ###F### any :: (a -> Bool) -> [a] -> Bool
|
633
|
+
define(:any) do |pr, xs|
|
634
|
+
ora[map[pr, xs]]
|
635
|
+
end
|
636
|
+
|
637
|
+
# ###F### all :: (a -> Bool) -> [a] -> Bool
|
638
|
+
define(:all) do |pr, xs|
|
639
|
+
anda[map[pr, xs]]
|
640
|
+
end
|
641
|
+
|
642
|
+
# ###F### sum :: Num a => [a] -> a
|
643
|
+
define(:sum) do |xs|
|
644
|
+
foldl[add, 0, xs]
|
645
|
+
end
|
646
|
+
|
647
|
+
# ###F### product :: Num a => [a] -> a
|
648
|
+
define(:product) do |xs|
|
649
|
+
foldl[mult, 1, xs]
|
650
|
+
end
|
651
|
+
|
652
|
+
# ###F### concat :: [[a]] -> [a]
|
653
|
+
define(:concat) do |xs|
|
654
|
+
foldr[append, [], xs]
|
655
|
+
end
|
656
|
+
|
657
|
+
# ###F### concatMap :: (a -> [b]) -> [a] -> [b]
|
658
|
+
define(:concat_map) do |f, xs|
|
659
|
+
concat[map[f, xs]]
|
660
|
+
end
|
661
|
+
|
662
|
+
# ###F### maximum :: Ord a => [a] -> a
|
663
|
+
define(:maximum, [[]]) do
|
664
|
+
error['Prelude.maximum: empty list']
|
665
|
+
end
|
666
|
+
define(:maximum) do |xs|
|
667
|
+
foldl1[max, xs]
|
668
|
+
end
|
669
|
+
|
670
|
+
# ###F### minimum :: Ord a => [a] -> a
|
671
|
+
define(:minimum, [[]]) do
|
672
|
+
error['Prelude.maximum: empty list']
|
673
|
+
end
|
674
|
+
define(:minimum) do |xs|
|
675
|
+
foldl1[min, xs]
|
676
|
+
end
|
677
|
+
|
678
|
+
# ###F### scanl :: (a -> b -> a) -> a -> [b] -> [a]
|
679
|
+
define(:scanl) do |f, z, xs|
|
680
|
+
[z] + scanl2[f, z, xs]
|
681
|
+
end
|
682
|
+
|
683
|
+
define(:scanl2, [X, X, []]) do |f, z|
|
684
|
+
[]
|
685
|
+
end
|
686
|
+
define(:scanl2, [X, X, X_XS]) do |f, z, x, xs|
|
687
|
+
scanl[f, f[z, x], xs]
|
688
|
+
end
|
689
|
+
|
690
|
+
# ###F### scanl1 :: (a -> a -> a) -> [a] -> [a]
|
691
|
+
define(:scanl1, [X, []]) do |f|
|
692
|
+
[]
|
693
|
+
end
|
694
|
+
define(:scanl1, [X, X_XS]) do |f, x, xs|
|
695
|
+
scanl[f, x, xs]
|
696
|
+
end
|
697
|
+
|
698
|
+
# ###F### scanr :: (a -> b -> b) -> b -> [a] -> [b]
|
699
|
+
define(:scanr, [X, X, []]) do |f, z|
|
700
|
+
[z]
|
701
|
+
end
|
702
|
+
define(:scanr, [X, X, X_XS]) do |f, z, x, xs|
|
703
|
+
qs = scanr[f, z, xs]
|
704
|
+
q, *qs2 = *qs
|
705
|
+
[f[x, q]] + qs
|
706
|
+
end
|
707
|
+
|
708
|
+
# ###F### scanr1 :: (a -> a -> a) -> [a] -> [a]
|
709
|
+
define(:scanr1, [X, []]) do |f|
|
710
|
+
[]
|
711
|
+
end
|
712
|
+
define(:scanr1, [X, [X]]) do |f, x|
|
713
|
+
[x]
|
714
|
+
end
|
715
|
+
define(:scanr1, [X, X_XS]) do |f, x, xs|
|
716
|
+
qs = scanr1[f, xs]
|
717
|
+
q, *qs2 = *qs
|
718
|
+
[f[x, q]] + qs
|
719
|
+
end
|
720
|
+
|
721
|
+
# ###I### iterate :: (a -> a) -> a -> [a]
|
722
|
+
# ###I### repeat :: a -> [a]
|
723
|
+
# not exist suitable method in ruby...
|
724
|
+
|
725
|
+
# ###N### replicate :: Int -> a -> [a]
|
726
|
+
define(:replicate) do |n, x|
|
727
|
+
Array.new(n, x)
|
728
|
+
end
|
729
|
+
|
730
|
+
# ###I### cycle :: [a] -> [a]
|
731
|
+
# not exist suitable method in ruby...
|
732
|
+
|
733
|
+
# ###F### take :: Int -> [a] -> [a]
|
734
|
+
define(:take, [X, []]) do |n|
|
735
|
+
[]
|
736
|
+
end
|
737
|
+
define(:take, [X, X_XS]) do |n, x, xs|
|
738
|
+
if le[n, 0]
|
739
|
+
[]
|
740
|
+
else
|
741
|
+
[x] + take[n - 1, xs]
|
742
|
+
end
|
743
|
+
end
|
744
|
+
|
745
|
+
# ###F### drop :: Int -> [a] -> [a]
|
746
|
+
define(:drop, [X, []]) do |n|
|
747
|
+
[]
|
748
|
+
end
|
749
|
+
define(:drop, [X, X_XS]) do |n, x, xs|
|
750
|
+
if le[n, 0]
|
751
|
+
[x] + xs
|
752
|
+
else
|
753
|
+
drop[n - 1, xs]
|
754
|
+
end
|
755
|
+
end
|
756
|
+
|
757
|
+
# ###F### splitAt :: Int -> [a] -> ([a], [a])
|
758
|
+
define(:split_at) do |n, xs|
|
759
|
+
[take[n, xs], drop[n, xs]]
|
760
|
+
end
|
761
|
+
|
762
|
+
# ###F### takeWhile :: (a -> Bool) -> [a] -> [a]
|
763
|
+
define(:take_while, [X, []]) do |pr|
|
764
|
+
[]
|
765
|
+
end
|
766
|
+
define(:take_while, [X, X_XS]) do |pr, x, xs|
|
767
|
+
if pr[x]
|
768
|
+
[x] + take_while[pr, xs]
|
769
|
+
else
|
770
|
+
[]
|
771
|
+
end
|
772
|
+
end
|
773
|
+
|
774
|
+
# ###F### dropWhile :: (a -> Bool) -> [a] -> [a]
|
775
|
+
define(:drop_while, [X, []]) do |pr|
|
776
|
+
[]
|
777
|
+
end
|
778
|
+
define(:drop_while, [X, X_XS]) do |pr, x, xs|
|
779
|
+
if pr[x]
|
780
|
+
drop_while[pr, xs]
|
781
|
+
else
|
782
|
+
[x] + xs
|
783
|
+
end
|
784
|
+
end
|
785
|
+
|
786
|
+
# ###F### span :: (a -> Bool) -> [a] -> ([a], [a])
|
787
|
+
define(:span, [X, []]) do |pr|
|
788
|
+
[[], []]
|
789
|
+
end
|
790
|
+
define(:span, [X, X_XS]) do |pr, x, xs|
|
791
|
+
if pr[x]
|
792
|
+
ys, zs = span[pr, xs]
|
793
|
+
[[x] + ys, zs]
|
794
|
+
else
|
795
|
+
[[], [x] + xs]
|
796
|
+
end
|
797
|
+
end
|
798
|
+
|
799
|
+
# ###F### break :: (a -> Bool) -> [a] -> ([a], [a])
|
800
|
+
define(:break_, [X, []]) do |pr|
|
801
|
+
[[], []]
|
802
|
+
end
|
803
|
+
define(:break_, [X, X_XS]) do |pr, x, xs|
|
804
|
+
if pr[x]
|
805
|
+
[[], [x] + xs]
|
806
|
+
else
|
807
|
+
ys, zs = break_[pr, xs]
|
808
|
+
[[x] + ys, zs]
|
809
|
+
end
|
810
|
+
end
|
811
|
+
|
812
|
+
# ###F### elem :: Eq a => a -> [a] -> Bool
|
813
|
+
define(:elem) do |x, xs|
|
814
|
+
any[curry(eq, x), xs]
|
815
|
+
end
|
816
|
+
|
817
|
+
# ###F### notElem :: Eq a => a -> [a] -> Bool
|
818
|
+
define(:not_elem) do |x, xs|
|
819
|
+
all[curry(ne, x), xs]
|
820
|
+
end
|
821
|
+
|
822
|
+
# ###I### lookup :: Eq a => a -> [(a, b)] -> Maybe b
|
823
|
+
# not exist suitable method in ruby...
|
824
|
+
|
825
|
+
# ###F### zip :: [a] -> [b] -> [(a, b)]
|
826
|
+
define(:zip, [[], X]) do |ys|
|
827
|
+
[]
|
828
|
+
end
|
829
|
+
define(:zip, [X, []]) do |xs|
|
830
|
+
[]
|
831
|
+
end
|
832
|
+
define(:zip, [X_XS, X_XS]) do |x, xs, y, ys|
|
833
|
+
[[x, y]] + zip[xs, ys]
|
834
|
+
end
|
835
|
+
|
836
|
+
# ###F### zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
|
837
|
+
define(:zip3, [[], X, X]) do |ys, zs|
|
838
|
+
[]
|
839
|
+
end
|
840
|
+
define(:zip3, [X, [], X]) do |xs, zs|
|
841
|
+
[]
|
842
|
+
end
|
843
|
+
define(:zip3, [X, X, []]) do |xs, ys|
|
844
|
+
[]
|
845
|
+
end
|
846
|
+
define(:zip3, [X_XS, X_XS, X_XS]) do |x, xs, y, ys, z, zs|
|
847
|
+
[[x, y, z]] + zip3[xs, ys, zs]
|
848
|
+
end
|
849
|
+
|
850
|
+
# ###F### zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
851
|
+
define(:zip_with, [X, [], X]) do |f, ys|
|
852
|
+
[]
|
853
|
+
end
|
854
|
+
define(:zip_with, [X, X, []]) do |f, xs|
|
855
|
+
[]
|
856
|
+
end
|
857
|
+
define(:zip_with, [X, X_XS, X_XS]) do |f, x, xs, y, ys|
|
858
|
+
[f[x, y]] + zip_with[f, xs, ys]
|
859
|
+
end
|
860
|
+
|
861
|
+
# ###F### zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
|
862
|
+
define(:zip_with3, [X, [], X, X]) do |f, ys, zs|
|
863
|
+
[]
|
864
|
+
end
|
865
|
+
define(:zip_with3, [X, X, [], X]) do |f, xs, zs|
|
866
|
+
[]
|
867
|
+
end
|
868
|
+
define(:zip_with3, [X, X, X, []]) do |f, xs, ys|
|
869
|
+
[]
|
870
|
+
end
|
871
|
+
define(:zip_with3, [X, X_XS, X_XS, X_XS]) do |f, x, xs, y, ys, z, zs|
|
872
|
+
[f[x, y, z]] + zip_with3[f, xs, ys, zs]
|
873
|
+
end
|
874
|
+
|
875
|
+
# ###F### unzip :: [(a, b)] -> ([a], [b])
|
876
|
+
define(:unzip) do |xs|
|
877
|
+
foldr[lambda {|a, b| [[fst[a]] + fst[b], [snd[a]] + snd[b]]}, [[], []], xs]
|
878
|
+
end
|
879
|
+
|
880
|
+
# ###F### unzip3 :: [(a, b, c)] -> ([a], [b], [c])
|
881
|
+
define(:unzip3) do |xs|
|
882
|
+
foldr[unzip32, [[], [], []], xs]
|
883
|
+
end
|
884
|
+
|
885
|
+
define(:unzip32, [[X, X, X], [X, X, X]]) do |x, y, z, xs, ys, zs|
|
886
|
+
[[x] + xs, [y] + ys, [z] + zs]
|
887
|
+
end
|
888
|
+
|
889
|
+
# ###F### lines :: String -> [String]
|
890
|
+
define(:lines) do |s|
|
891
|
+
s.split(/\n/)
|
892
|
+
end
|
893
|
+
|
894
|
+
# ###F### words :: String -> [String]
|
895
|
+
define(:words) do |s|
|
896
|
+
s.split(/\s+/)
|
897
|
+
end
|
898
|
+
|
899
|
+
# ###F### unlines :: [String] -> String
|
900
|
+
define(:unlines) do |xs|
|
901
|
+
map[lambda {|s| s + "\n"}, xs].join
|
902
|
+
end
|
903
|
+
|
904
|
+
# ###F### unwords :: [String] -> String
|
905
|
+
define(:unwords) do |xs|
|
906
|
+
xs.join(' ')
|
907
|
+
end
|
908
|
+
|
909
|
+
# type ShowS = String -
|
910
|
+
# class Show a where
|
911
|
+
# ###I### showsPrec :: Int -> a -> ShowS
|
912
|
+
# not exist suitable method in ruby...
|
913
|
+
|
914
|
+
# ###F### show :: a -> String
|
915
|
+
define(:show) do |x|
|
916
|
+
x.inspect
|
917
|
+
end
|
918
|
+
|
919
|
+
# ###I### showList :: [a] -> ShowS
|
920
|
+
# ###I### shows :: Show a => a -> ShowS
|
921
|
+
# ###I### showChar :: Char -> ShowS
|
922
|
+
# ###I### showString :: String -> ShowS
|
923
|
+
# ###I### showParen :: Bool -> ShowS -> ShowS
|
924
|
+
# not exist suitable method in ruby...
|
925
|
+
|
926
|
+
# type ReadS a = String -> [(a, String)]
|
927
|
+
# class Read a where
|
928
|
+
# ###I### readsPrec :: Int -> ReadS a
|
929
|
+
# ###I### readList :: ReadS [a]
|
930
|
+
# ###I### reads :: Read a => ReadS a
|
931
|
+
# ###I### readParen :: Bool -> ReadS a -> ReadS a
|
932
|
+
# ###I### read :: Read a => String -> a
|
933
|
+
# ###I### lex :: ReadS String
|
934
|
+
# not exist suitable method in ruby...
|
935
|
+
|
936
|
+
# data IO a
|
937
|
+
# ###F### putChar :: Char -> IO ()
|
938
|
+
define(:put_char) do |x|
|
939
|
+
putc x
|
940
|
+
end
|
941
|
+
|
942
|
+
# ###F### putStr :: String -> IO ()
|
943
|
+
define(:put_str) do |x|
|
944
|
+
print x
|
945
|
+
end
|
946
|
+
|
947
|
+
# ###F### putStrLn :: String -> IO ()
|
948
|
+
define(:put_str_ln) do |x|
|
949
|
+
puts x
|
950
|
+
end
|
951
|
+
|
952
|
+
# ###F### print :: Show a => a -> IO ()
|
953
|
+
define(:print_) do |x|
|
954
|
+
p x
|
955
|
+
end
|
956
|
+
|
957
|
+
# ###F### getChar :: IO Char
|
958
|
+
define(:get_char) do
|
959
|
+
$stdin.getc
|
960
|
+
end
|
961
|
+
|
962
|
+
# ###F### getLine :: IO String
|
963
|
+
define(:get_line) do
|
964
|
+
$stdin.gets.chomp
|
965
|
+
end
|
966
|
+
|
967
|
+
# ###F### getContents :: IO String
|
968
|
+
define(:get_contents) do
|
969
|
+
$stdin.read
|
970
|
+
end
|
971
|
+
|
972
|
+
# ###F### interact :: (String -> String) -> IO ()
|
973
|
+
define(:interact) do |f|
|
974
|
+
put_str ** f ** get_contents[]
|
975
|
+
end
|
976
|
+
|
977
|
+
# type FilePath = String
|
978
|
+
# ###F### readFile :: FilePath -> IO String
|
979
|
+
define(:read_file) do |file|
|
980
|
+
Pathname(file).read
|
981
|
+
end
|
982
|
+
|
983
|
+
# ###F### writeFile :: FilePath -> String -> IO ()
|
984
|
+
define(:write_file) do |file, s|
|
985
|
+
Pathname(file).open('w') do |out|
|
986
|
+
out.print s
|
987
|
+
end
|
988
|
+
end
|
989
|
+
|
990
|
+
# ###F### appendFile :: FilePath -> String -> IO ()
|
991
|
+
define(:append_file) do |file, s|
|
992
|
+
Pathname(file).open('a') do |out|
|
993
|
+
out.print s
|
994
|
+
end
|
995
|
+
end
|
996
|
+
|
997
|
+
# ###I### readIO :: Read a => String -> IO a
|
998
|
+
# ###I### readLn :: Read a => IO a
|
999
|
+
# not exist suitable method in ruby...
|
1000
|
+
|
1001
|
+
# type IOError = IOException
|
1002
|
+
# ###I### ioError :: IOError -> IO a
|
1003
|
+
# not exist suitable method in ruby...
|
1004
|
+
|
1005
|
+
# ###F### userError :: String -> IOError
|
1006
|
+
define(:user_error) do |a|
|
1007
|
+
raise("### Exception: user error (#{a})")
|
1008
|
+
end
|
1009
|
+
|
1010
|
+
# ###I### catch :: IO a -> (IOError -> IO a) -> IO a
|
1011
|
+
# not exist suitable method in ruby...
|