Anything you can do with a helper function you can do by recursion in the main function, by re-purposing its argument(s).
For instance, your sublist
expects integer numbers as its 2nd and 3rd parameter; you could put anything there (usually a tagged list, like (list 'sublist_internal_invocation n1 n2 ...)
) and have your function react to that fact, knowing that it is it itself that put it there, i.e. it is used as its own helper that way.
That is, if there is a need for it. In your case, there probably isn't, i.e. it can just be written as a plain recursive function, by augmenting the list and / or the indices at each recursive call.
To give you a hint,
(sublist '(a b c d e f g h) 1 4)
is the same as
(sublist '(b c d e f g h) 0 3)
and that is the same as
(cons 'b (sublist '(c d e f g h) 0 2))
Right?
Thus (sublist '(d e f g h) 0 1)
is (cons 'd (sublist '(e f g h) 0 0))
and hence (sublist '(e f g h) 0 0)
must be '()
.
update: if for some reason direct recursion is not what you want, the above fits into the foldr
paradigm so indeed can be translated into it; though with a twist. foldr
is defined so that
(foldr g z (cons x xs)) == (g x (foldr g z xs)) ; AND
(foldr g z (list)) == z
Thus we see that to fit the above examples g
must be defined so that
((g x (foldr g z xs)) i j) ==
== (cons x ((foldr g z xs) i (- j 1))) , WHEN i == 0, j > i ; OR
== ((foldr g z xs) (- i 1) (- j 1)) , WHEN i > 0, j > i ; OR
== (list) , OTHERWISE ; AND
((foldr g z (list)) i j)
== (z i j)
== (list)
The third case in the first clause accounts also for the possibility that the indices supplied by a user were invalid; and the second clause determines the definition of z
.
Now what's left is just to write all this down with the regular lambda
syntax:
(define (sublist xs i j)
((foldr
(lambda (x ys)
(lambda (i j)
(cond
((and (= i 0) (> j i))
(cons x (ys i (- j 1))))
((and ...... )
............)
(else
(list)))))
(lambda (i j)
......)
xs) i j ))
A simpler possibility is
(define (sublist xs i j)
(map .....
(filter
(lambda (xk)
(and (>= (cadr xk) .....) (..... (cadr xk) .....)))
(map list
xs
(build-list (....... xs) (lambda (x) x))))))
You will need to complete the missing parts, on the dots.