Search code examples
functional-programmingschemeracketinteger-division

Trying to return first 2 digits in scheme


I'm trying to test if i is less than 3 digits, if it is return it, else, recursively call first2 with i/10 untill it is less than 2 digits. With my logic it seems like 12345 should return 12.345 and I could figure out another way to cut off the decimal, but it just keeps returning 100. Why is this? I've been looking for examples of this logic and it seems this should work. It makes no sense to me.

 (define(first2 i)

    (cond(< i 100)

         (i)

         (first2 (/ i 10))))

> (first2 12345)

100

Solution

  • Changed it to an if and looked up the error, found out the i return value can't have a () around it since its not a procedure.

     (define(first2 i)
    
        (if(< i 100)
    
             i
    
             (first2 (/ i 10))))
    
    > (first2 12345)
    12