How to return item index from list SML?

Katarzyna Piecielska

I have go a problem with function in SML. This function should return list index of number which will not be summed, but was taken to sum. A call of a function: index(10, [1,2,3,4,5,6,7]) Result should be 3 (10 is a sum of numbers, we seek an index from the list which gives us 10, e.g: 1+2+3=6, 1+2+3+4=10, and return previuos one)

fun index (sum : int, numbers : int list) =
    if null numbers
    then 0
    else if hd(numbers) > sum
    then 0
    else 1 + index(sum, (hd(numbers)+(hd(tl numbers)))::(tl numbers))

It seems to work, but result is wrong. Function increments the result every two calling even if it should not. Can anybody tell me how to fix this?

Annie Lagang

You're almost there. While I agree with @koodawg that adding a counter and a running total is another solution for this problem, having those in your code will complicate it more than it needs to be.

First, I have a few comments about your code. You must remove the unnecessary parens. hd(numbers) is same as hd numbers and (hd(tl numbers)) is equal to hd(tl numbers). So your (hd(numbers)+(hd(tl numbers))) could be simplified to (hd numbers + hd(tl numbers)). Also, you can combine if null numbers and if hd(numbers) > sum in a single condition for code brevity since they yield the same result: 0.

I'll try to explain how code works and I hope you'll get the idea where you have to amend your code.

Using your example, index(10, [1,2,3,4,5,6,7]), your code execution will be like this:

1)

 fun index(10, [1,2,3,4,5,6,7]) =
     if 1 > 10 
     then 0 
     else 1 + (10, [1 + 2] append to [2,3,4,5,6,7]) 

new list: [3,2,3,4,5,6,7]
result: 1

2)

 fun index(10, [3,2,3,4,5,6,7]) =
     if 3 > 10 
     then 0 
     else 1 + (10, [3 + 2] append to [2,3,4,5,6,7]) 

new list: [5,2,3,4,5,6,7]
result: 1

3)

 fun index(10, [5,2,3,4,5,6,7]) =
     if 5 > 10 
     then 0 
     else 1 + (10, [5 + 2] append to [2,3,4,5,6,7]) 

new list: [7,2,3,4,5,6,7]
result: 1

4)

 fun index(10, [7,2,3,4,5,6,7]) =
     if 7 > 10 
     then 0 
     else 1 + (10, [7 + 2] append to [2,3,4,5,6,7]) 

new list: [9,2,3,4,5,6,7]
result: 1

5)

 fun index(10, [9,2,3,4,5,6,7]) =
     if 9 > 10 
     then 0 
     else 1 + (10, [9 + 2] append to [2,3,4,5,6,7]) 

new list: [11,2,3,4,5,6,7]
result: 1

6)

 fun index(10, [11,2,3,4,5,6,7]) =
     if 11 > 10 
     then 0 

result: 0

To sum all results: 1 + 1 + 1 + 1 + 1 + 0 = 5 (just like what you said that your function adds 2 to the expected result)

The correct code must behave like this:

1)

 fun index(10, [1,2,3,4,5,6,7]) =
     if 1 > 10 
     then 0 
     else 1 + (10, [1 + 2] append to [3,4,5,6,7]) 

new list: [3,3,4,5,6,7]
result: 1

2)

 fun index(10, [3,3,4,5,6,7]) =
     if 3 > 10 
     then 0 
     else 1 + (10, [3 + 3] append to [4,5,6,7]) 

new list: [6,4,5,6,7]
result: 1

3)

 fun index(10, [6,4,5,6,7]) =
     if 6 > 10 
     then 0 
     else 1 + (10, [6 + 4] append to [5,6,7]) 

new list: [10,5,6,7]
result: 1

4)

 fun index(10, [10,5,6,7]) =
     if 10 > 10 
     then 0

result: 0

To sum all results: 1 + 1 + 1 + 0 = 3 which is the expected answer.

HINT: You always make sure that the new list your function is processing must be smaller than the previous list/original list.

I hope I explained clearly why your code isn't working. I didn't include the code because I know this is a homework for an online class.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python 3: How to move an item from list x to list y and remove it from list x?

来自分类Dev

extract the integer and unicodes from each item in the list

来自分类Dev

如何在Vuetify中的v-list-item-action中使`Item Index`成为函数?

来自分类Dev

Removing Item from List in Scheme leaves empty null list element

来自分类Dev

Get Selected index and Selected item from DataGridViewComboBoxCell in unbound column

来自分类Dev

How to toggle a highlighted selected item in a group list

来自分类Dev

Is it possible to return a list from an iterator block?

来自分类Dev

How to call JS array item / Read console log return

来自分类Dev

List.filter函数在SML / NJ中不起作用

来自分类Dev

如何在SML / NJ中使用LIST_SORT函数?

来自分类Dev

How to change selected item text in list box at run time?

来自分类Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

来自分类Dev

How to create a list of Tab from a list of String?

来自分类Dev

How to return nearest element from an ordered set?

来自分类Dev

Accessing the item at a specified index in a 'SortedSet'

来自分类Dev

How to return the length of a list as type Integer instead of Int in Haskell

来自分类Dev

Android : How to get the Id of selected item from Spinner

来自分类Dev

How to list all items from QListWidget

来自分类Dev

How to get a value from keyvalue pair list?

来自分类Dev

How to extract from this list of tuples and convert into this dictionary?

来自分类Dev

How to return a NULL from a templated method, without using a pointer

来自分类Dev

How to return a simple QSqlQueryModel from a class to another class?

来自分类Dev

How to return a dictionary and multiple variables from a function in python?

来自分类Dev

Multiple checkboxes in list item android

来自分类Dev

"For item in list" loop, need explanation

来自分类Dev

Suitescript to return a the vendor prices for a selected item

来自分类Dev

list.index() not quite working

来自分类Dev

python list math index variable

来自分类Dev

Python list.index()与字典

Related 相关文章

  1. 1

    Python 3: How to move an item from list x to list y and remove it from list x?

  2. 2

    extract the integer and unicodes from each item in the list

  3. 3

    如何在Vuetify中的v-list-item-action中使`Item Index`成为函数?

  4. 4

    Removing Item from List in Scheme leaves empty null list element

  5. 5

    Get Selected index and Selected item from DataGridViewComboBoxCell in unbound column

  6. 6

    How to toggle a highlighted selected item in a group list

  7. 7

    Is it possible to return a list from an iterator block?

  8. 8

    How to call JS array item / Read console log return

  9. 9

    List.filter函数在SML / NJ中不起作用

  10. 10

    如何在SML / NJ中使用LIST_SORT函数?

  11. 11

    How to change selected item text in list box at run time?

  12. 12

    Construct Pandas DataFrame from dictionary in form {index: list of row values}

  13. 13

    How to create a list of Tab from a list of String?

  14. 14

    How to return nearest element from an ordered set?

  15. 15

    Accessing the item at a specified index in a 'SortedSet'

  16. 16

    How to return the length of a list as type Integer instead of Int in Haskell

  17. 17

    Android : How to get the Id of selected item from Spinner

  18. 18

    How to list all items from QListWidget

  19. 19

    How to get a value from keyvalue pair list?

  20. 20

    How to extract from this list of tuples and convert into this dictionary?

  21. 21

    How to return a NULL from a templated method, without using a pointer

  22. 22

    How to return a simple QSqlQueryModel from a class to another class?

  23. 23

    How to return a dictionary and multiple variables from a function in python?

  24. 24

    Multiple checkboxes in list item android

  25. 25

    "For item in list" loop, need explanation

  26. 26

    Suitescript to return a the vendor prices for a selected item

  27. 27

    list.index() not quite working

  28. 28

    python list math index variable

  29. 29

    Python list.index()与字典

热门标签

归档