Wiki Diff SampleCode, revision #3 to tip
= Prompt the user for a string
----
<eucode>
-- Sample of Euphoria Source code --
include std/io.e
constant CONSOLE = 1,
KEYBOARD = 0,
$
public function prompt_string(sequence prompt)
object answer
puts(CONSOLE, prompt)
answer = gets(KEYBOARD)
puts(CONSOLE, "\n")
if sequence(answer) and length(answer) > 0 then
return answer[1..$-1] -- trim the trailing end-of-line character
else
return ""
end if
end function
writefln("You entered '[]'", { prompt_string("Enter your name: ")})
</eucode>
<eucode>
-- Another version which gives the same results --
include std/console.e
object answer = prompt_string("Enter your name: ")
display("You entered '[]'", {answer})
</eucode>
----
= Sieve Of Erathosthenes
----
<eucode>
include std/io.e
include std/convert.e
include std/map.e
include std/cmdline.e
procedure eratosthenes(integer target)
sequence sieve
integer next_prime
integer limit
sieve = repeat(0, target)
limit = floor(power(target, 0.5))
sieve[1] = 1
next_prime = 2
while next_prime <= target and next_prime != 0 do
if next_prime <= limit then
integer startnum = next_prime + next_prime
for i = startnum to target by next_prime do
sieve[i] = 1
end for
end if
writef( "[] ", next_prime)
next_prime = find(0, sieve, next_prime+1)
end while
return
end procedure
procedure main()
integer n
object cmds
cmds = cmd_parse({{"l", "limit", "The largest prime to find", {HAS_PARAMETER,ONCE}, -1}})
-- Default is 50.
n = to_integer(map:get(cmds, "limit", 50) )
eratosthenes(n)
end procedure
main( )
</eucode>
----
= 99 Bottles of Beer
----
<eucode>
include std/text.e
include std/io.e
function cap(sequence text)
if length(text) > 0 then
return upper(text[1]) & lower(text[2..$])
end if
return ""
end function
function count_bottles(integer n)
sequence text
switch n do
case 0 then
text = "no more bottles"
case else
text = format("[1] [?]", {n, {"bottles","bottle"}})
end switch
return text
end function
procedure main()
sequence bottlecount
bottlecount = count_bottles(99)
for i = 99 to 0 by -1 do
-- Line one of the stanza
writefln("[1] of beer on the wall, [2] of beer.",
{cap(bottlecount), bottlecount})
-- Line 2 of the stanza
if i > 0 then
writef("Take one down and pass it around, ")
bottlecount = count_bottles(i-1)
else
writef( "Go to the store and buy some more, ")
bottlecount = count_bottles(99)
end if
writefln("[1] of beer on the wall.",{bottlecount})
writefln("")
end for
end procedure
main()
</eucode>
----
= Alternative version
----
<eucode>
include std/console.e
object stanza = {
"[] bottles of beer on the wall,",
"[] bottles of beer,",
"take one down, and pass it around,"
}
object bottles = 99
loop do
display(stanza[1],bottles)
display(stanza[2],bottles)
display(stanza[3])
bottles -= 1
display(stanza[1]&"\n",{bottles})
until bottles = 0
end loop
</eucode>
----
= Sequence manipulation
----
<eucode>
include std/console.e -- for display()
include std/sequence.e -- for remove_all()
include std/wildcard.e -- for is_match()
sequence names = {"Fred Smith","Sue Jones","Arnold Kahn","Jane Smith","Ben Franklin","Frank Smith"}
display("There are [] names:\n",length(names))
display(names)
for i = 1 to length(names) do -- remove the Smiths!
if is_match("* Smith",names[i]) then
names[i] = 0
end if
end for
names = remove_all(0,names)
display("Now there are [] names:\n",length(names))
display(names)
</eucode>
{{{
There are 6 names:
{
"Fred Smith",
"Sue Jones",
"Arnold Kahn",
"Jane Smith",
"Ben Franklin",
"Frank Smith"
}
Now there are 3 names:
{
"Sue Jones",
"Arnold Kahn",
"Ben Franklin"
}
}}}
----
= Sequences
----
<eucode>
include std/console.e -- for display()
sequence names = {"Fred Smith","Sue Jones","Arnold Kahn","Jane Smith","Ben Franklin","Frank Smith"}
sequence family = {}
sequence others = {}
for i = 1 to length(names) do
if match("Smith",names[i]) then
family &= "\t" & names[i] & "\n"
else
others &= "\t" & names[i] & "\n"
end if
end for
display("Smiths:\n[]",{family})
display("The rest:\n[]",{others})
</eucode>
{{{
Smiths:
Fred Smith
Jane Smith
Frank Smith
The rest:
Sue Jones
Arnold Kahn
Ben Franklin
}}}
=Sieve Of Erathosthenes
<eucode>
include get.e
procedure eratosthenes(integer target)
sequence sieve
integer next_prime
integer limit
sieve = repeat(0, target)
limit = floor(power(target, 0.5))
sieve[1] = 1
next_prime = 2
while next_prime <= target and next_prime != 0 do
if next_prime <= limit then
for i = next_prime + next_prime to target by next_prime do
sieve[i] = 1
end for
end if
printf(1, "%d ", next_prime)
next_prime = find_from(0, sieve, next_prime+1)
end while
return
end procedure
procedure main(sequence argv)
integer n
n = 50
if length(argv) >= 3 then
argv = value(argv[3])
n = argv[2]
end if
eratosthenes(n)
end procedure
main( command_line() )
</eucode>
=99 Bottles of Beer
<eucode>
include wildcard.e
function cap(sequence x)
x[1] = upper(x[1])
return x
end function
function count_bottles(integer n)
sequence text
switch n do
case 0:
text = "no more bottles"
break
case 1:
text = "1 bottle"
break
case else
text = sprintf("%d bottles", n)
end switch
return text
end function
procedure main()
sequence bottlecount
bottlecount = count_bottles(99)
for i = 99 to 0 by -1 do
-- Line one of the stanza
printf(1, "%s of beer on the wall, %s of beer.\n",
{cap(bottlecount), bottlecount})
-- Line 2 of the stanza
if i > 0 then
puts(1, "Take one down and pass it around, ")
else
puts(1, "Go to the store and buy some more, ")
end if
if i > 0 then
bottlecount = count_bottles(i-1)
else
bottlecount = count_bottles(99)
end if
printf(1, "%s of beer on the wall.\n\n",
{bottlecount})
end for
end procedure
main()
</eucode>
----
<eucode>
-- Sample of Euphoria Source code --
include std/io.e
constant CONSOLE = 1,
KEYBOARD = 0,
$
public function prompt_string(sequence prompt)
object answer
puts(CONSOLE, prompt)
answer = gets(KEYBOARD)
puts(CONSOLE, "\n")
if sequence(answer) and length(answer) > 0 then
return answer[1..$-1] -- trim the trailing end-of-line character
else
return ""
end if
end function
writefln("You entered '[]'", { prompt_string("Enter your name: ")})
</eucode>
<eucode>
-- Another version which gives the same results --
include std/console.e
object answer = prompt_string("Enter your name: ")
display("You entered '[]'", {answer})
</eucode>
----
= Sieve Of Erathosthenes
----
<eucode>
include std/io.e
include std/convert.e
include std/map.e
include std/cmdline.e
procedure eratosthenes(integer target)
sequence sieve
integer next_prime
integer limit
sieve = repeat(0, target)
limit = floor(power(target, 0.5))
sieve[1] = 1
next_prime = 2
while next_prime <= target and next_prime != 0 do
if next_prime <= limit then
integer startnum = next_prime + next_prime
for i = startnum to target by next_prime do
sieve[i] = 1
end for
end if
writef( "[] ", next_prime)
next_prime = find(0, sieve, next_prime+1)
end while
return
end procedure
procedure main()
integer n
object cmds
cmds = cmd_parse({{"l", "limit", "The largest prime to find", {HAS_PARAMETER,ONCE}, -1}})
-- Default is 50.
n = to_integer(map:get(cmds, "limit", 50) )
eratosthenes(n)
end procedure
main( )
</eucode>
----
= 99 Bottles of Beer
----
<eucode>
include std/text.e
include std/io.e
function cap(sequence text)
if length(text) > 0 then
return upper(text[1]) & lower(text[2..$])
end if
return ""
end function
function count_bottles(integer n)
sequence text
switch n do
case 0 then
text = "no more bottles"
case else
text = format("[1] [?]", {n, {"bottles","bottle"}})
end switch
return text
end function
procedure main()
sequence bottlecount
bottlecount = count_bottles(99)
for i = 99 to 0 by -1 do
-- Line one of the stanza
writefln("[1] of beer on the wall, [2] of beer.",
{cap(bottlecount), bottlecount})
-- Line 2 of the stanza
if i > 0 then
writef("Take one down and pass it around, ")
bottlecount = count_bottles(i-1)
else
writef( "Go to the store and buy some more, ")
bottlecount = count_bottles(99)
end if
writefln("[1] of beer on the wall.",{bottlecount})
writefln("")
end for
end procedure
main()
</eucode>
----
= Alternative version
----
<eucode>
include std/console.e
object stanza = {
"[] bottles of beer on the wall,",
"[] bottles of beer,",
"take one down, and pass it around,"
}
object bottles = 99
loop do
display(stanza[1],bottles)
display(stanza[2],bottles)
display(stanza[3])
bottles -= 1
display(stanza[1]&"\n",{bottles})
until bottles = 0
end loop
</eucode>
----
= Sequence manipulation
----
<eucode>
include std/console.e -- for display()
include std/sequence.e -- for remove_all()
include std/wildcard.e -- for is_match()
sequence names = {"Fred Smith","Sue Jones","Arnold Kahn","Jane Smith","Ben Franklin","Frank Smith"}
display("There are [] names:\n",length(names))
display(names)
for i = 1 to length(names) do -- remove the Smiths!
if is_match("* Smith",names[i]) then
names[i] = 0
end if
end for
names = remove_all(0,names)
display("Now there are [] names:\n",length(names))
display(names)
</eucode>
{{{
There are 6 names:
{
"Fred Smith",
"Sue Jones",
"Arnold Kahn",
"Jane Smith",
"Ben Franklin",
"Frank Smith"
}
Now there are 3 names:
{
"Sue Jones",
"Arnold Kahn",
"Ben Franklin"
}
}}}
----
= Sequences
----
<eucode>
include std/console.e -- for display()
sequence names = {"Fred Smith","Sue Jones","Arnold Kahn","Jane Smith","Ben Franklin","Frank Smith"}
sequence family = {}
sequence others = {}
for i = 1 to length(names) do
if match("Smith",names[i]) then
family &= "\t" & names[i] & "\n"
else
others &= "\t" & names[i] & "\n"
end if
end for
display("Smiths:\n[]",{family})
display("The rest:\n[]",{others})
</eucode>
{{{
Smiths:
Fred Smith
Jane Smith
Frank Smith
The rest:
Sue Jones
Arnold Kahn
Ben Franklin
}}}