Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Functional Programming / 04-Practice-27.10.2022.rkt
blob: cfb1933e12af05e66ece7841b060e82c7ff0f7cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#lang racket

;;; Exercise 01

(define (10^len n)
  (if (= n 0)
    1
    (* (10^len (quotient n 10)) 10)))

(define (substr? a b)
  (define (calc-iter b comp)
    (cond [(= b 0) #f]
          [(= (remainder (- b a) comp) 0) #t]
          [else (calc-iter (quotient b 10) comp)]))
  (calc-iter b (10^len a)))

(substr? 123 5123783)

;;; Exercise 02

(define (my-identity x)
  x)

(define (my-compose f g)
  (lambda (x) (f (g x))))

(define (my-negate p?)
  (not p?))

(define (my-curry f x)
  ((curry f) x))

;;; Exercise 03

(define (difference F a b)
  (- (F a) (F b)))

;;; Exercise 04

(define f
  (lambda (x) (* 2 x)))

;;; Exercise 05

(define (derive f eps)
  (lambda (x) (/ (- (f (+ x eps)) (f x)) eps)))