0

I know this is a very simple question but I seem to be having some problems.

I am trying to stem a list of words using porter_stem but I am getting an error:

Out of local stack

This is my code:

stemming([],[]).
stemming([H|T], A) :-
    stemming(T,Answer),
    porter_stem(H,S),
    append(Answer,S,A).

Basically the pseudocode for this is as follows:

for all items in list
    stem item
    add item to list2
    return list2

Can anyone please point me in the right direction please?

Lilz
  • 4,013
  • 13
  • 61
  • 95
  • well, the first thing your stemming predicate does is to call stemming, it should be the last thing it does. – m09 Jan 06 '12 at 23:15
  • Thanks, fixed. Still gets the same error though :/ – Lilz Jan 07 '12 at 00:07
  • 2
    I can detail a normal recursion to you if you want, but did you try to use `maplist/3` or equivalent depending on your prolog implementation ? something like `maplist(porter_stem, List, Result).` would suffice. – m09 Jan 07 '12 at 00:32
  • wow thanks. i didnt know maplist existed :O – Lilz Jan 07 '12 at 00:46
  • 1
    @Mog: post that as an answer! – Fred Foo Jan 07 '12 at 14:26
  • you are appending each new element at the end of the accumulating list: `append(Answer,S,A)`. This is quadratic - it has to go over and over from the start of the list, building new copy and discarding old, only to add *one* element each time. You could try `stemming([H|T],[S|R]):- porter_stem(H,S),stemming(T,R)`. That's kind of re-implementing `maplist/2`. Also, see if [this approach](http://stackoverflow.com/questions/8869485/lazy-lists-in-prolog) help. – Will Ness Aug 08 '12 at 22:25

1 Answers1

1

Consider using maplist/3 or equivalent depending on your prolog implementation: something like maplist(porter_stem, List, Result). would suffice.

If you're interested in learning how to build a proper recursion, post a comment and I'll try to expand my answer :)

m09
  • 7,490
  • 3
  • 31
  • 58
  • Thank you so much! If I could understand what was wrong with the code I pasted, I would be everso grateful. – Lilz Jan 07 '12 at 20:27