Condition:
when 1. list was longer than the 2. list Return Value :1
when 1. list had the same number of elements as the 2. list Return Value : 0
when the 1. list was shorter than the w. list Return Value : ~1
([1,2,4,5],[3,2,5]);
1
([1,2],[1,5]);
0
([5],[8,2,3]);
~1
Well the length function could be your friend in this case:
fun foo (xs, ys) =
let
val len_xs = length xs
val len_ys = length ys
in
case (len_xs < len_ys, len_xs > len_ys) of
(true, false) => ~1
| (false, true) => 1
| (false, false) => 0
end
which will yield the results:
- foo ([1,2,4,5],[3,2,5]);
val it = 1 : int
- foo ([1,2],[1,5]);
val it = 0 : int
- foo ([5],[8,2,3]);
val it = ~1 : int
However this is both inefficient and ugly. So we can also just pull off one element from each of the list at a time until one of them (or both) gets empty:
fun bar ([], []) = 0
| bar (_, []) = 1
| bar ([], _) = ~1
| bar (_ :: xs, _ :: ys) = bar (xs, ys)
Which gives the results:
- bar ([1,2,4,5],[3,2,5]);
val it = 1 : int
- bar ([1,2],[1,5]);
val it = 0 : int
- bar ([5],[8,2,3]);
val it = ~1 : int
Related
I wrote these function to build a sequence from a function i am having a stack overflow error while testing it
let rec from_fun f ()=
match f () with
| None -> Nil
| Some e -> Cons(e, from_fun f)
from_fun (fun () -> let x = 0 in if x<10 then Some (x+1) else None)
thanks
Your function always returns Some 1. It never returns None. So the sequence is infinitely long and the stack overflows while building it.
If you want a function to return different values when you call it, you can do two things. First, you can pass it different parameters. This isn't possible for your design of from_fun--the parameter to the function is always (). Second, your function can be impure. I.e., the function can maintain some mutable state.
Here is an example of a generator:
let range_generator from below step =
let counter = ref from
in fun () ->
if (!counter < below)
then (let result = (Some !counter) in
counter := !counter + step;
result)
else None
For example, a call to range_generator 0 10 2 returns a closure over an internal counter mutable variable which generates all natural even numbers below 10:
# let gen = range_generator 0 10 2;;
val gen : unit -> int option = <fun>
Each call to gen possibly mutates the internal counter:
# gen();;
- : int option = Some 0
# gen();;
- : int option = Some 2
# gen();;
- : int option = Some 4
# gen();;
- : int option = Some 6
# gen();;
- : int option = Some 8
# gen();;
- : int option = None
# gen();;
- : int option = None
With your function:
# from_fun (range_generator 0 5 1);;
- : int list = [0; 1; 2; 3; 4]
The variable x you are using is local to the anonymous function you are using. As a result the function always return Some 1.
What you probably wanted to do is for the function to take an argument:
let rec from_fun f n =
match f n with
| None -> Nil
| Some e -> Cons(e, from_fun f e)
let seq = from_fun (fun x -> if x<10 then Some (x+1) else None) 0
EDIT:
Here is a solution with the appropriate type signature:
let rec from_fun f () =
match f () with
| None -> Nil
| Some e -> Cons(e, from_fun f ())
let x = ref 0
let seq = from_fun
(fun () ->
let v = !x in
if v < 10
then begin
x := v + 1;
Some v
end
else None)
()
It is worth noting that because of the side effects, you would have to reinitialise x before building a new sequence. The unit argument passed in parameter to from_fun is unnecessary, you could remove it.
I'm trying to write a function that takes in a list, and returns the number of successive duplicate elements in the list.
For example, given [1;2;3;3;4;4;5], the function should return 2
This is my initial implementation, but unfortunately it always returns 0. I'm not quite sure where the bug lies.
Any help on how to improve it will be highly appreciated.
let rec count_successive_duplicates (lst: int list) (count: int) : (int) =
match lst with
| [] | [_]-> 0
| x :: y :: tl ->
if x = y then count_successive_duplicates (y::tl) (count + 1) else count_successive_duplicates (y::tl) count
;;
let () =
print_int (count_successive_duplicates [1;2;3;3;4;4;5] 0)
In the end, you'll want to return the accumulator with the count instead of 0 always:
let rec count_successive_duplicates (lst: int list) (count: int) : (int) =
match lst with
| [] | [_] -> count
(* ^^^^^ */)
| x :: y :: tl -> count_successive_duplicates (y::tl) (count + if x = y then 1 else 0)
Seems I was doing something silly by always returning 0 for the base case, instead of the computed count. The previous version was just ignoring the computed count it received. This now works:
let rec count_successive_duplicates lst count : (int) = match lst with
| [] | [_]-> count
| x :: y :: tl ->
if x = y then count_successive_duplicates (y::tl) (count + 1) else count_successive_duplicates (y::tl) count
;;
let () =
print_int (count_successive_duplicates [1;2;3;3;4;4;5] 0)
Given the declaration of the swapIfAdjacent function I need to do the following:
swapIfAdjacent :: Eq a => a -> a -> [a] -> [a]
If either a,b or b,a are consecutive items, then for the first and only occurrence the order of these two items should be swapped.
My code so far:
swapIfAdjacent _ _ [] = []
swapIfAdjacent a b (x:xs) | a `elem` xs && b `elem` xs
= if adjacent a b xs then swap a b (x:xs) else (x:xs)
| otherwise = error "not adjacent"
where
swap _ _ [] = []
swap a b (x:xs)
| a == x = b : swap a b xs
| b == x = a : swap a b xs
| otherwise = x : swap a b xs
The adjacent function that I'm using is already defined as follows:
adjacent :: Eq a => a -> a -> [a] -> Bool
adjacent a b (x:y:etc) | x == a
= y == b
| x == b
= y == a
| otherwise
= adjacent a b (y:etc)
adjacent _ _ _ = False
I have observed that according to my implementation the swapping procedure occurs for every element and not only for the first ones and also the following error:
swapIfAdjacent 'a' 'a' "banana" --> banana
while
swapIfAdjacent 'b' 'b' "banana" --> error "not adjacent"
Both of the above examples should produce as output the whole list and not errors. I assume the error has to do with the fact that 'b' is appearing in the list only once "banana" while 'a' multiple times.
You are doing a lot of unnecessary work here. Here's an implementation to consider.
swapIfAdjacent a b (x:y:xys) | (a, b) == (x, y) || (b, a) == (x, y) = y:x:xys
| otherwise = x : swapIfAdjacent a b (y:xys)
swapIfAdjacent _ _ xys = xys
You don't need any of elem, adjacent or swap.
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
i am trying to write a very simple function in haskell to change a value in a list depending on an input as follows
update_game :: [Int] -> Int -> Int -> [Int]
update_game (x:xs) row take_amnt | row == 1 = x - take_amnt:xs
| row == 2 = x : head(xs) - take_amnt : tail(xs)
| row == 3 = x : head(xs) : last(xs) - take_amnt`
the first two cases work fine, however the last case is causing me problems and im not sure why, the errors i get are :
http://i.stack.imgur.com/jpT8b.png
http://i.stack.imgur.com/tlz5t.png
The second argument to : should be a list, but last(xs) - take_amnt apparently yields only a single element. Try
row == 3 = x : head(xs) : [last(xs) - take_amnt]
The second parameter in ":" should be a list, last(xs) - take_amnt gives an element only.
wrap it in "[]" which would be [last(xs) - take_amnt]
last(xs) - take_amnt
is an Int, but the second argument of (:) must be a list, since (:) :: a -> [a] -> [a].
If your lists are always three elements long (but then you should probably use a tuple instead of a list), as it seems, wrapping that in a [ ] would solve it with the correct semantics,
update_game :: [Int] -> Int -> Int -> [Int]
update_game (x:xs) row take_amnt | row == 1 = x - take_amnt:xs
| row == 2 = x : head(xs) - take_amnt : tail(xs)
| row == 3 = x : head(xs) : [last(xs) - take_amnt]
However, it would be better to pattern-match accordingly
update_game [x,y,z] 1 take_amnt = [x - take_amnt, y, z]
update_game [x,y,z] 2 take_amnt = [x, y - take_amnt, z]
update_game [x,y,z] 3 take_amnt = [x, y, z - take_amnt]
update_game _ _ _ = error "Invalid input"
or make it generic without pattern matching
update_game xs index take_amnt = zipWith (-) xs (replicate (index-1) 0 ++ take_amnt : repeat 0)