0

I'm doing some exercise in "List" and "Match-With" but I'm a little stuck.

The exercise tell me Program the upper l x function that counts all the elements larger than x in the list

Example :

upper [10;20;30;40;50] 35; 

the results is 2.

I did this :

let rec upper x l1 =
  match l1 with
  |[] -> 0
  |[a] -> if (a>x) then 1 else 0
  |(a::r) when a>x -> +1 
  |(a::r) when a<x -> upper x r

but nothings work.

glennsl
  • 28,186
  • 12
  • 57
  • 75
SeveN
  • 107
  • 6
  • Please don't edit the question in ways that invalidate existing answers. SO is a collaborative knowledge base of (ideally generally applicable) questions and answers. It's not a personalized help desk. Please respect the people who donate their free time to help you enough to not render their answers useless by changing the context they were written in. – glennsl Oct 22 '22 at 22:31
  • Sorry I'm new and i don't know how to do some things – SeveN Oct 22 '22 at 23:18

2 Answers2

1

Your solution looks pretty good, except this expression:

+1

doesn't make a lot of sense. It's just another way of writing the number 1. Clearly the answer isn't 1 in this case. For one thing, the answer depends on the count for the tail of the list r.

A problem for later is that a > x and a < x do not cover all the cases. In particular, they don't cover the case when a = x.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • Thanks, normally in C I usually use a counter and make "counter = counter +1" but now i don't know how to increment my counter. for the equals, you are rign i will change it – SeveN Oct 22 '22 at 19:36
  • 1
    One way to look at it is that the answer should be 1 more than if a <= x. You're getting a good result for that case. The `+ 1` case is almost the same, it's just 1 bigger. – Jeffrey Scofield Oct 22 '22 at 19:39
  • i don't see how i can do it. I don't see what i have to increment and i have no "return" in OCaml so it's a bit problematic for me – SeveN Oct 22 '22 at 20:13
  • 1
    Look at the last case. You have `upper x r` for the value in that case. The second last case value is one more than this. This should make sense, right? If it does make sense, then how can you write an expression that's one bigger than `upper x r`? (Hint: it would be `upper x r + 1`.) – Jeffrey Scofield Oct 22 '22 at 20:16
  • I tryed this too, but it makes me a warning telling me that the pattern matchin is not exhaustive for example for the case _::_::_. But i don't have this case right? – SeveN Oct 22 '22 at 20:29
  • 1
    Making this change (change +1 to upper x r + 1) will have no effect on the set of patterns. Hence it has no relation to an exhaustiveness error. You can post your modified code to get help with it; I can't be sure what change you actually made. – Jeffrey Scofield Oct 22 '22 at 21:05
  • Edited my code, tell me if you see errors. Thank you for your time – SeveN Oct 22 '22 at 21:27
  • 1
    Aha, I get it. This new code is OK. The supposedly missing case is covered by the two guarded cases at the end. (The warning message points out that this is possible.) If you try ths new code you should see that it works correctly. I might suggest rewriting this without `when` (maybe after some more exercises)--the code might be tidier that way (and less prone to warnings). – Jeffrey Scofield Oct 22 '22 at 21:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248987/discussion-between-seven-and-jeffrey-scofield). – SeveN Oct 22 '22 at 23:19
1
let rec upper x l1 =
  match l1 with
  |[] -> 0
  |[a] -> if (a>x) then 1 else 0
  |(a::r) when a>x -> +1 
  |(a::r) when a<x -> upper x r

You've matched the empty list case, and when there is one or more elements in the list. The case where there is exactly one element is thus extraneous, as the tail of the list will be the empty list.

let rec upper x =
  function
  | [] -> 0
  | x'::xs when x' > x -> 1 + upper x xs
  | _::xs -> upper x xs

For fun, this is readily solved with a fold.

let upper x lst = 
  List.fold_left (fun i x' -> if x' > x then i+1 else i) 0 lst
Chris
  • 26,361
  • 5
  • 21
  • 42
  • Thank you but, this is not good because the function has to take a list and the number. Like i create a list l1 and than i said upper list1 23. and he serach for the the number major of 23 in the list and he says "there are 2 number major of 23" – SeveN Oct 22 '22 at 20:12
  • 1
    @SeveN all the provided functions here take both a number and a list. Note that the order of these is not consistent in your question... – jthulhu Oct 23 '22 at 20:38