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


Groups > comp.lang.forth > #46977

Re: "pattern matching as inverting functions" for "languages without named parameters"

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: "pattern matching as inverting functions" for "languages without named parameters"
Date 2016-05-07 14:51 +0000
Organization A noiseless patient Spider
Message-ID <ngkvec$e3q$1@dont-email.me> (permalink)
References <fefae4e1-f4e0-40ed-b668-4071310ac798@googlegroups.com> <2016May6.165650@mips.complang.tuwien.ac.at>

Show all headers | View raw


Anton Ertl wrote:

> : sum ( list -- n )
>        {
>               { [ <nil> ] [ 0 ] }
>               { [ <cons> ] [ sum + ] }
>        } case ;

OCaml:

Not tail-recursive (can blow the stack):

let rec sum = function
    []      -> 0
  | a::more -> a + sum more ;;


Tail-recursive:

let sum list =
  let rec loop result = function
      []      -> result
    | a::more -> loop (result + a) more
  in loop 0 list ;;


Testing the tail-recursive version:


sum (Array.to_list (Array.init 20_000 abs)) ;;
 ===>
199990000

-- 
If confirmed, Garland would be the fourth Jewish justice on the nation's
highest court, which is comprised entirely of Jews and Catholics. The three
current Jewish members of the Supreme Court are Ruth Bader Ginsburg, Elana
Kagan, and Stephen Breyer.
www.jta.org/2016/03/16/news-opinion/united-states/obama-to-name-jewish-federal-judge-to-supreme-court

Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

"pattern matching as inverting functions" for "languages without named parameters" Andreas Wagner <andreas.wagner@lowfatcomputing.org> - 2016-05-03 11:24 -0700
  Re: "pattern matching as inverting functions" for "languages without named parameters" Andreas Wagner <andreas.wagner@lowfatcomputing.org> - 2016-05-03 11:42 -0700
  Re: "pattern matching as inverting functions" for "languages without named parameters" anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-06 14:56 +0000
    Re: "pattern matching as inverting functions" for "languages without named parameters" anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-06 16:35 +0000
      Re: "pattern matching as inverting functions" for "languages without named parameters" Paul Rubin <no.email@nospam.invalid> - 2016-05-06 09:43 -0700
        Re: "pattern matching as inverting functions" for "languages without named parameters" anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-07 13:57 +0000
          Re: "pattern matching as inverting functions" for "languages without named parameters" Paul Rubin <no.email@nospam.invalid> - 2016-05-07 17:51 -0700
            Re: "pattern matching as inverting functions" for "languages without named parameters" anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-08 14:02 +0000
    Re: "pattern matching as inverting functions" for "languages without named parameters" "WJ" <w_a_x_man@yahoo.com> - 2016-05-07 14:51 +0000

csiph-web