Find progressively bigger elements in prolog list

Viewed 783

Given a prolog list I want to create a second one containing the progressively bigger elements. For example,

L = [ 1, 5, 2, 3, 4, 10, 15, 11, 12, 13, 20 ]

Answer = [ 1, 5, 10, 15, 20 ]

My code:

local_max([],_,_).
local_max([XH|XT],Y,temp) :-
    ( XH =< temp ->
      local_max(XT,Y,temp)
      ;
      local_max(XT,[XH|Y],XH)
    ).

I thought this should produce my answered simply reversed but it doesn't. Just false.

The list contains only positive integers so I simply did

local_max([ 1, 5, 2, 3, 4, 10, 15, 11, 12, 13, 20 ],Answer,0).
2 Answers
Related