3

I am trying to implement a function that takes an element and a list and removes said element.

This is not producing an output and I'm not sure why. I am new to F# so it is likely that I am making a basic mistake. Note that I am trying to do this using recursion.

newguy
  • 53
  • 3
  • This alone is just a function sitting there and waiting to be called. You need to call it with something like `remove (3, [1..5])`. – TeaDrivenDev Jan 31 '17 at 18:37
  • When I attempt to call it like that I get "The terminal process terminated with exit code: 1". I'm not sure how to fix that. – newguy Jan 31 '17 at 18:56
  • It is recursive, as you're calling it again inside the function. But I just spotted an error that likely causes the process to terminate because the stack overflows: In the middle case of the match expression, it should be `remove (item, tl)`. You're currently passing in the original list again, which causes the recursion to never terminate. – TeaDrivenDev Jan 31 '17 at 19:06
  • Sorry for all the questions, do you have any idea why it will only remove the first iteration of the element? For example, if I run remove (2, [1;2;3;2;4]) the function returns [1;3;2;4] when it should be returning [1;3;4] – newguy Jan 31 '17 at 19:18
  • You can also upvote an accepted answer. :) – Guy Coder Jan 31 '17 at 19:49

2 Answers2

3

I dont know what the intention of your question is, is it really removing or is it writing some recursive function on a list.

The easiest way to achive what you want is:

let remove item lst = List.filter (fun x -> x <> item)

That is: filter out all elements that are equal to your item

Does that help?

robkuz
  • 9,488
  • 5
  • 29
  • 50
3

Try this.

let rec remove item lst =
    let rec removeInner item lst acc =
       match lst with
       | h::tl when item = h -> removeInner item tl acc
       | h::tl -> removeInner item tl (h::acc)
       | []    -> List.rev(acc)
    removeInner item lst []

[<EntryPoint>]
let main argv = 

    let result = remove 2 [1;2;2;3;4;2]
    printfn "%A" result

    0

This is not producing an output and I'm not sure why.

The reason your version is not generating output is because of this line

| []    -> []

it reads: when the list is empty return an empty list.

What you need to do is accumulate the values that are not removed and return the accumulated values as the result.

Using an accumulator with F# is typical for a starter and is created with this line.

removeInner item lst []

The [] at the end is the accumulator, which is an empty list to start with since you have not added anything to it.

Then as you progress you add to it with this line

| h::tl -> removeInner item tl (h::acc)

and lastly you reverse the values in the accumulator and return it with this line

| []    -> List.rev(acc)
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • You could use [CPS](https://en.wikipedia.org/wiki/Continuation-passing_style). Essentually recursively pass a function on each call and on the way back out of the recursion the function is called appending the item to the list. Since the items are append to the list in the correct order, no need to reverse them. A bit advanced, but you could do it. Funny thing is I just answered something similar using [Prolog](http://stackoverflow.com/a/41915709/1243762), but with Prolog you don't pass a function you pass a [difference list](https://en.wikibooks.org/wiki/Prolog/Difference_Lists). – Guy Coder Jan 31 '17 at 21:10
  • You could use the list append operator [@](https://en.wikibooks.org/wiki/F_Sharp_Programming/Lists#List.append_and_the_.40_Operator) but it is not efficient to append to the end of a list. One of the F# third party libraries created difference list, but I can't find it. – Guy Coder Jan 31 '17 at 21:19
  • Of interst: [continuation passing example](http://stackoverflow.com/a/3248477/1243762) – Guy Coder Jan 31 '17 at 21:20
  • Of interest: [The Continuation Passing Transform and the Yoneda Embedding](https://golem.ph.utexas.edu/category/2008/01/the_continuation_passing_trans.html) If you are taking a class, ask your teacher about this and see what happens. – Guy Coder Jan 31 '17 at 22:15