Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > standard Ml question

standard Ml question
Thread Tools
Forum Regular
Join Date: Jan 2001
Status: Offline
Reply With Quote
Dec 6, 2002, 10:38 AM
 
If I got list with char's, how should I write a funtion to get that list to a int?

ex:

I want this list:

-val chList = [#"5",#"1",#"2"];
>val chList = [#"5",#"1",#"2"] : char list

to this int:

512

the type of that funtion should be:

char list -> int
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Dec 6, 2002, 02:58 PM
 
Assuming you're working with digital numbers, the obvious way would be to iterate through the list multiplying the current value by 10 and adding the new digit.

I don't know what language you're using, but a AppleScript-based model could be:

Code:
set theList to {"5", "1", "2"} set theResult to 0 repeat with i from 1 to (number of items in theList) set theResult to theResult * 10 set theResult to theResult + (item i of theList as number) end repeat theResult
I've used AppleScript as an example because it's the easiest to understand and therefore translate into other languages, but the principle should be the same.

Of course, good code would add error handling, bounds checking, etc.
Gods don't kill people - people with Gods kill people.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:26 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2