tags:

views:

53

answers:

2

Declare a function of

shortLong: string list -> int * int

, so the result of a call short long ts becomes (k, l) where k is the number of texts in ts with six or fewer characters, and l is the number of texts in ts with seven or peripheral characters. Try to express the short-long in such a way that the argument "not" be run two, but only once.

A: 

You can use explode to convert a string to a character list. Do this recursively on each item in string list (or use foldl).

swegi
You don't need to convert the strings to character list. All you need is there length and you can get that (more efficiently even) without exploding them.
sepp2k
yea I know, but I'm relly lost, and don't know where to start..fun shortLong...
peter81
@sepp2k: you are right, but my last use of SML was years ago
swegi
hmm,, yea......
peter81
+2  A: 

Use foldl starting with (0,0) and then at each iteration use the size function to find out the length of the string and then add 1 to the first or second item of the tuple accordingly.

Edit:

Now that you mention it, an easier way than using foldl is to use List.partition. You can use size to write a function that returns true iff a string has 6 or fewer characters. You can then use partition with that function, to get a list of strings with 6 or fewer characters and another one with the strings with more than 6 characters.

You can then use length on both lists to get their length and then return a tuple containing the two lengths.

Edit2:

Here's the code resulting from the comments over multiple lines for readability:

fun shortLong list =
  let
    val (longs, shorts) = List.partition (fn k => size k > 6) list
  in
    (length shorts, length longs)
  end
sepp2k
I'm still wondering how.. because I can't declare the function string list -> int * int
peter81
@peter: How did you try? What was the error you got? Generally you don't need to declare the type of your functions, it will be inferred automatically.
sepp2k
I am not really started yet! that the probs. I wonder to use List.filter or List.partition
peter81
@peter: Actually using `partition` is an excellent idea. Better than using `foldl`. I'll update my answer.
sepp2k
Do I start like this?? .. . ... .fun shortLong list = List.filter (fn k => size k > 6) list
peter81
@peter: You can, but using `partition` makes more sense than `filter`, so `fun shortLong list = let val (shorts, longs) = List.partition (fn k => size k > 6) list in ... end`
sepp2k
list in.... in what?
peter81
@peter: In the part of the code that you write yourself. All that's left to do is take the lengths of `shorts` and `longs` and put them in a tuple.
sepp2k
soo... like this? .. .. .. .. .. .. fun shortLong list = let val (shorts, longs) = List.partition (fn k => size k > 6) list in short(fn fun => size fun <7) or...???Do I have to use (fn fun => size fun <7) my string and at the first place of the tuple I will have those that are <=6 and at the second place the other ones it's automatically... .... .... I'm lost!
peter81
@peter: No, not like that. The code I gave you is already 90% there. You have a list `shorts` and a list `longs`. All you need to do is take the length of `longs` and the length of `shorts` and put them in a tuple. Then you're done.
sepp2k
fun shortLong list = let val (shorts, longs) = List.partition (fn k => size k > 6) list in (length longs, length shorts) .... .... ike this?
peter81
@peter: Yes, exactly. Though I see that I switched around the variable names. It would make more sense as `fun shortLong list = let val (longs, shorts) = List.partition (fn k => size k > 6) list in (length shorts, length longs) end`
sepp2k
superb... many thanks for the help mate...
peter81