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


Groups > comp.lang.forth > #49147

Remove consecutive duplicates

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Remove consecutive duplicates
Date 2016-07-14 06:59 +0000
Organization A noiseless patient Spider
Message-ID <nm7d7k$ndh$1@dont-email.me> (permalink)

Show all headers | View raw


At the risk of blowing the stack (not tail-recursive):

let remove_consecutives ?(test=(=)) list =
  let rec loop = function
      [] -> []
    | a::b::rest when test a b -> loop (b::rest)
    | a::rest -> a :: loop rest
  in loop list ;;

remove_consecutives [2;2;3;4;4;4;5;2;6];;
 ===>
[2; 3; 4; 5; 2; 6]

remove_consecutives ~test:(<) [2;2;3;4;4;4;5;2;6;6];;
 ===>
[2; 4; 4; 5; 6; 6]

Since ANS Forth is the best language, show a better solution
in ANS Forth.


-- 
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

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


Thread

Remove consecutive duplicates "WJ" <w_a_x_man@yahoo.com> - 2016-07-14 06:59 +0000
  Re: Remove consecutive duplicates Richard Todd <richard.wesley.todd@gmail.com> - 2016-07-14 12:50 -0700
    Re: Remove consecutive duplicates Ron Aaron <rambamist@gmail.com> - 2016-07-15 13:43 +0300
      Re: Remove consecutive duplicates Ron Aaron <rambamist@gmail.com> - 2016-07-15 14:49 +0300
  Re: Remove consecutive duplicates Chris Curl <ccurl609@gmail.com> - 2016-07-14 17:14 -0700
    Re: Remove consecutive duplicates Julian Fondren <julian.fondren@gmail.com> - 2016-07-14 17:45 -0700
      Re: Remove consecutive duplicates missycurl@gmail.com - 2016-07-14 19:46 -0700
  Re: Remove consecutive duplicates "Ala'a" <amalawi@gmail.com> - 2016-07-15 14:25 -0700

csiph-web