Created
December 23, 2018 15:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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