Saturday, December 26, 2009

How to extract numbers in Scheme?

Hi guys,





I am in a first year prorgamming course using Scheme, and I was just wondering.





Given a number, say, 351021,


how could I extract a select section of the number? For example, 351 or 102?





In strings, I understand that you can use the simple command ';substring';, so i was just wondering if there's something similar to that with numbers





Thanks!How to extract numbers in Scheme?
you could put it in a list and just use tail- recursion to remove the numbers you want e.g.





(define...


(lambda (..)


(...-help n '())))








and so onHow to extract numbers in Scheme?
I think your best bet is to use write-to-string and subseq. Something like this:





(defun subint (num)


(parse-integer (subseq (write-to-string num) 0 3)))





(subint 351021) should give you back:


351


3





One note, I'm doing this in LISP. Where did you find a Scheme class?! I'm a bit jealous.

No comments:

Post a Comment