Delete the ith element of a list










1















Write a function that deletes the ith element of a list. If the length of the list is less than i, return the list.



This is the output wanted:



- deleteIth([1,2,3,4,5,6],3);
val it = [1,2,4,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


Here's my code:



fun deleteIth (L, i) =
let
(* Deletes the element of a list at ith index *)
fun delete (nil, i, position) = nil
| delete (x::xs, i, position) = if i = position then xs else

x :: delete (xs, i, position + 1)
in
if i >= 0 andalso i < length L then delete (L, i, 0) else L
end;


note: the line x :: delete (xs, I, position + 1) should be right after the else in the previous line the line wrap made me show the code this way. Sorry for that.



But my code out puts



 - deleteIth([1,2,3,4,5,6],3);
val it = [1,2,3,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


I would appreciate the help thanks.










share|improve this question



















  • 1





    You don't need the helper function or length. The list and the index are all the parameters you need. (Hint: count down instead of up.)

    – molbdnilo
    Nov 14 '18 at 6:44











  • Thanks, I figured out how to get the output I wanted. But how would I go about doing it without the helper function.

    – Mr.Money
    Nov 15 '18 at 4:26















1















Write a function that deletes the ith element of a list. If the length of the list is less than i, return the list.



This is the output wanted:



- deleteIth([1,2,3,4,5,6],3);
val it = [1,2,4,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


Here's my code:



fun deleteIth (L, i) =
let
(* Deletes the element of a list at ith index *)
fun delete (nil, i, position) = nil
| delete (x::xs, i, position) = if i = position then xs else

x :: delete (xs, i, position + 1)
in
if i >= 0 andalso i < length L then delete (L, i, 0) else L
end;


note: the line x :: delete (xs, I, position + 1) should be right after the else in the previous line the line wrap made me show the code this way. Sorry for that.



But my code out puts



 - deleteIth([1,2,3,4,5,6],3);
val it = [1,2,3,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


I would appreciate the help thanks.










share|improve this question



















  • 1





    You don't need the helper function or length. The list and the index are all the parameters you need. (Hint: count down instead of up.)

    – molbdnilo
    Nov 14 '18 at 6:44











  • Thanks, I figured out how to get the output I wanted. But how would I go about doing it without the helper function.

    – Mr.Money
    Nov 15 '18 at 4:26













1












1








1








Write a function that deletes the ith element of a list. If the length of the list is less than i, return the list.



This is the output wanted:



- deleteIth([1,2,3,4,5,6],3);
val it = [1,2,4,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


Here's my code:



fun deleteIth (L, i) =
let
(* Deletes the element of a list at ith index *)
fun delete (nil, i, position) = nil
| delete (x::xs, i, position) = if i = position then xs else

x :: delete (xs, i, position + 1)
in
if i >= 0 andalso i < length L then delete (L, i, 0) else L
end;


note: the line x :: delete (xs, I, position + 1) should be right after the else in the previous line the line wrap made me show the code this way. Sorry for that.



But my code out puts



 - deleteIth([1,2,3,4,5,6],3);
val it = [1,2,3,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


I would appreciate the help thanks.










share|improve this question
















Write a function that deletes the ith element of a list. If the length of the list is less than i, return the list.



This is the output wanted:



- deleteIth([1,2,3,4,5,6],3);
val it = [1,2,4,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


Here's my code:



fun deleteIth (L, i) =
let
(* Deletes the element of a list at ith index *)
fun delete (nil, i, position) = nil
| delete (x::xs, i, position) = if i = position then xs else

x :: delete (xs, i, position + 1)
in
if i >= 0 andalso i < length L then delete (L, i, 0) else L
end;


note: the line x :: delete (xs, I, position + 1) should be right after the else in the previous line the line wrap made me show the code this way. Sorry for that.



But my code out puts



 - deleteIth([1,2,3,4,5,6],3);
val it = [1,2,3,5,6] : int list

- deleteIth([1,2,3,4,5,6],7);
val it = [1,2,3,4,5,6] : int list


I would appreciate the help thanks.







sml smlnj ml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:20









Simon Shine

9,58013047




9,58013047










asked Nov 14 '18 at 3:06









Mr.MoneyMr.Money

191




191







  • 1





    You don't need the helper function or length. The list and the index are all the parameters you need. (Hint: count down instead of up.)

    – molbdnilo
    Nov 14 '18 at 6:44











  • Thanks, I figured out how to get the output I wanted. But how would I go about doing it without the helper function.

    – Mr.Money
    Nov 15 '18 at 4:26












  • 1





    You don't need the helper function or length. The list and the index are all the parameters you need. (Hint: count down instead of up.)

    – molbdnilo
    Nov 14 '18 at 6:44











  • Thanks, I figured out how to get the output I wanted. But how would I go about doing it without the helper function.

    – Mr.Money
    Nov 15 '18 at 4:26







1




1





You don't need the helper function or length. The list and the index are all the parameters you need. (Hint: count down instead of up.)

– molbdnilo
Nov 14 '18 at 6:44





You don't need the helper function or length. The list and the index are all the parameters you need. (Hint: count down instead of up.)

– molbdnilo
Nov 14 '18 at 6:44













Thanks, I figured out how to get the output I wanted. But how would I go about doing it without the helper function.

– Mr.Money
Nov 15 '18 at 4:26





Thanks, I figured out how to get the output I wanted. But how would I go about doing it without the helper function.

– Mr.Money
Nov 15 '18 at 4:26












1 Answer
1






active

oldest

votes


















1














Since you've got your expected results, here's a shorter version that only traverses the list once, and never beyond the element to remove.

(length must traverse the entire list to determine its length. It's possibly the least useful list function.)



The general case, k > 1 and the list is not empty:



  • To remove the k:th element, remove element k-1 from the tail of the list, then add the head of the original list to the result.

Base cases:



  • Removing element 1 from a list produces the tail of the list.

  • Removing anything from the empty list produces the empty list.

The case where the list is shorter than k will terminate when it reaches the empty list.



Like this:



fun delete_ith (, k) = 
| delete_ith (x::xs, 1) = xs
| delete_ith (x::xs, k) = x :: delete_ith (xs, k - 1)





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292615%2fdelete-the-ith-element-of-a-list%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Since you've got your expected results, here's a shorter version that only traverses the list once, and never beyond the element to remove.

    (length must traverse the entire list to determine its length. It's possibly the least useful list function.)



    The general case, k > 1 and the list is not empty:



    • To remove the k:th element, remove element k-1 from the tail of the list, then add the head of the original list to the result.

    Base cases:



    • Removing element 1 from a list produces the tail of the list.

    • Removing anything from the empty list produces the empty list.

    The case where the list is shorter than k will terminate when it reaches the empty list.



    Like this:



    fun delete_ith (, k) = 
    | delete_ith (x::xs, 1) = xs
    | delete_ith (x::xs, k) = x :: delete_ith (xs, k - 1)





    share|improve this answer





























      1














      Since you've got your expected results, here's a shorter version that only traverses the list once, and never beyond the element to remove.

      (length must traverse the entire list to determine its length. It's possibly the least useful list function.)



      The general case, k > 1 and the list is not empty:



      • To remove the k:th element, remove element k-1 from the tail of the list, then add the head of the original list to the result.

      Base cases:



      • Removing element 1 from a list produces the tail of the list.

      • Removing anything from the empty list produces the empty list.

      The case where the list is shorter than k will terminate when it reaches the empty list.



      Like this:



      fun delete_ith (, k) = 
      | delete_ith (x::xs, 1) = xs
      | delete_ith (x::xs, k) = x :: delete_ith (xs, k - 1)





      share|improve this answer



























        1












        1








        1







        Since you've got your expected results, here's a shorter version that only traverses the list once, and never beyond the element to remove.

        (length must traverse the entire list to determine its length. It's possibly the least useful list function.)



        The general case, k > 1 and the list is not empty:



        • To remove the k:th element, remove element k-1 from the tail of the list, then add the head of the original list to the result.

        Base cases:



        • Removing element 1 from a list produces the tail of the list.

        • Removing anything from the empty list produces the empty list.

        The case where the list is shorter than k will terminate when it reaches the empty list.



        Like this:



        fun delete_ith (, k) = 
        | delete_ith (x::xs, 1) = xs
        | delete_ith (x::xs, k) = x :: delete_ith (xs, k - 1)





        share|improve this answer















        Since you've got your expected results, here's a shorter version that only traverses the list once, and never beyond the element to remove.

        (length must traverse the entire list to determine its length. It's possibly the least useful list function.)



        The general case, k > 1 and the list is not empty:



        • To remove the k:th element, remove element k-1 from the tail of the list, then add the head of the original list to the result.

        Base cases:



        • Removing element 1 from a list produces the tail of the list.

        • Removing anything from the empty list produces the empty list.

        The case where the list is shorter than k will terminate when it reaches the empty list.



        Like this:



        fun delete_ith (, k) = 
        | delete_ith (x::xs, 1) = xs
        | delete_ith (x::xs, k) = x :: delete_ith (xs, k - 1)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 6:05

























        answered Nov 15 '18 at 5:57









        molbdnilomolbdnilo

        40.8k32152




        40.8k32152



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292615%2fdelete-the-ith-element-of-a-list%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto