Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #44057 > unrolled thread

Unfeasible?

Started by"WJ" <w_a_x_man@yahoo.com>
First post2015-12-03 20:22 +0000
Last post2015-12-06 01:01 +0100
Articles 16 — 7 participants

Back to article view | Back to comp.lang.forth


Contents

  Unfeasible? "WJ" <w_a_x_man@yahoo.com> - 2015-12-03 20:22 +0000
    Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-03 16:17 -0800
      Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-03 17:47 -0800
        Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-03 19:48 -0800
          Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-04 21:13 -0800
            Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-04 23:48 -0800
              Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-05 00:11 -0800
                Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-05 00:35 -0800
                  Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-05 00:40 -0800
      Re: Unfeasible? hughaguilar96@gmail.com - 2015-12-04 20:53 -0800
    Was: Unfeasible? Is: Infeasible rickman <gnuarm@gmail.com> - 2015-12-03 19:18 -0500
    Re: Unfeasible? "WJ" <w_a_x_man@yahoo.com> - 2015-12-04 01:11 +0000
      Re: Unfeasible? rickman <gnuarm@gmail.com> - 2015-12-03 20:50 -0500
    Re: Unfeasible? Bernd Paysan <bernd.paysan@gmx.de> - 2015-12-04 03:23 +0100
    Re: Unfeasible? "WJ" <w_a_x_man@yahoo.com> - 2015-12-04 05:55 +0000
    Re: Unfeasible? Melzzzzz <mel@zzzzz.com> - 2015-12-06 01:01 +0100

#44057 — Unfeasible?

From"WJ" <w_a_x_man@yahoo.com>
Date2015-12-03 20:22 +0000
SubjectUnfeasible?
Message-ID<n3q89d$9d1$1@dont-email.me>
One has a function "get_list" that produces a list.
He has another function "test" that determines whether
a member of the list passes muster.
He has a function "foo" that is used to transform
an element of the list.

The task is to iterate through the list while
printing each element with its index and collecting
the transformations of the elements that fit the bill.

Wouldn't this be painful in the Toy Language (ANS Forth)?

Ruby:

def get_list; ('aa'..'zz').step(36).to_a ; end
def test s; s == s.reverse ; end
def foo x; x.upcase ; end

get_list.each_with_index.
  grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}

 0 - aa
 1 - bk
 2 - cu
 3 - ee
 4 - fo
 5 - gy
 6 - ii
 7 - js
 8 - lc
 9 - mm
10 - nw
11 - pg
12 - qq
13 - sa
14 - tk
15 - uu
16 - we
17 - xo
18 - yy
    ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]

-- 
I would hesitate to describe America as a nation at all in 1997, because it is
something very different: a polyethnic federation administered by a single
ruling class....  [T]he accumulated rot in America has now reached a point
where the pillars of society itself are failing.  --- Ambrose Evans-Pritchard

[toc] | [next] | [standalone]


#44059

FromJulian Fondren <julian.fondren@gmail.com>
Date2015-12-03 16:17 -0800
Message-ID<c6fdaf42-c21b-48ca-a847-c28d757639da@googlegroups.com>
In reply to#44057
On Thursday, December 3, 2015 at 2:24:34 PM UTC-6, WJ wrote:
> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
> 
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
> 
> Wouldn't this be painful in the Toy Language (ANS Forth)?

No.  Thanks for asking!

I would tend to avoid list operations however as, when writing Forth,
I generally avoid dynamic memory allocations.  There's no need for
deforestation optimizations if you don't have forests in the first
place.  And lists in my Forth programs are generally just 'immutable'
structures of indefinite size that I'll be processing in an order
related to how I added them to the list.

For example,

  routing begin:

  route :: index-rule
    if: client request request-uri s" /" compare 0=
        if s" /index.html" client request 'request-uri 2! then false ;

  static :: static-rule

  route :: 404-rule
    always-match
    do: 404 s\" HTTP/1.1 404 Not Found\r\n\r\n" client fail-with ;

  routing end;

There could be any number of routes defined here, and I want to
consider them in order of definition.  I actually could easily use an
array instead of a list - ROUTING END; would just ALLOT the space of
the array and populate it with what's on the stack.  But I didn't
know how exactly how this interface would shake out as I was writing
the code behind it.  More often, there isn't a "OK, no more list
elements" command, and normally you can't use the stack to hold the
contents of the list - you need some place to store them, and then
you may as well leave them where you stored them.

"Tend to avoid" just means that.  If I have an actual goal I'm
writing a program for, from the universe of ways to reach that goal,
I try to select ways that make more sense for the goal.  For example
if I were writing some CGI scripts on a shared hosting server to
serve API requests made by a mobile app, I would care about using as
little CPU as possible - even, maybe, at the expensive of serving the
requests to the demands of the app - as this is a severely
CPU-metered platform and I don't want the server shut down for abuse.
(That's just a (real world) example, not a design endorsement.)

If you never write any actual programs, but just spend all seeing how
cutely you can solve little problems that *might* come up if you
*were* to write an actual program, then I think two things will tend
to happen.  First, you will find yourself valuing languages based on
their cuteness rather than on more practical concerns.  Second, when
it comes time to write something for real, you will stare mutely at a
blank file, trying and failing to see how all those little
lego-pieces you wrote can be assembled into the precise program you
want.

> Ruby:
> 
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
> 
> get_list.each_with_index.
>   grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}
> 
>  0 - aa
>  1 - bk
>  2 - cu
>  3 - ee
>  4 - fo
>  5 - gy
>  6 - ii
>  7 - js
>  8 - lc
>  9 - mm
> 10 - nw
> 11 - pg
> 12 - qq
> 13 - sa
> 14 - tk
> 15 - uu
> 16 - we
> 17 - xo
> 18 - yy
>     ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]

  : | ( "n1 n2 n3 ..." n -- n' )
    begin parse-word ?dup while evaluate , 1+ repeat drop ;

  create results  0
    |  6 66  7 71  4 77 80
    | 10 66  7 70  4 77 85
  constant #results

  #results cells buffer: hits
  variable #hits

  : res ( index -- n )  cells results + @ ;
  : even? ( n -- f )  1 and 0= ;
  : transform ( n -- n' )  negate 2* ;
  : hit ( n -- )  #hits @ cells hits + !  #hits ++ ;

  : process ( -- )
    #results 0 ?do
      cr i .  i res dup .
      dup even? if transform hit ."  *"
    else drop then loop ;

  : .hits ( -- )
    hits #hits @ cells bounds ?do i ? cell +loop ;

  \\
  process 
  0 6  *
  1 66  *
  2 7 
  3 71 
  4 4  *
  5 77 
  6 80  *
  7 10  *
  8 66  *
  9 7 
  10 70  *
  11 4  *
  12 77 
  13 85  ok
  .hits -12 -132 -8 -160 -20 -132 -140 -8  ok

Timings:

  $ time for x in {1..1000}; do sf include foo.fs process .hits bye > /dev/null; done

  real	0m4.077s
  user	0m1.175s
  sys	0m2.976s
  $ time for x in {1..1000}; do ruby foo.rb > /dev/null; done 

  real	0m29.288s
  user	0m25.391s
  sys	0m4.025s


  $ sf include foo.fs ":noname process .hits bye ; 'MAIN ! program foo bye" 
  $ time for x in {1..1000}; do ./foo > /dev/null; done

  real	0m1.033s
  user	0m0.338s
  sys	0m0.773s


Here's a challenge for you.  Write a CGI script in any language that
increments a number, and outputs it, and saves the number.  It is
important that this script never display the same number for two
requests.  It really must be a CGI script.


Cheers,
Julian

[toc] | [prev] | [next] | [standalone]


#44063

FromPaul Rubin <no.email@nospam.invalid>
Date2015-12-03 17:47 -0800
Message-ID<87r3j3dlxc.fsf@nightsong.com>
In reply to#44059
Julian Fondren <julian.fondren@gmail.com> writes:
> if I were writing some CGI scripts on a shared hosting server to
> serve API requests made by a mobile app, I would care about using as
> little CPU as possible

The fork/exec of launching the CGI will probably swamp any CPU cycles
used by the CGI itself.  You probably want to look into SCGI or FastCGI
or something like that.

> Write a CGI script in any language that increments a number, and
> outputs it, and saves the number.  It is important that this script
> never display the same number for two requests.  It really must be a
> CGI script.

flock(2) ?

[toc] | [prev] | [next] | [standalone]


#44066

FromJulian Fondren <julian.fondren@gmail.com>
Date2015-12-03 19:48 -0800
Message-ID<313aca34-5010-4add-91ae-ca23920bc417@googlegroups.com>
In reply to#44063
On Thursday, December 3, 2015 at 7:47:45 PM UTC-6, Paul Rubin wrote:
> The fork/exec of launching the CGI will probably swamp any CPU cycles
> used by the CGI itself.  You probably want to look into SCGI or FastCGI
> or something like that.

Everyone knows this :)

CGI is ancient 90s technology that is not to be used.

I used to chat Perl people who knew this more than a decade ago.
They also knew that PHP was conclusively the worst language in
the entire world, and they had very good templating systems and
ways to avoid dirtying their fingers with HTML, and other good
stuff.

Then I got into the industry and it's CGI-handled PHP from one
horizon to the other.

> > Write a CGI script in any language that increments a number, and
> > outputs it, and saves the number.  It is important that this script
> > never display the same number for two requests.  It really must be a
> > CGI script.
> 
> flock(2) ?

Nah.  Linux is available.

[toc] | [prev] | [next] | [standalone]


#44073

FromPaul Rubin <no.email@nospam.invalid>
Date2015-12-04 21:13 -0800
Message-ID<87bna5eaux.fsf@nightsong.com>
In reply to#44066
Julian Fondren <julian.fondren@gmail.com> writes:
> Then I got into the industry and it's CGI-handled PHP from one
> horizon to the other.

Typically it's mod_php in apache or php/fastcgi in nginx.

>> flock(2) ?
> Nah.  Linux is available.

Ok, what do you have in mind?  I do have some vague memory of a new
kernel service to deliver sequence numbers, but can't find any info and
might be imagining it.

I guess you could use mmap or shm with appropriate protection but that's
not a lot different from using flock.

[toc] | [prev] | [next] | [standalone]


#44075

FromJulian Fondren <julian.fondren@gmail.com>
Date2015-12-04 23:48 -0800
Message-ID<864ac9a3-3712-4028-bbfa-fdfd20e2465d@googlegroups.com>
In reply to#44073
On Friday, December 4, 2015 at 11:13:46 PM UTC-6, Paul Rubin wrote:
> Julian Fondren <julian.fondren@gmail.com> writes:
> > Then I got into the industry and it's CGI-handled PHP from one
> > horizon to the other.
> 
> Typically it's mod_php in apache or php/fastcgi in nginx.

My zabbix instance has 54k hosts in it, which is undercounting
things a bit, unfortunately.  If I were to set up a single
website on a single-service server for a single business,
I would configure it rather differently from what I consider
typical.

> 
> >> flock(2) ?
> > Nah.  Linux is available.
> 
> Ok, what do you have in mind?  I do have some vague memory of a new
> kernel service to deliver sequence numbers, but can't find any info and
> might be imagining it.
> 
> I guess you could use mmap or shm with appropriate protection but that's
> not a lot different from using flock.

O_APPEND :-)

I'm surprised you didn't recommend Redis or memcached.


-- Julian

[toc] | [prev] | [next] | [standalone]


#44077

FromPaul Rubin <no.email@nospam.invalid>
Date2015-12-05 00:11 -0800
Message-ID<874mfxe2mz.fsf@nightsong.com>
In reply to#44075
Julian Fondren <julian.fondren@gmail.com> writes:
> O_APPEND :-)

I don't see how that prevents simultaneous instances from interfering.

> I'm surprised you didn't recommend Redis or memcached.

You said it had to be a cgi, which I took to mean long-running processes
weren't allowed and all the state had to be in the file system.
Otherwise, yes, redis would handle this nicely.

[toc] | [prev] | [next] | [standalone]


#44078

FromJulian Fondren <julian.fondren@gmail.com>
Date2015-12-05 00:35 -0800
Message-ID<9b9342ea-fbc0-43eb-a15e-8b398650e6d5@googlegroups.com>
In reply to#44077
On Saturday, December 5, 2015 at 2:11:17 AM UTC-6, Paul Rubin wrote:
> Julian Fondren <julian.fondren@gmail.com> writes:
> > O_APPEND :-)
> 
> I don't see how that prevents simultaneous instances from interfering.

OK.  Let it be a mystery.  It's a pretty daft solution anyway; looking
back I found it more appealing for being lockless than for being fast.

> 
> > I'm surprised you didn't recommend Redis or memcached.
> 
> You said it had to be a cgi, which I took to mean long-running processes
> weren't allowed and all the state had to be in the file system.
> Otherwise, yes, redis would handle this nicely.

That's reasonable.  There are long-running system services, though.
A typical PHP script, or a WordPress plugin, would keep the number
in the database.


-- Julian

[toc] | [prev] | [next] | [standalone]


#44079

FromPaul Rubin <no.email@nospam.invalid>
Date2015-12-05 00:40 -0800
Message-ID<87zixpcmqa.fsf@nightsong.com>
In reply to#44078
Julian Fondren <julian.fondren@gmail.com> writes:
> OK.  Let it be a mystery.  It's a pretty daft solution anyway; looking
> back I found it more appealing for being lockless than for being fast.

Yeah I think I figured it out, very clever, but I'd have to examine
the kernel source to make sure it really works.

> That's reasonable.  There are long-running system services, though.
> A typical PHP script, or a WordPress plugin, would keep the number
> in the database.

Ok, sure, if something like mysql is available that works too (slowly).
Alternatively nginx with the lua module would be insanely fast.

[toc] | [prev] | [next] | [standalone]


#44072

Fromhughaguilar96@gmail.com
Date2015-12-04 20:53 -0800
Message-ID<d18099aa-52d0-4b3f-8a02-e1aea5d3a965@googlegroups.com>
In reply to#44059
On Thursday, December 3, 2015 at 5:17:23 PM UTC-7, Julian Fondren wrote:
> On Thursday, December 3, 2015 at 2:24:34 PM UTC-6, WJ wrote:
> > One has a function "get_list" that produces a list.
> > He has another function "test" that determines whether
> > a member of the list passes muster.
> > He has a function "foo" that is used to transform
> > an element of the list.
> > 
> > The task is to iterate through the list while
> > printing each element with its index and collecting
> > the transformations of the elements that fit the bill.
> > 
> > Wouldn't this be painful in the Toy Language (ANS Forth)?
> 
> No.  Thanks for asking!
> 
> I would tend to avoid list operations however as, when writing Forth,
> I generally avoid dynamic memory allocations.  There's no need for
> deforestation optimizations if you don't have forests in the first
> place.  And lists in my Forth programs are generally just 'immutable'
> structures of indefinite size that I'll be processing in an order
> related to how I added them to the list.

Well, in my novice-package I use lists as my primary data-structure. I got the idea from Factor that there should be a primary data-structure (Factor has "sequences") that most programs use unless they have some reason to do otherwise --- having a primary data-structure makes general-purpose code more widely usable.

I have CONCRETE-ALLOCATE that is like ALLOCATE except for the dictionary. The words FREE and RESIZE can tell the difference between memory blocks in the heap or dictionary so they can continue to be used without changing the source-code. Because of this, it is easy for me to switch back and forth between having my data in the dictionary or in the heap. I typically start out with everything in the heap and generated at run-time. Sometimes I realize that some of the work being done at run-time could be done at compile-time because all the information needed is available at compile-time, so I will switch it over. This can make the program faster at run-time, but it can also make the program bigger, so there is a trade-off.

> There could be any number of routes defined here, and I want to
> consider them in order of definition.  I actually could easily use an
> array instead of a list - ROUTING END; would just ALLOT the space of
> the array and populate it with what's on the stack.  But I didn't
> know how exactly how this interface would shake out as I was writing
> the code behind it.  More often, there isn't a "OK, no more list
> elements" command, and normally you can't use the stack to hold the
> contents of the list - you need some place to store them, and then
> you may as well leave them where you stored them.

That is why I use dynamic data-structures such as lists --- I don't generally know ahead of time how much data there will be.

> If you never write any actual programs, but just spend all seeing how
> cutely you can solve little problems that *might* come up if you
> *were* to write an actual program, then I think two things will tend
> to happen.  First, you will find yourself valuing languages based on
> their cuteness rather than on more practical concerns.  Second, when
> it comes time to write something for real, you will stare mutely at a
> blank file, trying and failing to see how all those little
> lego-pieces you wrote can be assembled into the precise program you
> want.

This is true. All of these challenges from WJ are a waste of time. They aren't real-world-like.

I have example programs in the novice package that are larger and more real-world-like. A better challenge would be for people to either improve on them to do more, or write their own versions that do the same thing in a different way.

[toc] | [prev] | [next] | [standalone]


#44060 — Was: Unfeasible? Is: Infeasible

Fromrickman <gnuarm@gmail.com>
Date2015-12-03 19:18 -0500
SubjectWas: Unfeasible? Is: Infeasible
Message-ID<n3qm09$cq$1@dont-email.me>
In reply to#44057
You do know "Unfeasible" is not a word, right?

On 12/3/2015 3:22 PM, WJ wrote:
> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
>
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
>
> Wouldn't this be painful in the Toy Language (ANS Forth)?
>
> Ruby:
>
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
>
> get_list.each_with_index.
>    grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}
>
>   0 - aa
>   1 - bk
>   2 - cu
>   3 - ee
>   4 - fo
>   5 - gy
>   6 - ii
>   7 - js
>   8 - lc
>   9 - mm
> 10 - nw
> 11 - pg
> 12 - qq
> 13 - sa
> 14 - tk
> 15 - uu
> 16 - we
> 17 - xo
> 18 - yy
>      ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]
>


-- 

Rick

[toc] | [prev] | [next] | [standalone]


#44062

From"WJ" <w_a_x_man@yahoo.com>
Date2015-12-04 01:11 +0000
Message-ID<n3qp85$a3v$1@dont-email.me>
In reply to#44057
WJ wrote:

> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
> 
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
> 
> Wouldn't this be painful in the Toy Language (ANS Forth)?
> 
> Ruby:
> 
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
> 
> get_list.each_with_index.
>   grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}
> 
>  0 - aa
>  1 - bk
>  2 - cu
>  3 - ee
>  4 - fo
>  5 - gy
>  6 - ii
>  7 - js
>  8 - lc
>  9 - mm
> 10 - nw
> 11 - pg
> 12 - qq
> 13 - sa
> 14 - tk
> 15 - uu
> 16 - we
> 17 - xo
> 18 - yy
>     ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]

For those who are not well-read:

Webster's Third New International Dictionary of the English Language Unabridged
shows that "unfeasible" is correct.

[toc] | [prev] | [next] | [standalone]


#44064

Fromrickman <gnuarm@gmail.com>
Date2015-12-03 20:50 -0500
Message-ID<n3qrcj$frn$1@dont-email.me>
In reply to#44062
On 12/3/2015 8:11 PM, WJ wrote:
>
> Webster's Third New International Dictionary of the English Language Unabridged
> shows that "unfeasible" is correct.

http://www.amazon.com/Garners-Modern-American-Usage-Garner/dp/0195382757/ref=dp_ob_title_bk

-- 

Rick

[toc] | [prev] | [next] | [standalone]


#44065

FromBernd Paysan <bernd.paysan@gmx.de>
Date2015-12-04 03:23 +0100
Message-ID<n3qtba$6cm$1@dont-email.me>
In reply to#44057
WJ wrote:

> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
> 
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
> 
> Wouldn't this be painful in the Toy Language (ANS Forth)?
> 
> Ruby:
> 
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
> 
> get_list.each_with_index.
>   grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}

No pain at all.  Forth:

: .aa ( n -- )  26 /mod 'a' + emit 'a' + emit ;
: reversible? ( n -- flag ) 26 /mod = ;
: gen 0 26 dup * 0 DO
     dup 2 .r ."  - " i .aa cr
     i reversible? IF  i ,  THEN  1+
  36 +LOOP  drop ;
: .gen ( -- ) here gen here swap ?DO  i @ .aa space cell +LOOP ;

cr .gen 
 0 - aa
 1 - bk
 2 - cu
 3 - ee
 4 - fo
 5 - gy
 6 - ii
 7 - js
 8 - lc
 9 - mm
10 - nw
11 - pg
12 - qq
13 - sa
14 - tk
15 - uu
16 - we
17 - xo
18 - yy
aa ee ii mm qq uu yy  ok

There's no need to first generate a list, and then to filter that list.  
Just generate the numbers and filter them right when they have been 
generated.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o ID: kQusJzA;7*?t=uy@X}1GWr!+0qqp_Cn176t4(dQ*
http://bernd-paysan.de/

[toc] | [prev] | [next] | [standalone]


#44068

From"WJ" <w_a_x_man@yahoo.com>
Date2015-12-04 05:55 +0000
Message-ID<n3r9rq$gtt$1@dont-email.me>
In reply to#44057
WJ wrote:

> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
> 
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
> 
> Wouldn't this be painful in the Toy Language (ANS Forth)?
> 
> Ruby:
> 
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
> 
> get_list.each_with_index.
>   grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}
> 
>  0 - aa
>  1 - bk
>  2 - cu
>  3 - ee
>  4 - fo
>  5 - gy
>  6 - ii
>  7 - js
>  8 - lc
>  9 - mm
> 10 - nw
> 11 - pg
> 12 - qq
> 13 - sa
> 14 - tk
> 15 - uu
> 16 - we
> 17 - xo
> 18 - yy
>     ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]

Ocaml:

let rpush aref x = aref := x :: !aref ;;
let rget aref = List.rev !aref ;;

let to_alpha n = [n/26; n mod 26]
  |> List.map (fun m -> String.make 1 (char_of_int (m+97)))
  |> String.concat "" ;;

let get_list () =
  let rec  loop n =
    if n >= 26*26 then []
    else (to_alpha n) :: loop (n+36)
  in loop 0 ;;

let test s = s.[0] = s.[1] ;;
let foo = String.uppercase ;;

let accum = ref [] in
get_list ()
|> List.iteri
     (fun i s -> Printf.printf "%2d %s\n" i s ;
        if test s then rpush accum (foo s)) ;
rget accum ;;

 0 aa
 1 bk
 2 cu
 3 ee
 4 fo
 5 gy
 6 ii
 7 js
 8 lc
 9 mm
10 nw
11 pg
12 qq
13 sa
14 tk
15 uu
16 we
17 xo
18 yy
- : string list = ["AA"; "EE"; "II"; "MM"; "QQ"; "UU"; "YY"]

[toc] | [prev] | [next] | [standalone]


#44095

FromMelzzzzz <mel@zzzzz.com>
Date2015-12-06 01:01 +0100
Message-ID<20151206010133.3820da9e@maxa-pc>
In reply to#44057
On Thu, 3 Dec 2015 20:22:07 -0000 (UTC)
"WJ" <w_a_x_man@yahoo.com> wrote:

> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
> 
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
> 
> Wouldn't this be painful in the Toy Language (ANS Forth)?
> 
> Ruby:
> 
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
> 
> get_list.each_with_index.
>   grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}
> 
>  0 - aa
>  1 - bk
>  2 - cu
>  3 - ee
>  4 - fo
>  5 - gy
>  6 - ii
>  7 - js
>  8 - lc
>  9 - mm
> 10 - nw
> 11 - pg
> 12 - qq
> 13 - sa
> 14 - tk
> 15 - uu
> 16 - we
> 17 - xo
> 18 - yy
>     ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]  
> 
Haskell ;)

[bmaxa@maxa-pc haskell]$ ghc unfeasible.hs
[1 of 1] Compiling Main             ( unfeasible.hs, unfeasible.o )
Linking unfeasible ...
[bmaxa@maxa-pc haskell]$ ./unfeasible 
 0 - aa
 1 - bk
 2 - cu
 3 - ee
 4 - fo
 5 - gy
 6 - ii
 7 - js
 8 - lc
 9 - mm
10 - nw
11 - pg
12 - qq
13 - sa
14 - tk
15 - uu
16 - we
17 - xo
18 - yy

["AA","EE","II","MM","QQ","UU","YY"]
[bmaxa@maxa-pc haskell]$ cat unfeasible.hs
import Text.Printf
import Data.Char

get_list = takeEvery 36 [[a,b] | a <- ['a'..'z'], b <- ['a'..'z']]
    where
        takeEvery n (x:xs) = x : (takeEvery n $ drop (n-1) xs)
        takeEvery _ [] = []

main = do
    putStrLn $ unlines 
             $ map (\(a,b) -> printf "%2d - %s" (a::Int) b)
             $ zip [0..] get_list
    print $ map (map toUpper) $ filter (\x -> x == reverse x) get_list

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web