Hi Kashyap,

> While working on the json library I had to write a string to float

I recommend to use upper case for locally bound symbols

   (de isNum (N)
      (or (= "." N) (num? (format N))) )

The 'num?' can be omitted, because 'format' returns NIL if it does not succeed.

      (or (= "." N) (format N)) )


> function. In a list of characters when I encounter a digit, I'd like to go
> over the rest of the list and consume all the digits (and possibly a
> period) that form a number. I'd appreciate some feedback from a Lispiness
> and PicoLispiness point of view :)

Just a few notes:

(de read-num (N Lst)
   (let
      (C 0 # count of digits found
         P F # period found?

'F' (or 'f' in the original) is probably wrong here. It is not bound and
contains an unpredictable value. You meant

         P NIL  # period found?

         Ds (pack N
            (make
               (while (isNum (car Lst))
                  (ifn P (setq P (= "." (car Lst))))
                  #-> (and (= "." (car Lst)) (on P))
                  (setq C (inc C))
                  #-> (inc 'C)

                  (link (pop Lst))
                  (setq Lst (cdr Lst)) ) ) )
                  #-> (link (++ Lst))   # Same as (link (pop 'Lst))

(pop Lst) does not pop the list, but pops from the CAR of that list which
happens to be a transient symbol with its value being itself. So it luckily
worked ;)

         result (if P Ds (pack Ds ".0")) )  # if a period was not present then 
add a ".0"
      (list C result)) )


There are many ways to write this function. I don't know which would be the best
one, perhaps I would go with this:

(de read-num (N Lst)
   (let L
      (make
         (while (isNum (car Lst))
            (link (++ Lst)) ) )
      (list
         (length L)
         (pack N L (unless (member "." L) ".0")) ) ) )



> On a related note - Is chopping a reasonable way to break an input file
> into characters while parsing?

Yes, but you don't need to 'chop' explicitly. You can call (char) to get a
single character, (line) to get a (chopped) list of characters, or (till) to get
a list of all characters in the file (usually not the best solution).

You may look at e.g. @lib/xm.l to see a non-trivial (yet still simple) parser.


> I mean, would each character now occupy one
> cell?

Yes.
☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to