Module: SchemeLists

Includes:
SchemeListsHelper
Included in:
Tokenizer
Defined in:
lib/lisp/interpreter/list.rb

Overview

Scheme lists module

Instance Method Summary collapse

Methods included from SchemeListsHelper

#build_cons_from_list, #build_list, #car_cdr_values, #cons_helper, #evaluate_list, #find_all_values_list_evaluate, #find_idx_for_list, #find_list_function_value, #find_to_evaluate_or_not, #get_cons_values, #map_helper, #no_eval_list, #split_list_as_string, #split_list_string

Instance Method Details

#car(other) ⇒ Object



112
113
114
115
116
# File 'lib/lisp/interpreter/list.rb', line 112

def car(other)
  value = car_cdr_values other
  raise 'Cannot apply car on ' + other[0].to_s if value.nil? || value.empty?
  value.shift
end

#cdr(other) ⇒ Object



118
119
120
121
122
123
# File 'lib/lisp/interpreter/list.rb', line 118

def cdr(other)
  value = car_cdr_values other
  raise 'Cannot apply cdr on ' + other[0].to_s if value.nil? || value.empty?
  idx = value[1] == '.' ? 2 : 1
  build_list value[idx..-1]
end

#cons(other) ⇒ Object



103
104
105
106
# File 'lib/lisp/interpreter/list.rb', line 103

def cons(other)
  raise 'Incorrect number of arguments' if other.size != 2
  cons_helper other
end

#length(other) ⇒ Object



141
142
143
# File 'lib/lisp/interpreter/list.rb', line 141

def length(other)
  (find_list_function_value other).size
end

#list(other) ⇒ Object



108
109
110
# File 'lib/lisp/interpreter/list.rb', line 108

def list(other)
  build_list other
end

#list?(other) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
# File 'lib/lisp/interpreter/list.rb', line 125

def list?(other)
  raise 'Incorrect number of arguments' if other.size != 1
  other[0].to_s.list? ? '#t' : '#f'
end

#map(other) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/lisp/interpreter/list.rb', line 150

def map(other)
  raise 'Incorrect number of arguments' if other.empty?
  func, other = valid_function other
  raise 'Incorrect number of arguments' if other.empty?
  lst = find_all_values other
  lst = lst.map { |t| find_list_function_value [t] }
  lst = (equalize_lists lst).transpose
  build_list map_helper lst, func
end

#null?(other) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
# File 'lib/lisp/interpreter/list.rb', line 135

def null?(other)
  raise 'Incorrect number of arguments' if other.size != 1
  return '#f' unless other[0].to_s.list?
  other[0].to_s.size == 3 ? '#t' : '#f'
end

#pair?(other) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/lisp/interpreter/list.rb', line 130

def pair?(other)
  raise 'Incorrect number of arguments' if other.size != 1
  other[0].to_s.pair? ? '#t' : '#f'
end

#reverse(other) ⇒ Object



145
146
147
148
# File 'lib/lisp/interpreter/list.rb', line 145

def reverse(other)
  value = find_list_function_value other
  build_list value.reverse
end

#shuffle(other) ⇒ Object



160
161
162
163
# File 'lib/lisp/interpreter/list.rb', line 160

def shuffle(other)
  values = find_list_function_value other
  build_list values.shuffle
end