Last active
April 17, 2021 11:14
-
-
Save kraftdorian/e182e0f2e019db7d057485ee0dc1ecb1 to your computer and use it in GitHub Desktop.
Recursive list minimum in JavaScript written in Prolog style
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
const usePrologStyleList = (array) => { | |
const [head, ...tail] = array; | |
return [head, tail]; | |
}; | |
const listMinimumState = (list, minimum, acc) => { | |
const [head, tail] = usePrologStyleList(list); | |
if (head === undefined) { | |
return minimum = acc; | |
} | |
if (!acc) { | |
return listMinimumState(tail, minimum, head); | |
} else { | |
return listMinimumState(tail, minimum, Math.min(acc, head)); | |
} | |
}; | |
const listMinimum = (list) => listMinimumState(list, null, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is based on this Prolog program.
Use case:
Result: