Search code examples
recursionclojurelogarithm

Clojure Recursive Log Table


(ns logtable) 


(defn displayLogTable[start stop step]
    (if (> start stop) nil
        (if < star stop) log10 start)
        (displayLogTable ( + start step) stop step)
        )
))





(defn -main []
    (println "\n Enter your start, stop and step: ")
        (let 
            [ start stop step (Integer/parseInt (read-line))]
            (print (displayLogTable start stop step)
        )

    )

I'm getting a "Too many arguments to if" error I'm trying to implement a recursive function to print out my log table.


Solution

  • There are multiple errors in this part of the code:

    (defn displayLogTable[start stop step]
        (if (> start stop) nil
            (if < star stop) log10 start)
            (displayLogTable ( + start step) stop step)
            )
    ))
    

    Formatting to make it obvious:

    (defn displayLogTable[start stop step]
      (if (> start stop)
        nil ; 1
        (if <  ;2
          star 
          stop) 
        log10 ; 3
        start) ; 4
      (displayLogTable (+ start step) stop step))
    )) ; to much closing parens
    

    The if has too many forms (1-4), where only three are allowed (if predicate then else). The if at 2 is correctly formed, but for sure not what you want (if < is true (always) then star (typo, most likely start) else stop).