2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1 +1,3 @@
;Task:
Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.
<br><br>

View file

@ -0,0 +1,62 @@
-- isNumString :: String -> Bool
on isNumString(s)
try
if class of s is string then
set c to class of (s as number)
c is real or c is integer
else
false
end if
on error
false
end try
end isNumString
-- TEST
on run
map(isNumString, {3, 3.0, 3.5, "3.5", "3E8", "-3.5", "30", "three", three, four})
--> {false, false, false, true, true, true, true, false, false, false}
end run
-- three :: () -> Int
script three
3
end script
-- four :: () -> Int
on four()
4
end four
-- GENERIC FUNCTIONS FOR TEST
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn

View file

@ -0,0 +1 @@
{false, false, false, true, true, true, true, false, false, false}

View file

@ -1,15 +1,16 @@
% Is string numeric?
function out = is_str_numeric(s)
out = ~isempty(parse_float(s));
end
% copy from Octave version on this page
function r = isnum(a)
if ( isnumeric(a) )
r = 1;
else
o = str2num(a);
r = !isempty(o);
endif
end
% Returns the float (double) if true, empty array otherwise.
function f = parse_float(s)
[f_in_cell, pos] = textscan(s, '%f');
% Make sure there are no trailing chars. textscan(..) is greedy.
if pos == length(s)
f = f_in_cell{:};
else
f = [];
end
end
% tests
disp(isnum(123)) % 1
disp(isnum("123")) % 1
disp(isnum("foo123")) % 0
disp(isnum("123bar")) % 0
disp(isnum("3.1415")) % 1

View file

@ -0,0 +1,51 @@
# $a0 char val
# $a1 address pointer
# $a2 PERIOD_HIT_FLAG
# $a3 HAS_DIGITS_FLAG
.data
### CHANGE THIS STRING TO TEST DIFFERENT ONES... ###
string: .asciiz "-.1236"
s_false: .asciiz "False"
s_true: .asciiz "True"
.text
main:
set_up: #test for 0th char == 45 or 46 or 48...57
la $a1,string
lb $a0,($a1)
beq $a0,45,loop # == '-'
beq $a0,46,loop # == '.'
blt $a0,48,exit_false # isn't below the ascii range for chars '0'...'9'
bgt $a0,57,exit_false # isn't above the ascii range for chars '0'...'9'
loop:
addi $a1,$a1,1
lb $a0,($a1)
beqz $a0,exit_true # test for \0 null char
beq $a0,46,period_test #test for a duplicate period
blt $a0,48,exit_false #test for
bgt $a0,57,exit_false
la $a3,1 #set the HAS_DIGITS flag. This line is only reached because the
# tests for period and - both jump back to start.
j loop
exit_true:
beqz $a3,exit_false
la $a0,s_true
la $v0,4
syscall
li $v0,10
syscall
exit_false:
la $a0,s_false
la $v0,4
syscall
li $v0,10
syscall
period_test:
beq $a2,1,exit_false
li $a2,1
j loop

View file

@ -0,0 +1,11 @@
isNumeric := proc(s)
try
if type(parse(s), numeric) then
printf("The string is numeric."):
else
printf("The string is not numeric."):
end if:
catch:
printf("The string is not numeric."):
end try:
end proc:

View file

@ -2,6 +2,6 @@ sub is-number( $term --> Bool ) {
?($term ~~ /\d/) and +$term ~~ Numeric;
}
printf "%10s %s\n", "<$_>", is-number( $_ ) for
printf "%10s %s\n", "<$_>", is-number( $_ ) for flat
<1 1.2 1.2.3 -6 1/2 12e B17 1.3e+12 1.3e12 -2.6e-3 zero
0x 0xA10 0b1001 0o16 0o18 2+5i>, '1 1 1', '', ' ';