For Programmers: Free Programming Magazines  


Home > Archive > Prolog > June 2007 > String or List TO FLOAT









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author String or List TO FLOAT
calimero22@freemail.it

2007-06-30, 8:04 am

Hi everybody

Is there a predicate that converts a string to float or a list to
float, calculating the effective value?
example:

convert("3+4+7",X) ----> X=14

or

convert([5,+,2,-,1],X) ------> X=6

Thank you to everybody by Giovanni

Mauro Di Nuzzo

2007-06-30, 10:10 pm


Consider that "5+2-1"=[53,43,50,45,49].
This is a quick answer useful if this is not an exercise:

convert(String, X) :-
atom_codes(Atom, String),
catch((atom_to_term(Atom, Term, _), X is Term), _, fail).


Cheers --


M

<calimero22@freemail.it> ha scritto nel messaggio
news:1183179205.635340.309780@k79g2000hse.googlegroups.com...
> Hi everybody
>
> Is there a predicate that converts a string to float or a list to
> float, calculating the effective value?
> example:
>
> convert("3+4+7",X) ----> X=14
>
> or
>
> convert([5,+,2,-,1],X) ------> X=6
>
> Thank you to everybody by Giovanni
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com