Define the function iota1(n, m) that takes positive integers n, m with n < m as input, and outputs the list (n,n+1,n+2,...,m)
I\'ve tried switching the code around mu
There's a few oddities to the code you provided, which I've formatted for readability:
(define (iota1 n m)
(if (eq? n 0)
'()
(append (iota (< n m) (+ n 1))
(list n))))
The first is that the expression (< n m)
evaluates to a boolean value, depending on whether n
is less than m
or not. When you apply iota
to (< n m)
in the expression (iota (< n m) (+ n 1))
, you are giving iota
a boolean value for its first argument instead of a positive integer.
Secondly, the use of append
is strange here. When constructing a list in Racket, it's much more common to use the function cons
, which takes as arguments a value, and a list, and returns a new list with the value added to the front. For example,
(append '(3) (append '(4) (append '(5) '()))) ==> '(3 4 5)
(cons 3 (cons 4 (cons 5 '()))) ==> '(3 4 5)
It's a good idea to opt for using cons
instead of append
because it's simpler, and because it is faster, since cons
does not traverse the entire list like append
does.
Since this sounds a bit like a homework problem, I'll leave you with a "code template" to help you find the answer:
; n : integer
; m : integer
; return value : list of integers
(define (iota1 n m)
(if (> n m) ; base case; no need to do work when n is greater than m
... ; value that goes at the end of the list
(cons ... ; the value we want to add to the front of the list
(iota1 ... ...)))) ; the call to iota, generating the rest of the list