login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Here is a package that converts strings to numbers. It is only to be considered as a starting point. There is no error checking, it is probably slow and certainly buggy...

fricas
)abbrev package STRCNV StringConversions
StringConversions(): Exports == Implementation where
    Exports == with
coerce: String -> Integer
coerce: String -> Fraction Integer
coerce: String -> List Fraction Integer
coerce: String -> List Integer
coerce: String -> Set Integer
Implementation == add
sexfloat:SExpression:=convert(coerce("Float")@Symbol)$SExpression
coerce(s: String): Fraction Integer == if not NUMBERP(READ_-FROM_-STRING(s)$Lisp)$Lisp then error "coerce: String -> Fraction Integer: not a number" else sex := interpret(packageTran(ncParseFromString(s)$Lisp)$Lisp)$Lisp if (car car sex = sexfloat) then retract((cdr cdr sex) pretend Float)@Fraction(Integer) else if integer?(cdr sex) then ((cdr sex) pretend Integer)::Fraction Integer else (cdr cdr sex) pretend Fraction Integer
coerce(s: String): Integer == PARSE_-INTEGER(s)$Lisp
coerce(r: String): List Fraction Integer == map(coerce #1, split(r, char(" "))$String)_ $FiniteLinearAggregateFunctions2(String, List String, Fraction Integer, _ List Fraction Integer)
coerce(r: String): List Integer == map(coerce(#1), split(r, char(" "))$String)_ $FiniteLinearAggregateFunctions2(String, List String, Integer, _ List Integer)
coerce(r: String): Set Integer == s: String := delete(delete(r, #r), 1) brace(map(PARSE_-INTEGER(#1)$Lisp, split(s, char(" "))$String)_ $FiniteLinearAggregateFunctions2(String, List String, Integer, _ List Integer))$Set(Integer)
fricas
Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/1862277760152673546-25px.001.spad
      using old system compiler.
   STRCNV abbreviates package StringConversions 
------------------------------------------------------------------------
   initializing NRLIB STRCNV for StringConversions 
   compiling into NRLIB STRCNV 
   compiling exported coerce : String -> Fraction Integer
****** comp fails at level 5 with expression: ******
error in function coerce 
(SEQ (LET (|:| #1=#:G671 (|Boolean|)) ((|Sel| |Lisp| NUMBERP) ((|Sel| |Lisp| READ-FROM-STRING) |s|))) (|exit| 1 (IF #1# (SEQ | << | (LET |sex| ((|Sel| |Lisp| |interpret|) ((|Sel| |Lisp| |packageTran|) ((|Sel| |Lisp| |ncParseFromString|) |s|)))) | >> | (LET (|:| #2=#:G673 (|Boolean|)) (= (|car| (|car| |sex|)) |sexfloat|)) (|exit| 1 (IF #2# (@ (|retract| (|pretend| (|cdr| (|cdr| |sex|)) (|Float|))) (|Fraction| (|Integer|))) (SEQ (LET (|:| #3=#:G672 (|Boolean|)) (|integer?| (|cdr| |sex|))) (|exit| 1 (IF #3# (|::| (|pretend| (|cdr| |sex|) (|Integer|)) (|Fraction| (|Integer|))) (|pretend| (|cdr| (|cdr| |sex|)) (|Fraction| (|Integer|))))))))) (|error| "coerce: String -> Fraction Integer: not a number")))) ****** level 5 ****** $x:= (LET sex ((Sel Lisp interpret) ((Sel Lisp packageTran) ((Sel Lisp ncParseFromString) s)))) $m:= NoValueMode $f:= ((((#:G671 # #) (|s| # #) (|sexfloat| # #) (|#| #) ...)))
>> Apparent user error: No mode in assignment to: sex

Let's try it:

fricas
coerce("123.12")@FRAC INT
An expression involving @ Fraction(Integer) actually evaluated to one of type None . Perhaps you should use :: Fraction(Integer) .




  Subject:   Be Bold !!
  ( 15 subscribers )  
Please rate this page: