-1

I am writing an OCaml function that accepts a function type, such as (fun _ -> true) and a list. This is what I currently have:

let drop_until_boolean (x: 'a -> bool) lst =
    match lst with
    | x -> true 

Currently that written statement does not work properly, as it always evaluates to true.

When I call drop_until_boolean (fun _ -> true) [] I want it to return true, and when I call drop_until_boolean (fun _ -> true) ["a"] I want it to return false.

Question Summary: How do I make a function such that drop_until_boolean (fun _ -> true) [] evaluates to true.

Another example: drop_until_boolean (fun s -> s.[0]='z') ["z"] evaluates to true and drop_until_boolean (fun s -> s.[0]='z') ["y"] evaluates to false.

glennsl
  • 28,186
  • 12
  • 57
  • 75

2 Answers2

0

I managed to figure out what I wanted to do, probably did a terrible job explaining it. This is what I wanted.

let drop_until_boolean (x: 'a -> bool) lst = if (x lst) then true else false
  • 1
    This is equivalent to `let drop_until_boolean x lst = x lst`, and even `let drop_until_boolean x = x` thanks to partial application. I don't really see where this would be useful though. – glennsl Oct 01 '17 at 14:19
  • Avoid `if b then true else false`. This is strictly equivalent to `b`. – Lhooq Oct 03 '17 at 14:11
-1

Your current function says the following in English:

Take a function, call it x, and a second value of any type. Examine the second value. In all cases, no matter what the value, return true.

The variable x that appears in your match is a new variable that is matched against the second argument. Since it's just a simple variable, it always matches successfully. It has no relationship to the first parameter (which happens to be named x also).

It shouldn't be surprising that this function always returns true.

I'm not at all sure what you want the function to do. The name suggests it will return some trailing portion of the list that you give it. But you seem to be saying that it should return a boolean.

Let's assume that you want to do something reasonably simple with the second argument. You say the second argument is a list. The most common structure for a simple list-processing function is like this:

let rec my_function list =
    match list with
    | [] ->
        (* Handle case of empty list *)
    | head :: rest ->
        (* Handle case of non-empty list,
           probably with recursive call *)

Maybe you could think about this general structure as a possible solution to your problem. I hope it is helpful.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • I posted the answer of what I actually wanted to do. This is part of a larger portion of the problem, so thats why I had that weird name. This is what the whole answer looked like, `let drop_until_boolean (x: 'a -> bool) lst = if (x lst) then true else false;; let rec drop_until (x: 'a -> bool) lst = match lst with | [] -> [] | h::t -> if drop_until_boolean x h then lst else drop_until x t` – Eduardo Flores Sep 30 '17 at 18:30