2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,11 +1,6 @@
|
|||
Generate an array, list, lazy sequence, or even an indexable string
|
||||
of all the lower case ASCII characters, from 'a' to 'z'.
|
||||
|
||||
If the standard library contains such sequence, show how to access it,
|
||||
but don't fail to show how to generate a similar sequence.
|
||||
For this basic task use a reliable style of coding, a style fit for a very large program, and use strong typing if available.
|
||||
|
||||
It's bug prone to enumerate all the lowercase chars manually in the code.
|
||||
During code review it's not immediate to spot the bug in a Tcl line like this contained in a page of code:
|
||||
;Task:
|
||||
Generate an array, list, lazy sequence, or even an indexable string of all the lower case ASCII characters, from <big> a </big> to <big> z.</big> If the standard library contains such a sequence, show how to access it, but don't fail to show how to generate a similar sequence.
|
||||
|
||||
For this basic task use a reliable style of coding, a style fit for a very large program, and use strong typing if available. It's bug prone to enumerate all the lowercase characters manually in the code. During code review it's not immediate obvious to spot the bug in a Tcl line like this contained in a page of code:
|
||||
<lang tcl>set alpha {a b c d e f g h i j k m n o p q r s t u v w x y z}</lang>
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
ASCLOW: PHA ; push contents of registers that we
|
||||
TXA ; shall be using onto the stack
|
||||
PHA
|
||||
LDA #$61 ; ASCII "a"
|
||||
LDX #$00
|
||||
ALLOOP: STA $2000,X
|
||||
INX
|
||||
CLC
|
||||
ADC #$01
|
||||
CMP #$7B ; have we got beyond ASCII "z"?
|
||||
BNE ALLOOP
|
||||
LDA #$00 ; terminate the string with ASCII NUL
|
||||
STA $2000,X
|
||||
PLA ; retrieve register contents from
|
||||
TAX ; the stack
|
||||
PLA
|
||||
RTS ; return
|
||||
|
|
@ -0,0 +1 @@
|
|||
⎕UCS 96+⍳26
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
-- showCharRange :: String -> String -> [String]
|
||||
on showCharRange(strFrom, strTo)
|
||||
-- showChar :: Int -> String
|
||||
script showChar
|
||||
on lambda(intID)
|
||||
character id intID
|
||||
end lambda
|
||||
end script
|
||||
|
||||
map(showChar, range(id of strFrom, id of strTo))
|
||||
end showCharRange
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
{showCharRange("a", "z"), ¬
|
||||
showCharRange("🐐", "🐟")}
|
||||
|
||||
end run
|
||||
|
||||
|
||||
---------------------------------------------------------------------------
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- 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
|
||||
|
||||
-- range :: Int -> Int -> [Int]
|
||||
on range(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end range
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
{{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
||||
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"},
|
||||
{"🐐", "🐑", "🐒", "🐓", "🐔", "🐕", "🐖", "🐗",
|
||||
"🐘", "🐙", "🐚", "🐛", "🐜", "🐝", "🐞", "🐟"}}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
identification division.
|
||||
program-id. lower-case-alphabet-program.
|
||||
data division.
|
||||
working-storage section.
|
||||
01 ascii-lower-case.
|
||||
05 lower-case-alphabet pic a(26).
|
||||
05 character-code pic 999.
|
||||
05 loop-counter pic 99.
|
||||
procedure division.
|
||||
control-paragraph.
|
||||
perform add-next-letter-paragraph varying loop-counter from 1 by 1
|
||||
until loop-counter is greater than 26.
|
||||
display lower-case-alphabet upon console.
|
||||
stop run.
|
||||
add-next-letter-paragraph.
|
||||
add 97 to loop-counter giving character-code.
|
||||
move function char(character-code) to lower-case-alphabet(loop-counter:1).
|
||||
|
|
@ -1,24 +1 @@
|
|||
\ generate a string filled with the lowercase ASCII alphabet
|
||||
\ RAW Forth is quite low level. Strings are simply named memory spaces in Forth.
|
||||
\ Typically they return an address on the Forth Stack (a pointer) with a count value in CHARs
|
||||
\ These examples use a string with the first byte containing the length of the string
|
||||
|
||||
\ We show 2 ways to load the ASCII values
|
||||
|
||||
create lalpha 27 chars allot \ create a string for 26 letters and count byte
|
||||
|
||||
: ]lalpha ( index -- addr ) \ word to index the string like an array
|
||||
lalpha char+ + ;
|
||||
|
||||
\ method 1: use a loop
|
||||
: fillit ( -- )
|
||||
26 0
|
||||
do
|
||||
[char] a I + \ calc. the ASCII value
|
||||
I ]lalpha c! \ store the char (c!) in the string at I
|
||||
loop
|
||||
26 lalpha c! ; \ store the count byte at the head of the string
|
||||
|
||||
|
||||
\ method 2: load with a string literal
|
||||
: Loadit s" abcdefghijklmnopqrstuvwxyz" lalpha PLACE ;
|
||||
: printit 26 0 do [char] a I + emit loop ;
|
||||
|
|
|
|||
|
|
@ -1,9 +1 @@
|
|||
fillit ok
|
||||
|
||||
lalpha count type abcdefghijklmnopqrstuvwxyz ok
|
||||
|
||||
loadit ok
|
||||
|
||||
lalpha count type abcdefghijklmnopqrstuvwxyz ok
|
||||
|
||||
ok
|
||||
: printit2 [char] z 1+ [char] a do I emit loop ;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
create lalpha 27 chars allot \ create a string in memory for 26 letters and count byte
|
||||
|
||||
: ]lalpha ( index -- addr ) \ index the string like an array (return an address)
|
||||
lalpha char+ + ;
|
||||
|
||||
\ method 1: fill memory with ascii values using a loop
|
||||
: fillit ( -- )
|
||||
26 0
|
||||
do
|
||||
[char] a I + \ calc. the ASCII value, leave on the stack
|
||||
I ]lalpha c! \ store the value on stack in the string at index I
|
||||
loop
|
||||
26 lalpha c! ; \ store the count byte at the head of the string
|
||||
|
||||
|
||||
\ method 2: load with a string literal
|
||||
: Loadit s" abcdefghijklmnopqrstuvwxyz" lalpha PLACE ;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
printit abcdefghijklmnopqrstuvwxyz ok
|
||||
|
||||
fillit ok
|
||||
lalpha count type abcdefghijklmnopqrstuvwxyz ok
|
||||
lalpha count erase ok
|
||||
lalpha count type ok
|
||||
loadit ok
|
||||
lalpha count type abcdefghijklmnopqrstuvwxyz ok
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
procedure lower_case_letters() # entry point for function lower_case_letters
|
||||
return &lcase # returning lower caser letters represented by the set &lcase
|
||||
end
|
||||
|
||||
procedure main(param) # main procedure as entry point
|
||||
write(lower_case_letters()) # output of result of function lower_case_letters()
|
||||
end
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(lstRanges => {
|
||||
|
||||
// charRange :: Char -> Char -> [Char]
|
||||
function charRange(cFrom, cTo) {
|
||||
let [m, n] = [cFrom, cTo]
|
||||
.map(s => s.codePointAt(0));
|
||||
|
||||
return Array.from({
|
||||
length: (n - m) + 1
|
||||
}, (_, i) => String.fromCodePoint(m + i));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TEST
|
||||
return lstRanges
|
||||
.map(([from, to]) => charRange(from, to).join(' '))
|
||||
.join('\n')
|
||||
|
||||
})([['a', 'z'], ['א','ת'],['α', 'ω'],['🐐', '🐟']]);
|
||||
|
|
@ -0,0 +1 @@
|
|||
`c$97+!26
|
||||
|
|
@ -1,10 +1,8 @@
|
|||
function getAlphabet ()
|
||||
local letters = {}
|
||||
for ascii = 97, 122 do
|
||||
table.insert(letters, string.char(ascii))
|
||||
end
|
||||
return letters
|
||||
local letters = {}
|
||||
for ascii = 97, 122 do table.insert(letters, string.char(ascii)) end
|
||||
return letters
|
||||
end
|
||||
|
||||
local alpha = getAlphabet()
|
||||
io.write(alpha[25] .. alpha[1] .. alpha[25] .. "\n")
|
||||
print(alpha[25] .. alpha[1] .. alpha[25])
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Strchr(Vecsmall([97..122]))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$asString = 97..122 | ForEach-Object -Begin {$asArray = @()} -Process {$asArray += [char]$_} -End {$asArray -join('')}
|
||||
$asString
|
||||
|
|
@ -0,0 +1 @@
|
|||
$asArray
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
/*REXX pgm creates an indexable string of lowercase ASCII characters a ──► z */
|
||||
LC='' /*set lowercase letters list to null*/
|
||||
do j=0 for 2**8; _=d2c(j) /*convert decimal J to character. */
|
||||
if datatype(_,'L') then LC=LC||_ /*Lowercase? Then add it to LC list*/
|
||||
end /*j*/ /* [↑] add lowercase letters ──► LC*/
|
||||
say LC /*stick a fork in it, we're all done*/
|
||||
/*REXX program creates an indexable string of lowercase ASCII or EBCDIC characters: a─►z*/
|
||||
$= /*set lowercase letters list to null. */
|
||||
do j=0 for 2**8; _=d2c(j) /*convert decimal J to a character. */
|
||||
if datatype(_, 'L') then $=$ || _ /*Is lowercase? Then add it to $ list.*/
|
||||
end /*j*/ /* [↑] add lowercase letters ──► $ */
|
||||
say $ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
fn main() {
|
||||
// An iterator over the lowercase alpha's
|
||||
let ascii_iter = (0..26).map(|x| (x + 'a' as u8) as char);
|
||||
|
||||
println!("{:?}", ascii_iter.collect::<Vec<_>>());
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
variable alpha_ch = ['a':'z'], a;
|
||||
|
|
@ -0,0 +1 @@
|
|||
variable alpha_st = array_map(String_Type, &char, alpha_ch);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
print(alpha_st[23]);
|
||||
foreach a (alpha_ch)
|
||||
() = printf("%c ", a);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
| asciiLower |
|
||||
asciiLower := String new.
|
||||
97 to: 122 do: [:asciiCode |
|
||||
asciiLower := asciiLower , asciiCode asCharacter
|
||||
].
|
||||
^asciiLower
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(97..122).asAscii;
|
||||
// answers abcdefghijklmnopqrstuvwxyz
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
"abcdefghijklmnopqrstuvwxyz".ascii
|
||||
// answers [ 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122 ]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
10 DIM l$(26): LET init= CODE "a"-1
|
||||
20 FOR i=1 TO 26
|
||||
30 LET l$(i)=CHR$ (init+i)
|
||||
40 NEXT i
|
||||
50 PRINT l$
|
||||
Loading…
Add table
Add a link
Reference in a new issue