Page 1 of 1

How to make a scheme program accept inputs

PostPosted: Sat Nov 11, 2017 3:58 am
by vitomzev
I have the following program
Code: Select all
(define (cat . nums)  (apply string-append (map number->string nums)))
 
(define (my-compare a b)  (string>? (cat a b) (cat b a)))
 
(map  (lambda (xs) (string->number (apply cat (sort xs my-compare))))
      '((1 34 3 98 9 76 45 4) (54 546 548 60)))


and I would like it to accept inputs, that I type for example: (my-function '((1 34 3 98 9 76 45 4) (54 546 548 60)))

and then it executes the program and outputs the result
Thanks in advance

Re: How to make a scheme program accept inputs

PostPosted: Thu Nov 16, 2017 2:28 pm
by sylwester
So imagine you have code as an expression that takes data:

Code: Select all
(some-processexpression some-otherargument data1 data2)


You can always make a true refactoring by wrapping it in a fucntion and call it instead:

Code: Select all
(define (my-abstraction-name arg1 arg2)
  (some-processexpression some-otherargument arg1 arg2))

;; call the abstraction instead:
(my-abstraction-name data1 data2)


These are identical programs. The reason is that you can always replace a procedure call with it-s body and replacing the bound variables for their evaluated argument.