1
votes

I'm completely stuck on a problem to write a function that does NOT use recursion, locals, or lambda. Only abstract list functions can be used. The function must input a list of positive integers, and output how many of those integers are strictly greater than 1. ie, the length of the list of all integers > 1

any ideas on how to do this? I've never implemented abstract list functions on their own and I'm not sure which ones would be used in this case.

Also, the language setting is Intermediate Student, no higher. Which is why lambda, and other functions cannot be used.

2

2 Answers

1
votes

Would this fit your restrictions? Subtract one from every number, filter the positives, and take the length of the resulting list:

(define (f lst)
  (length (filter positive? (map sub1 lst))))

> (f '(1 2 3 4 5 0 -1 -2 -3 -4))
4

Works fine in Intermediate Student.

1
votes

Given the language restriction, we can do this using foldl:

(define lst '(-4 -3 -2 -1 0 1 2 3 4 5))

(define (adder e acc)
  (if (> e 1) (add1 acc) acc))

(foldl adder 0 lst)
=> 4

If it weren't for the restriction, there's a simpler way in Racket:

(count (lambda (x) (> x 1)) lst)
=> 4

In this case, the abstract list function in use is count, see the documentation for many more functions available.