Flatten a list in Prolog

后端 未结 7 594
执笔经年
执笔经年 2020-11-27 08:00

I\'ve only been working with Prolog for a couple days. I understand some things but this is really confusing me.

I\'m suppose to write a function that takes a list

相关标签:
7条回答
  • 2020-11-27 08:37

    Without any other predicate, with tail-recursion only.

    flatten([[X|S]|T], F) :- flatten([X|[S|T]], F).
    flatten([[]|S], F) :- flatten(S, F).
    flatten([X|S], [X|T]) :- \+(X = []), \+(X = [_|_]), flatten(S, T).
    flatten([], []).
    
    0 讨论(0)
提交回复
热议问题