Constructor of functions in the circuit

It is assumed that the function that I should build should contain a list of numbers as a parameter and give one function in the form of output, which does the following: if the number in the list is positive, add it, if it is negative, multiply by it, and if it is equal 0, dial a number.

For example, if I pass (4 -1 0), it should return a function that takes one parameter, adds 4 to it, multiplies it by -1, expresses a square and returns it.

I think I'm on the right track, but now I'm seriously confused. I'm not necessarily looking for a solution, but any help received there would be awesome. Here is what I still have:

(define (buildseries L)
  (define (a x) 
    (lambda (y) (+ x y)))
  (define (m x) 
    (lambda (y) (* x y)))
  (define (s x) 
    (lambda (x) (* x x)))
  (define (funcfind d)
    (cond
     [(null? d) (a 0)]
     [(> d 0) (a d)]
     [(= d 0) (s d)]
     [(< d 0) (m d)]))
  (funcfind (first L)))

((buildseries '(2)) 2)

I don’t know how to build a function that is an integral part of other functions ... I just lost the feeling here.

+1
3

. . , ( , , ):

(define (fun nums)
  (lambda (x)
    (let loop ((nums nums)
               (value x))
      (if (null? nums) value
          (let ((num (car nums))
                (rest (cdr nums)))
            (cond ((positive? num)
                   (loop rest (+ value num)))
                  ((negative? num)
                   (loop rest (* value num)))
                  ((zero? num)
                   (loop rest (* value value)))))))))

, , , , , , Jon compose.: -)


: : SRFI 1 fold, :

(define (fun nums)
  (define (step num value)
    (cond ((positive? num) (+ value num))
          ((negative? num) (* value num))
          (else (* value value))))
  (lambda (x)
    (fold step x nums)))
+1

, . , buildseries. identity, . compose, f g , . a m, " ", , . , . : (f . g)(x) = f(g(x)).

, , s : " factory", , x. , () x, (define (s x) (* x x)). (= d 0) s, .

. -, ? , l ? , l , , first , , , , rest ?

, . , !

+3

-, bulider, :

(define (num->fun d)
  (cond [(> d 0) (lambda (x) (+ x d))]
        [(= d 0) (lambda (x) (* x x))]
        [(< d 0) (lambda (x) (* x d))]))

, , :

> (map num->fun '(4 -1 0))
=>'(#<procedure> #<procedure> #<procedure>)

so that you can use the high-order compose function to compile this list of functions:

(apply compose (reverse (list-of-functions)

be careful, compose will create functions in the reverse order, so change the list of functions before compiling. you can abstract this:

(define (buildseries L)
  (apply compose (reverse (map num->fun L))))
;try it:
((buildseries '(4 -1 0)) 1);=>25
+1
source

All Articles