(defun LM_maxsublist
( lst
/ idx rtn tmp
) ;; Define function and declare arguments & local variables
;; Initialise variables
;; 'rtn' holds the value that will eventually be returned by the function
;; 'tmp' holds the comparison value to test whether each item improves the overall sum, initialised to the same value as 'rtn'
tmp rtn
;; Index variable initialised to 1 as we've already processed the first item
idx 1
) ;; end setq
;; For every other item in the list
;; If the item is greater than the current sum plus the item
(if (< (+ (car tmp
) itm
) itm
) ;; Then all previous items would contribute negatively to the sum, so start the sublist at this item
;; Else add this item to the current sum and update the upper index
) ;; end if
;; If the current sum is greater than our current maximum
;; Then update the current maximum as we've found something better
) ;; end if
;; Increment the index variable for each item processed
) ;; end foreach
;; Return the maximum sum obtained and corresponding indexes
rtn
) ;; end defun