Example 2 from Application: Mapped Filters*

34 views
Skip to first unread message

Kai Yu Yeo

unread,
Dec 9, 2021, 4:49:16 AM12/9/21
to AI4IMPACT
Question: Could someone point out how the "counter is reset to one" when there is a change in run? Thank you!

  • Based on following the code's progression it would appear that counter increments or decrements by the current state (i.e. "count !"), rather than resetting based on a change from +1 to -1 or vice versa.  
  • "Book1.xlsx" is attached, and illustrates the progression
  • Example 2's code is pasted at the bottom as a reference

: mapf ( seq xt -- seq ) ~ { f }
    [: f >R ;] map
    ['] R> filter
;

: positive-negative ( n -- n )
    0 > if 1 else -1 then
;

: changes ( seq -- seq )
    dup tail swap ['] - zipwith
    ['] positive-negative map
;

: freq ( n -- xt )
    0 ref { count } 0 ref { state }
    [:
    dup state @ =
    swap state !
    if state @ count +! 0
    else count @ state @ count ! then
    dup 0 = if false else true then
    ;]
;

nil 95 cons 105 cons 120 cons 110 cons 100 cons 130 cons 140 cons 150 cons 130 cons 120 cons dup .list
changes dup .list
freq mapf .list 
Book1.xlsx
Reply all
Reply to author
Forward
0 new messages