Regarding to an older question Find if Duplicates Exist SML NJ, if I want the opposite result:
[1,2,2,3,4,5,6] should return false
[1,2,3,4,5,6,7] should return true
[1,2,3,4,5,6,1] should return false
how can I have it with:
fun duplicated [] = false
| duplicated (x::xs) = (List.exists (fn y => x = y) xs) orelse (duplicated xs)
For example,
fun non_duplicated ps =
case ps of
[] => false
| x::xs' => (List.exists (fn y => x<>y) xs') andalso (non_duplicated xs')
doesn't work.
Why???
Thanks.
If you would like to obtain the opposite result, just define the function as follows:
fun non_duplicated xs = not (duplicated xs)
That said, you can push not inwards the body of duplicated function using De Morgan laws:
not (a orelse b) <=> (not a) andalso (not b)
not exists equal <=> forall (not equal)
not false <=> true
Then you arrive at the opposite version:
fun non_duplicated [] = true
| non_duplicated (x::xs) =
(List.forall (fn y => x <> y) xs) andalso (non_duplicated xs)
Related
isMember:: a -> [a] -> Bool
isMember y [] = False
isMember y (x:xs) =
if y == x then
True
else
isMember y xs
Trying to create a function that will identify whether something is a member of a list. For example:
isMember 6 [1,2,3,4,5,6]
>True
However I keep getting a complier error stating 'no instance for (Eq a) arising from the use of '=='
Help would be appreciated (I'm new to Haskell & Recursion in functional languages so explain like I'm five.)
you are almost there
isMember :: Eq a => a -> [a] -> Bool
isMember _ [] = False
isMember y (x:xs) =
if y == x then True else isMember y xs
What the compiler tells you that you promised to accept any type of list members - but later you use the function == which is not available for all types (for example functions).
By adding Eq a => you say I accept all input which have an equals method.
Some additional notes
You can (re)write the last line as
isMember y (x:xs) = (y == x) || isMember y xs
which is equivalent to your implementation (thanks #chi for the comment).
What is nice about your version is that it is tail recursive.
Another point to note - the pattern:
return something for empty list case (isMember _ [] = False)
and iterate over the list with this value (isMember y (x:xs) = ...)
happens to turn up a lot and has been abstracted into the family of fold -functions (foldl, foldr ...). Putting it in your use case it looks like
isMember y xs = foldl False (\x b -> (x == y) || b) xs
I need to compare if all elements of a given list are unique.
(For the record I am doing so for academic purposes.)
Here is what I have thus far:
allDifferent :: (Eq a) => [a] -> Bool
allDifferent list = case list of
[] -> True
(x:xs) -> if x `elem` xs then False else allDifferent xs
Which works wonderfully!
Now, when I try to do it like this...
allDifferent2 :: (Eq a) => [a] -> Bool
allDifferent2 list
| null list = True
| (head list) `elem` (tail list) || allDifferent2 (tail list) = False
| otherwise
It just doesn't work as intended.
I get the following output from GHCi:
*Main> allDifferent2 [1..4]
False
*Main> allDifferent2 [1..5]
True
*Main> allDifferent2 [1..6]
False
*Main> allDifferent2 [1..7]
True
i.e. For every list with an even amount of elements it outputs False and for an odd amount of elements, True.
What am I missing?
Would anyone care to shine some light?
An alternative exploiting notElem:
allDifferent :: (Eq a) => [a] -> Bool
allDifferent list = case list of
[] -> True
(x:xs) -> x `notElem` xs && allDifferent xs
Minor variant, using pattern matching directly in the equations:
allDifferent :: (Eq a) => [a] -> Bool
allDifferent [] = True
allDifferent (x:xs) = x `notElem` xs && allDifferent xs
I tend to stay away from partial functions like head,tail, so the variants based on guards look worse to me.
I would do this differently. Recursion + elem is O(n²). Alternatively you can first sort the list, and then compare elements pairwise. This way the sorting is O(n⋅log n), and the traversal O(n). So overall O(n⋅log n):
import Data.List
allDifferent :: (Ord a, Eq a) => [a] -> Bool
allDifferent = comparePairwise.sort
comparePairwise :: Eq a => [a] -> Bool
comparePairwise [] = True
comparePairwise [_] = True
comparePairwise (x:y:xs)
| x == y = False
| otherwise = comparePairwise (y : xs)
You can rely on library functions: allDifferent xs = nub xs == xs.
Or, written in point-free notation: allDifferent = uncurry (==) . (nub &&& id).
Using Data.Discrimination.nub, this happens in O(n) time.
The simplest reasonable idiomatic approach I can think of is
allDifferent :: Ord a => [a] -> Bool
allDifferent = pairwiseDifferent . sort
pairwiseDifferent :: Eq a => [a] -> Bool
pairwiseDifferent xs = and $ zipWith (/=) xs (drop 1 xs)
For fun with folds,
import Data.Maybe
pairwiseDifferent xs = foldr go (const True) xs Nothing
where
go x k Nothing = k (Just x)
go x k (Just prev) = x /= prev && k (Just x)
Another option is to use a Set (some of the strictness annotations may not actually be necessary):
import qualified Data.Set as S
allDifferent xs = foldr go (\s -> s `seq` True) xs S.empty
where
go x k s
| S.member x s = False
| otherwise = k $! S.insert x s
Try this:
allDifferent2::(Eq a) => [a] -> Bool
allDifferent2 list
| list == [] = True
| (head list) `elem` (tail list) = False
| otherwise = allDifferent2(tail list)
If the list is [] you should return True (As #bheklilr said :) )
If the list isn't null, you can verify if the first element is in the tail of the list. If it is, return False. Okay.
But when you say "if it is in the tail of the list OR allDifferent2 (tail list)" you are killing your function. "If all the elements are different in this list, return FALSE", and that isn't what you want.
EDIT: Yeah, it will #Luis. I fixed that by putting that "otherwise" there. When I put the guard before the allDifferent2(tail list) it checked if this function returned True. Thus it would work for [1, 1, 2] (my test-case) but not for [1, 2, 2] (similar to your case).
Sort the list, group runs of equal elements together, and check if all groups have exactly one element.
import Data.List (group, sort)
pairwiseDistinct :: Ord a => [a] -> Bool
pairwiseDistinct xs = all (\ys -> null (tail ys)) (group (sort xs))
Point-free version:
pairwiseDistinct = all (null . tail) . group . sort
This assumes that for any two elements x and y, x == y if and only if compare x y == EQ.
tail is fine here because none of the groups will ever be empty, but you can substitute drop 1 if you're averse to partial functions.
allDifferent [] = True
allDifferent (h:t) =
let (e,(l,r)) = segment h t
in e && allDifferent l && allDifferent r
segment p [] = (True,([],[])))
segment p (h:s)
| p > h = let (e,(l,r)) = segment p s in (e,(l,h:r))
| p < h = let (e,(l,r)) = segment p s in (e,(h:l,r))
| otherwise = (False,([],[])))
As you can see the structure of this solution is very similar to quickSort.
It shares as an intermediate data structure a binary tree and for that reason, the time complexity is extremely similar.
Anyone able to offer any advice for a function in SML that will take 2 lists and return the XOR of them, so that if you have the lists [a,b,c,d], [c,d,e,f] the function returns [a,b,e,f] ?
I have tried to do it with 2 functions, but even that does not work properly.
fun del(nil,L2) = nil
|del(x::xs,L2)=
if (List.find (fn y => y = x) L2) <> (SOME x) then
del(xs, L2) # [x]
else
del(xs, L2);
fun xor(L3,L4) =
rev(del(L3,L4)) # rev(del(L4,L3));
Your attempt seems almost correct, except that fn x => x = x does not make sense, since it always returns true. I think you want fn y => y = x instead.
A couple of other remarks:
You can replace your use of List.find with List.filter which is closer to what you want.
Don't do del(xs,L) # [x] for the recursive step. Appending to the end of the list has a cost linear to the length of the first list, so if you do it in every step, your function will have quadratic runtime. Do x :: del(xs,L) instead, which also allows you to drop the list reversals in the end.
What you call "XOR" here is usually called the symmetric difference, at least for set-like structures.
The simplest way would be to filter out duplicates from each list and then concatenate the two resulting lists. Using List.filter you can remove any element that is a member (List.exists) of the other list.
However that is quite inefficient, and the below code is more an example of how not to do it in real life, though it is "functionally" nice to look at :)
fun symDiff a b =
let
fun diff xs ys =
List.filter (fn x => not (List.exists ( fn y => x = y) ys)) xs
val a' = diff a b
val b' = diff b a
in
a' # b'
end
This should be a better solution, that is still kept simple. It uses the SML/NJ specific ListMergeSort module for sorting the combined list a # b.
fun symDiff1 a b =
let
val ab' = ListMergeSort.sort op> (a # b)
(* Remove elements if they occur more than once. Flag indicates whether x
should be removed when no further matches are found *)
fun symDif' (x :: y :: xs) flag =
(case (x = y, flag) of
(* Element is not flagged for removal, so keep it *)
(false, false) => x :: symDif' (y :: xs) false
(* Reset the flag and remove x as it was marked for removal *)
| (false, true) => symDif' (y::xs) false
(* Remove y and flag x for removal if it wasn't already *)
| (true, _) => symDif' (x::xs) true)
| symDif' xs _ = xs
in
symDif' ab' false
end
However this is still kind of stupid. As the sorting function goes through all elements in the combined list, and thus it also ought to be the one that is "responsible" for removing duplicates.
I need a program that checks if the difference between all pairs of elements is in the interval from -2 up to 2 ( >= -2 && < 2). If it is, then return True, else return False. Foe example, [1,2,3] is True, but [1,3,4] is False.
I am using the all function. What is wrong with my if clause?
allfunc (x : xs)
= if all (...) xs
then allfunc xs
else [x] ++ allfunc xs
allfunc _
= []
Or I am doing something completely wrong?
For this, it's probably easier to use list comprehensions or do-notation.
pairsOf lst = do
x <- lst
y <- lst
return (x, y)
pairsOf returns the list of pairs of numbers in the input lst. For example, pairsOf [1,2,3] results in [(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)].
Now, you can define the difference between a pair in a one-liner \(x, y) -> x - y and map that over the list:
differences lst = map (\(x, y) -> x - y) (pairsOf lst)
Now you just have to make sure that each element in differences lst is between -2 and 2.
Of course, this is just one possible way to do it. There are many other ways as well.
The naive way to do what you describe is:
allfunc xs = all (<=2) [abs(a-b) | a <- xs, b <- xs ]
However, a more efficient method would be to compare the minimum and maximum of the list:
fastfunc [] = true
fastfunc xs = maximum xs - minimum xs <= 2
Why not simply...
allfunc xs = (maximum xs - minimum xs) <= 2
Or if you really want to investigate every pair, you can use monads:
import Control.Monad
allfunc xs = all ((<=2).abs) $ liftM2 (-) xs xs
liftA2 from Control.Applicative would do as well.
Well, the problem specification isn't very clear.
You say:
the diffence between all elements is in interval from -2 till 2 ( >= -2 && < 2)
But also:
Foe example, [1,2,3] is True, but [1,3,4] is False
How is it True for [1,2,3]?
Assuming you mean -2 <= diff <= 2, then I would use this:
allfunc :: (Ord a, Num a) => [a] -> Bool
allfunc theList = all (\x -> (x >= -2) && (x<2)) [x-y | x <- theList, y <- theList ]
allfunc [1,2,3] -- => True
allfunc [1,3,4] -- => False
Basically, yes you're doing something wrong. all is meant to take a predicate and a list of values to test. So it will return True if and only if all values yield true when applied to the given predicate function. I.e.:
allValuesEven = all even
allValuesOdd = all odd
I'm trying to write a function that takes in a list and returns true if it is in sorted order and false if not:
So far what I have is:
myordered [] = True
myordered [x] = True
myordered list1
| (head list1) <= (head (tail list1)) = myordered(tail list1)
| otherwise = False
Based on our assignment, all head and tail operations should be written down as "x:xs" type syntax.
the translation I come up with for the section with a guard is:
myordered y:x:xs
| (y) <= (x) = myordered(xs)
| otherwise = False
Essentially this question boils down to:
How do you express the (head (tail list1)) in "x:xs" type syntax?
Cheers,
-Zigu
Your pattern is almost correct, you just need to surround it with parentheses:
myordered (y:x:xs)
Also note that there's no need to surround y and x with parentheses in y <= x.
Also there's a semantic mistake in your second version:
myordered(xs) here xs refers to the tail of tail, but you want the whole tail, so you should do myordered (x:xs) or alternatively:
myordered (y:xs#(x:_))
| y <= x = myordered xs
| otherwise = False
Which says: xs is the tail of that list, x is the head of that tail, and _ (which is ignored) is the tail of the tail.
How about another way to do this with the help of zipWith function available in Data.List
myordered xs= and $ zipWith (<=) xs (tail xs)
zipWith function takes two list and apply a function. Here it will return an array of boolean according to the condition .
and takes a list of boolean values and returns True only if all the values in the list are True
How about
isordered :: [Int] → Bool
isordered [] = True
isordered xs = foldl (&&) True $ zipWith (<=) xs $ tail xs
Oh, and just for fun:
isordered :: [Int] → Bool
isordered [] = True
isordered (x:xs) = (foldl comp (Just x) xs) /= Nothing
where comp (Just a) b = if a ≤ b then Just b else Nothing
comp Nothing _ = Nothing