Last active
October 10, 2019 22:57
-
-
Save iamandrewluca/55073d4e1a3760eba592ae2cbf2bdcd7 to your computer and use it in GitHub Desktop.
Functional Programming (V24)
This file contains 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
;;;; Aplicand recursia si variabile colectoare din lista de atomi arbitrari L | |
;;;; sa se elimine toti atomii numerici | |
(DEFUN remove-numbers (L &OPTIONAL REZ) | |
(COND | |
((NULL L) REZ) | |
((NUMBERP (CAR L)) | |
(remove-numbers (CDR L) REZ) | |
) | |
(T (REVERSE | |
(remove-numbers | |
(CDR L) | |
(CONS (CAR L) REZ) | |
) | |
)) | |
) | |
) | |
(SETQ L `(A 4 C 1 D 3)) | |
(WRITE (remove-numbers L)) ; (A C D) |
This file contains 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
;;;; Este dat șirul de numere întregi L = (M, A1, A2, ..., An). | |
;;;; Să se găsească subșirurile (dacă există) L1 = (Ai, Ai+1, ..., Ai+k), | |
;;;; i+k <= N, i >= 1, pentru care: M = (Ai) + (Ai+1) + ... + (Ai+k) | |
;;;; Exemplu: L = (1 3 -16, 5 7 8 2 2), M = 12 | |
;;;; Rezultat: L1 = (5 7), L1 = (1 3 -16), L1 = (8 2 2) | |
(DEFUN list-sum (L) (apply '+ L)) | |
(DEFUN find-sublist-sum (L M &OPTIONAL REZ) | |
(COND | |
((NULL L) (REVERSE REZ)) | |
((= (list-sum L) M) | |
(find-sublist-sum (rest L) M (CONS L REZ)) | |
) | |
((> (list-sum L) M) | |
(find-sublist-sum (rest L) M REZ) | |
) | |
(T (find-sublist-sum (rest L) M REZ)) | |
) | |
) | |
(SETQ L `(1 3 -16 5 7 0 8 2 2)) | |
(WRITE (find-sublist-sum L 12)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment