Advertisement
teleias

Some basic functions in LISP

Nov 9th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.64 KB | None | 0 0
  1. (define (mySquare x)
  2.   (* x x)
  3. )
  4. (define (appendd x L)
  5.   (if (null? L) (cons x '() )
  6.       (cons (car L) (appendd x (cdr L)))))
  7. (define (squareList L)
  8.   (if (null? L)
  9.       '()
  10.       (cons (mySquare(car L)) (squareList(cdr L)))
  11.   )
  12. )
  13. (define (concat M N)
  14.    (if (null? M) N
  15.       (cons (car M) (concat (cdr M) N) )
  16.      )
  17. )
  18. (define (tail L)
  19.   (if (null? (cdr L)) (car L) (tail (cdr L)))
  20. )
  21.  
  22. (define (tailless L)
  23.   (if (null? (cdr L)) '() (cons (car L) (tailless (cdr L)))))
  24.  
  25. (define (secondFinder L)
  26.   (car (cdr L)))
  27.  
  28. (define (nFinder n L)
  29.   (if (null? L) -1
  30.    (if (= n 1) (car L) (nFinder (- n 1) (cdr L) ) ) )
  31.   )
  32. ;(nFinder 3 '("a" "b" "c"))
  33.  
  34. (define (evenFinder n L)
  35.   (if (null? (cdr L) )
  36.   "1" (cons (nFinder (+ n 2) (cdr (cdr L))) '())))
  37.  
  38. (define (removeNegs L)
  39.   (if ( null? L ) '()
  40.       (if (< (car L) 0) (removeNegs (cdr L))
  41.           (cons (car L) (removeNegs (cdr L))))))
  42.  
  43. (define (isNegative x) (< x 0))
  44. (define (isEven x) (= 0 (modulo x 2)))
  45.  
  46. (define (removeF f L)
  47.   (if ( null? L ) '()
  48.       (if (f (car L)) (removeF f (cdr L))
  49.           (cons (car L) (removeF f (cdr L))))))
  50.  
  51. (define (removeFAfterG f g L)
  52.   (if ( null? L ) '()
  53.       (if (f (g (car L))) (removeFAfterG f g (cdr L))
  54.           (cons (car L) (removeFAfterG f g (cdr L))))))
  55. ;(removeFAfterG isEven mySquare '(1 4 3 5 9 12))
  56.  
  57. (define (removeQAfterG g L)
  58.   (if ( null? L ) '()
  59.       (if ((lambda (x) (not (= 0 (modulo x 3)))) (g (car L))) (removeQAfterG g (cdr L))
  60.           (cons (car L) (removeQAfterG g (cdr L))))))
  61. ;(removeQAfterG mySquare '(1 4 3 5 9 12))
  62.  
  63. (define (isGreaterThan x)
  64.   (lambda (n) (> x n)))
  65. ;((isGreaterThan 5) 3)
  66.  
  67. ;take in a function (isNegative) and output a function (removeAllNegs)
  68. (define (removeIfBuilder f)
  69.   (define (remover L)
  70.     (if (null? L) '()
  71.        (if (f (car L)) (remover (cdr L))
  72.            (cons (car L) (remover (cdr L))))))
  73.   remover
  74. )
  75. ;removes all negatives
  76. ;((removeIfBuilder isNegative) '(-1 93 -5 6 -7 -10 2))
  77. ;removes all negative and even numbers
  78. ;((removeIfBuilder isEven) ((removeIfBuilder isNegative) '(-1 93 -5 6 -7 -10 2)))
  79. (define (removeIfAorBBuilder f g)
  80.   (define (remover L)
  81.     (if (null? L) '()
  82.         (if (or (f (car L)) (g (car L) )) (remover (cdr L))
  83.             (cons (car L) (remover (cdr L))))))
  84.   remover
  85. )
  86. ;((removeIfAorBBuilder isNegative isEven) '(-1 93 -5 6 -7 -10 2))
  87. (define (myCompose f g)
  88.   (define (composition x)
  89.     (f (g x)))
  90.   composition
  91.   )
  92. (define (lambdaCompose f g)
  93.   (lambda (x) (f (g x))))
  94.  
  95. (define (_reverse L)
  96.   (if (null? (cdr L))
  97.           (cons (car L) '())
  98.           (appendd (car L) (_reverse (cdr L)))))
  99. ;(_reverse '( 0 1 2 ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement