Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created December 23, 2018 15:35
Show Gist options
  • Save DonaldKellett/92e16181432032ec69689871aa5472d9 to your computer and use it in GitHub Desktop.
Save DonaldKellett/92e16181432032ec69689871aa5472d9 to your computer and use it in GitHub Desktop.
Learn Prolog Now! - Chapter 6 - Practical Session - Programming Exercise 3 - Flatten a list
% flatten/2 - Checks whether the second argument is a fully flattened form of the first, both of which are lists
% Caveat - This solution doesn't make use of append/3 as the exercise page explicitly stated but uses append/2 instead (probably not what the author intended) :p
flatten([], []).
flatten([X | List], [X | Flat]) :- \+ is_list(X), flatten(List, Flat).
flatten([X | List], NewFlat) :- is_list(X), flatten(X, XFlat), flatten(List, Flat), append([XFlat, Flat], NewFlat).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment