0

I am trying to create code that takes an integer 'a' and a list and returns a list of every value in the list that is less than a. I have created code that will figure out if the first number in the list is less than 'a' but I can't quite figure out the recursion. Any help would be great!

fun smallethan(a,[]) = [] | smallerthan(a,list) = if hd(list) < a then [hd(list)]; 
M. Pollino
  • 45
  • 6
  • Welcome to the site M. Pollino and if you have a moment, take the tour at https://stackoverflow.com/tour. If you wish to mark up code in question text, so integer 'a' becomes `integer a` backtic is your friend. Recursion on a list usually operates on first element on a list and passes a so-called **tail**, that is a list created by taking the already checked **head** away. In your case you would need an accumulator variable for keeping the already found elements that in future will create your full list. Or use filtering, to take only elements you want (Java): lst.stream().filter(e -> e – LAFK 4Monica_banAI_modStrike Mar 17 '18 at 16:37
  • @LIttleAncientForestKami Okay so how would I go about that accumulator variable? – M. Pollino Mar 17 '18 at 17:15

2 Answers2

0

You could use filter. I tested the below code and it works.

filter ((fn x => fn y => (x >= y)) a) lst

Without higher order functions:

fun less (a, []) = [] 
  | less (a , x::lst) = if x < a then [x] @ less (a,lst) else [] @ less (a,lst);

i.e.

fun less (a, []) = [] | less (a , x::lst) = if x < a then [x] @ less (a,lst) else [] @ less (a,lst);
val less = fn : int * int list -> int list
- less (10, [8,9,10,11,12]);
val it = [8,9] : int list
  • I haven't really learned about filters yet. Is there any other way of doing this? – M. Pollino Mar 17 '18 at 17:14
  • Well I was just introduced to ML like two week ago so I don't know all too much. I am just now learning about patterns and that's why I am trying to see if there is a more simple way of doing this.Little Ancient mentioned in the comments an accumulator variable, maybe that is something I could use? – M. Pollino Mar 17 '18 at 17:30
  • Right. Ok, so the error you're making here is that you're forgetting to recursively call the less function. Also use the infix operator :: to access the elements of a list. See my edited comment for an implementation of it. – Kevin Koehler Mar 17 '18 at 17:53
  • You forgot to test your `less` with a non-increasing list. – molbdnilo Mar 17 '18 at 18:22
  • `[x] @ xs` is the same as `x::xs`, but less efficient. `[] @ xs` is usually written `xs`. – molbdnilo Mar 18 '18 at 08:45
0

The case for the non-empty list is

  • if the first element is less than a, put that element in front of what you get from recursing on the tail of the list,
  • otherwise, the result is just the result of recursing on the tail.

Since you've apparently learned about pattern matching, use it:

fun less (a, []) = []
  | less (a, x::xs) = if x < a 
                      then x :: less (a, xs)
                      else less (a, xs)
molbdnilo
  • 64,751
  • 3
  • 43
  • 82