Get the incrementing elements of a list - list

I have a list and I am trying to get the elements that are greater from the ones on their left. This is my code:
fun incList (hd::h2::tl : int list) = if h2 > hd
then hd::h2::(incList tl)
else hd::(incList tl)
| incList ([x] : int list) = [x]
| incList ([] : int list) = []
Example:
LIST: [1,5,9,2,88,33,90]
CORRECT RESULT:[1,5,9,88,90]
RETURNS: [1,5,9,2,88,33,90]

Fixed my code. It was checking every other character.Now it works:
fun incList ([] : int list) = []
| incList ([x] : int list) = [x]
| incList (h1::h2::t1 : int list) = if h2>h1 then h1::(incList
(h2::t1)) else (incList (h1::t1))

Related

expand list of lists by adding element once to every list

I implement function which adds an element once to every list of a list.
example:
f :: a -> [[a]] -> [[[a]]]
f 7 [[1],[2],[3]]
[[[7,1],[2],[3]],[[1],[7,2],[3]],[[1],[2],[7,3]]]
I start with this solution:
f :: a -> [[a]] -> [[[a]]]
f e xs = ((\n -> (\(x,l)-> if x==n then e:l else l) <$> zip [1..] xs) <$> [1..length xs])
Can you please provide some more nice implementations of this function?
You can implement this with recursion. As base case we consider an empty list:
f _ [] = []
for non-empty lists (x:xs) we can use the first item, which is the first sublist. We thus can produce a list where we prepend the first sublist x with the element e, followed by the remaining items xs, so (e:x) : xs is the first item. For the remaining items we recurse on the tail of the list xs and will for each sublist prepend this with the sublist x:
f e (x:xs) = ((e:x) : xs) : map (x:) (f e xs)
so putting these together gives us:
f :: a -> [[a]] -> [[[a]]]
f _ [] = []
f e (x:xs) = ((e : x) : xs) : map (x:) (f e xs)
Write splits which gives all possible ways of splitting a list
splits :: [a] -> [([a], [a])]
splits xs = zip (inits xs) (tails xs)
for example
> splits "abc"
[("","abc"),("a","bc"),("ab","c"),("abc","")]
and using it write a function that operates on each element of a list
onEach :: (a -> a) -> [a] -> [[a]]
onEach f xs = [ys ++ f z : zs | (ys, z:zs) <- splits xs]
like this
> onEach toUpper "abc"
["Abc","aBc","abC"]
and now f is just
f :: a -> [[a]] -> [[[a]]]
f x = onEach (x:)
Answer of David Flercher with onEach :: (a -> a) -> [a] -> [[a]] very interesting, I do some generalization with typeclass, I think this is usefull when we need some versions of objects mutated in one parameter..:
class Eachable e where
each :: (a -> a) -> e a -> [e a]
Now we can each on different types for example on Lists:
instance Eachable [] where
each _ [] = []
each g (x:xs) = ((g x) : xs) : map (x:) (each g xs)
each (+1) [1,2,3]
[[2,2,3],[1,3,3],[1,2,4]]
and Trees
data Tree a = Empty | Node (Tree a) a (Tree a) deriving Show
instance Eachable Tree where
each _ Empty = []
each g t#(Node l a r) = (\i -> e g 1 i t) <$> [1..count t] where
e _ _ _ Empty = Empty
e g c i (Node l a r) = Node l' a' r' where
a' = if c==i then g a else a
l' = if c==i then l else e g (c+1) i l
r' = if c==i then r else e g (c+(count l)+1) i r
count Empty = 0
count (Node l _ r) = 1 + count l + count r
tr = Node (Node Empty 1 Empty) 2 (Node Empty 3 Empty)
each (+1) tr
[Node (Node Empty 1 Empty) 3 (Node Empty 3 Empty),Node (Node Empty 2 Empty) 2 (Node Empty 3 Empty),Node (Node Empty 1 Empty) 2 (Node Empty 4 Empty)]
and others:
data Animal a = Animal {speed::a,size::a,smart::a} deriving Show
instance Eachable Animal where
each g (Animal sp sz sm) = [Animal (g sp) sz sm, Animal sp (g sz) sm, Animal sp sz (g sm)]
each (+1) $ Animal 1 1 1
[Animal {speed = 2, size = 1, smart = 1},Animal {speed = 1, size = 2, smart = 1},Animal {speed = 1, size = 1, smart = 2}]

SMLNJ list traversion

How would I go about traversing a list in SMLNJ. I have been at this for 3 hours now and I cannot figure it out for the life of me.
So just to traverse and print a list out. In the simplest way [5,2,3] would print out 5 2 3 or a list variant of this.
How would I go about traversing a list in SMLNJ
It depends on the type of traversal you want to do: mapping, folding, iterating.
Using recursion:
(* mapping: *)
fun incr_each_by_1 [] = []
| incr_each_by_1 (x::xs) = x + 1 :: incr_each_by_1 xs
val demo_1 = incr_each_by_1 [5,2,3] (* [6,3,4] *)
(* folding: *)
fun sum_all_together [] = 0
| sum_all_together (x::xs) = x + sum_all_together xs
val demo_2 = sum [5,2,3] (* 10 *)
(* iteration: *)
fun print_each [] = ()
| print_each (x::xs) = ( print (Int.toString x ^ "\n") ; print_each xs )
val demo_3 = print_each [5,2,3] (* no result, but side-effect *)
Using higher-order functions:
val demo_1 = List.map (fn x => x + 1) [5,2,3]
val demo_2 = List.foldl (fn (x, result) => x + result) 0 [5,2,3]
val demo_3 = List.app (fn x => Int.toString x ^ "\n") [5,2,3]

"deleteAll" method only removes one occurrence

I've been asked to write a Standard ML Program which deletes every occurrence of a list from another list. For the life of me I cannot understand what is wrong with my code. Any help would be appreciated!
My input for this program is as follows:
deleteAll [1,2,3] [3,2,1,2,3,2,1,2,3];
However my output is this:
val it = [3,2,2,1,2,3] : int list
When it should be: [3,2,2];
fun length(x) = if x = [] then 0 else 1+length(tl(x));
val length = fn : ''a list -> int
fun drop 0 L = L
| drop n [] = raise c4
| drop n (h::t) = drop (n-1) t;
val drop = fn : int -> 'a list -> 'a list
fun starts [] _ = true
| starts _ [] = false
| starts (h::t) (x::xs) = if(h=x) then starts t xs else false;
val starts = fn : ''a list -> ''a list -> bool
fun deleteAll [] _ = []
| deleteAll xs [] = xs
| deleteAll (x::xs) (y::ys) = if(starts (x::xs) (y::ys))
then deleteAll (x::xs) (drop (length(x::xs)) (y::ys))
else y::(deleteAll (x::xs) ys);
val deleteAll = fn : ''a list -> ''a list -> ''a list
First you don't need to create a length function as length is a build-in function that returns an int representing the elements of an 'a list.
One more thing, you raise an exception c4 in your function drop. Then you should also include that in the beginning of your program.
The primary reason your code doesn't work is your deleteAll function base cases. The corrected version should be:
fun deleteAll [] xs = xs (* when the first list is empty, it should return the original list *)
| deleteAll xs [] = [] (* when the second list is empty, it should return an empty list *)
| deleteAll (x::xs) (y::ys) = if(starts (x::xs) (y::ys))
then deleteAll (x::xs) (drop (length(x::xs)) (y::ys))
else y::(deleteAll (x::xs) ys);
The rest is good! After the change the answer should be correct : )
- deleteAll [1,2,3] [3,2,1,2,3,2,1,2,3];
val it = [3,2,2] : int list

Match Non-exhaustive failure

I cannot figure out which case I am missing here,for my functions,can someone help me find which case to consider also so that I do not get the match non-exhaustive error.It is taking time for me to really figure out which part is missing.
fun eval_1 (nil,_) = nil
| eval_1 (_,nil) = nil
|eval_1 ((x::h::xs),(y::t::ys)) = if x = ~1 then (h,t)::eval_1(xs,ys) else(x,y)::eval_1(h::xs,t::ys);
fun eval_2 (nil,_) = nil
| eval_2 (x,nil) = nil
|eval_2 ((x),(y::ys)) = eval_1 (x,y)::eval_2(x,ys);
fun eval_3 (nil,_) = nil
| eval_3 ((x::nil),nil) = nil
| eval_3 ((x::xs),(ys)) = eval_2(x,ys)::eval_3(xs,ys);
Matching on (x:xs, y:ys) instead of ((x::h::xs),(y::t::ys)) gets rid of the non-exhaustive warning. To make the rest of the code equivalent you can use hd and tl from List to get the head of the tail and the tail of the tail instead of decomposing the list with x::next::xs.
fun eval_1 (nil,_) = nil
| eval_1 (_,nil) = nil
| eval_1 (x::xs, y::ys) = if x = ~1 then (hd xs, hd ys)::eval_1(tl xs, tl ys) else (x,y) :: eval_1(xs, ys)
fun eval_2 (nil,_) = nil
| eval_2 (x,nil) = nil
| eval_2 ((x),(y::ys)) = eval_1 (x,y)::eval_2(x,ys);
fun eval_3 (nil,_) = nil
| eval_3 ((x::nil),nil) = nil
| eval_3 ((x::xs),(ys)) = eval_2(x,ys)::eval_3(xs,ys);
Sample run:
> val eval_1 = fn : int list * 'a list -> (int * 'a) list
val eval_2 = fn : int list * 'a list list -> (int * 'a) list list
val eval_3 = fn : int list list * 'a list list -> (int * 'a) list list list
val it = () : unit
> eval_1([1,2,3], [4,5,6]);
val it = [(1, 4), (2, 5), (3, 6)] : (int * int) list

Ocaml returning a list from a recursive function

I want to go through an array and return a list of ints (the value of indexes) when a value in the array matches true.
The array is a boolean array of just true/false values.
let get_elements (i:int)(b:bool) : int =
if b = true then (i::l)
else (())
;;
let rec true_list (b: bool array) : int list =
(fun i l -> get_elements i l)
;;
The syntax is wrong for my code and I am confused on exactly how to return a list of ints.I only want to return the indexes of those elements that are true in the array.
You refer to 'l' in get_elements, but it's not in the scope of that function.
Here's an approach using a ref to an integer list (a mutable list):
boolarray = [|true; false; true; false; false; true|] ;;
type ilist = (int list) ref ;;
let intlist () : ilist = ref [] ;;
let push ( l: ilist) (x: int) : unit = l := x::(!l) ;;
let lst = intlist () ;;
Array.iteri ( fun i b -> if b = true then (push lst i )) boolarray ;;
!lst ;; (* => int list = [5; 2; 0] *)
Or, if you'd rather avoid refs (which is usually a good idea) this is cleaner:
let get_true_list (b: bool array) : int list =
let rec aux i lst =
if (i = Array.length b) then lst else
(if b.(i) = true then ( aux (i+1) (i::lst)) else (aux (i+1) lst)) in
aux 0 [] ;;
(* using boolarray defined above *)
get_true_list boolarray ;; (* => int list = [5; 2; 0] *)
I present an example which does not use state, avoids the 'if then else' construct making it easier to read and verify.
let mylist = [| true; false; false; true; false; true |] in
let get_true_indexes arr =
let a = Array.to_list arr in
let rec aux lst i acc = match lst with
| [] -> List.rev acc
| h::t when h = true -> aux t (i+1) (i::acc)
| h::t -> aux t (i+1) acc
in
aux a 0 []
in
get_true_indexes mylist