Advertisement
Tavi33

CFLP 7

Apr 6th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.98 KB | None | 0 0
  1. ;Problema [1]
  2.  
  3. (defun suma(l)
  4.  
  5.     (apply '+ (remove-if-not '(lambda (x) (evenp (position x l))) l))
  6.  
  7. )
  8.  
  9. ;EXEMPLU
  10. ;(suma '(1 2 3 4 5))
  11. ;9
  12. ;> (suma '(0 1 0 1 0 1))
  13. ;0
  14. ;>
  15. ;____________________________________________________________________
  16.  
  17. ;Problema [2]
  18.  
  19. ;(rot-left '(a b c) 1)
  20. ;(B C A)
  21. ;> (rot-left '(a b c) 2)
  22. ;(C A B)
  23. ;>
  24.  
  25. (defun rot-right (lista n)
  26.     (cond
  27.    
  28.     ((= n 0) lista)
  29.     (t (rot-right (append (last lista) (reverse (cdr (reverse lista)))) (- n 1)))
  30.    
  31.     )
  32.    
  33. )
  34. ;(rot-right '(a b c) 1)
  35. ;(C A B)
  36. ;> (rot-right '(a b c) 2)
  37. ;(B C A)
  38. ;>
  39. ;____________________________________________________________________
  40.  
  41. ;Problema [3]
  42.  
  43.  (defun aparitii (lista x)
  44.     (setq count 0)
  45.     (car (last (mapcar '(lambda (i) (if (= i x) (setq count (+ count 1)))) lista)))
  46. )
  47. ;EXEMPLU:
  48. ;>(lungimi '((b c) (d e f)) '())
  49. ;(2 3)
  50. ;> (lungimi '((b c) d e (f) g (h i j)) '())
  51. ;(2 1 3)
  52. ;>
  53. ;> (lungimi '(a b c) '())
  54. ;NIL
  55. ;____________________________________________________________________
  56.  
  57. ;Problema [4]
  58.  
  59. ;(presentp '(a (b c)) 'b)
  60. ;T
  61. ;> (presentp '(a (b c) (d e f) (g)) 'f)
  62. ;T
  63. ;>
  64. ;____________________________________________________________________
  65.  
  66. ;Problema [5]
  67.  
  68. (defun cifre (x lista)
  69.  
  70.     (cond
  71.    
  72.     ((= x 0) lista)
  73.     (t (cifre (floor (/ x 10)) (append (list (mod x 10)) lista)))
  74.    
  75.     )
  76.  
  77. )
  78.  
  79. ;> (cifre 3548 '())
  80. ;(3 5 4 8)
  81. ;____________________________________________________________________
  82.  
  83. ;Problema [6]
  84.  
  85. (defun pozpar (lista)
  86.  
  87.     (do ( (i 1 (+ i 1)) (l lista (cdr l)) (listai '() ) )
  88.    
  89.         ((null l) listai)
  90.         (if (not (= (mod i 2) 0)) (setq listai (append listai (list (car l)))))
  91.     )
  92.  
  93. )
  94.  
  95. ;> (pozpar '(1 2 3 4 5 6 7))
  96. ;(1 3 5 7)
  97. ;____________________________________________________________________
  98.  
  99. ;Problema [7]
  100.  
  101. (defun cmmdc (x y)
  102.  
  103.     (do ( (a x) (b y) )
  104.    
  105.         ((= a b) a)
  106.         (if (> a b) (setq a (- a b)) (setq b (- b a)))
  107.    
  108.     )
  109.  
  110. )
  111.  
  112. ;> (cmmdc 25 5)
  113. ;5
  114. ;> (cmmdc 3 27)
  115. ;3
  116. ;____________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement