Historical SampleCode, Revision 9

Prompt the user for a string

-- 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  
        return ""  
    end if  
end function  
writefln("You entered '[]'", { prompt_string("Enter your name: ")}) 

Sieve Of Erathosthenes

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  
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) ) 
end procedure  
main( )  

99 Bottles of Beer

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)  
      writef( "Go to the store and buy some more, ")  
      bottlecount = count_bottles(99)  
    end if  
   writefln("[1] of beer on the wall.",{bottlecount})  
  end for  
end procedure  
Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu