tags:

views:

102

answers:

2

Hi, can anyone help me with this F# code? I am a F# beginner and I have a problem with it. Big thanks

let rec rem l xs = match xs with
| [] -> []
| x::xs -> if x>l then rem l y
                 else x::(rem l y)   

let rec minfree1 l:long xs = match xs with
    | [] -> 1
    | _ -> let nxs = rem xs l
           let l1 = List.length(nxs)
           in  if l1=l then (l+1)
                       else minfree1 l1 nxs

let minfree xs = minfree1 (List.length(xs)) xs
+2  A: 

What exactly doesn't work? What is your code supposed to do? What don't you understand about the errors your compiler gives you?

Some obvious things you should think about are:

  • What is y in your first function? You never declare it.
  • There's no long in F#, there's int64. And you need brackets ( ( ) ) around l:int64 in your second function.
  • The rem function expects xs to be a list, but you pass it l which is not a list in your second function.
  • Probably more.

What is it you're trying to accomplish?

IVlad
I am so sorry I am stupid, I have no problem more with it.
877
+1  A: 

And you're writing using low-level idioms when higher-level programming will do much better. For example

let rem l = List.filter (fun x -> x <= l)

Your algorithm (minfree1) is also doing multiple passes on your data (once for computing the length, another for filtering, repeat). If you actually worked on the pair of the list and its length, this would be faster. In any case, this looks like a task where some actual data-structure will be needed [lists are functional program's assembly-level...]

Jacques Carette