Error "No instance for (Num [t])" in Collatz function - haskell

I am new to Haskell, and programming in general. I am trying to define a function which generates the sequence of Collatz numbers from n. I have:
collatz n = (collatz' n) : 1
where collatz' n = (takeWhile (>1) (collatz'' n))
where collatz'' n = n : collatz'' (collatz''' n)
where collatz''' 1 = 1
collatz''' n = if (even n) then (div n 2) else ((3*2)+1)
When I run this in GHCi, I get the error:
No instance for (Num [t])
arising from the literal `2' at <interactive>:1:7
Possible fix: add an instance declaration for (Num [t])
I don't know what this means. The problem seems to be appending "1" to the list. This problem emerges because
collatz' n = (takeWhile (>0) (collatz'' n))
generates an infinite sequence of "1"s following the correct Collatz sequence; however,
collatz' n = (takeWhile (>1) (collatz'' n))
generates all Collatz numbers from n except "1". What am I doing wrong?

(:) :: a -> [a] -> [a]
Your first line collatz n = (collatz' n) : 1 forces 1 to become [a].
I guess you wanted something like (collatz' n) ++ [1]
And you have error in if (even n) then (div n 2) else ((3*2)+1) there should be ((3*n)+1 or something like that else you have collatz''' 7 = 7

ony's answer is correct, but since you're new to Haskell, maybe this is a clearer explanation. The : operator prepends a value to a list, so doing somelist : 7 is invalid since that's trying to append a value to a list. That's why (collatz' n) : 1 doesn't compile, since the type of (collatz' n) is a list of numbers.
Try replacing the : 1 with ++ [1].

Another way to go at the problem may be for you to use a Data.Sequence structure instead of a list. Sequences allow you to "snoc" a value (put a value on the back of a sequence) as well as the more usual "cons" (put it on the front of the sequence).
Another solution for you may be to use span to make your own "takeUntil" function.
Let me explain: span p xs gives you the same answer as (takeWhile p xs, dropWhile p xs) for whichever p function and xs list you'd use, the same way that splitAt n xs is the same as (take n xs, drop n xs).
Anyway, you can use span to make your own "takeUntil" function:
takeUntil p xs = taken ++ take 1 dropped where
(taken, dropped) = span p xs
This is the form that you were looking for, when you used the collatz n = (collatz' n) : 1 form.
I hope this helps.

Related

Haskell:Check if first element in a list is a duplicate

I am writing a recursive function called threeN that takes a list with one element and adds a new number depending on if it's even or odd. If it's even divide it by 2, and if it's odd multiply it by three and subtract 1. I'm having trouble with my base case which will check the list to see if an element is already contained in the list.
It should be like this:
prelude> threeN [9]
[14,5,10,20,7,14,28,56,19,38,13,26,9]
But Instead I get this:
prelude> threeN [9]
[9]
This is my code so far:
threeN :: [Integer] -> [Integer]
threeN [] = []
threeN [n]
| n `elem` [n] = [n]
| n `mod` 2 ==0 = threeN[n `div` 2] ++ [n]
| n `mod` 2 ==1 = threeN[(3*n)-1] ++ [n]
This is supposed to be done with basic functions, so I can't use very advanced ones to solve the issue, which is why I'm having trouble.
Even if you can only use basic functions, it still helps if you can decompose the problem into smaller problems. Based on the OP and the comments thread, it seems that these three requirements must be fulfilled:
Calculate a number based on a previous number
Generate a list where the next element is based on the previous element
Stop generating when a duplicate is encountered
I'd suggest solving each of these problems independently of each other, and then combining the solutions.
Calculate the next number
At first glance, this seems to be the heart of the problem. This is where you need to detect whether the number is even or odd, and calculate a result accordingly. Implement this as a function that you could call, say, calcNext. It needs to have a type like this:
Prelude> :t calcNext
calcNext :: Integral a => a -> a
With it, you can calculate the next number based on any input:
Prelude> calcNext 9
26
Prelude> calcNext 26
13
Prelude> calcNext 13
38
Prelude> calcNext 38
19
I'll leave the implementation of calcNext as an exercise.
Generate a list
Sometimes, it can be helpful to look for existing functions. Perhaps there's already a function that can be used to generate a list from a function like calcNext.
One way to look for such functions is to use Hoogle.
Making a guess and typing (a -> a) -> [a] into the search field on Hoogle yields a number of results, among which is iterate, which has the similar (but not identical) type of (a -> a) -> a -> [a]. That seems to be close enough, because it enables you to kick off list generation like this:
Prelude> take 10 $ iterate calcNext 9
[9,26,13,38,19,56,28,14,7,20]
Here I chose to take 10 because iterate will go on forever.
The order here is reversed compared to the OP, but I'll leave it as another exercise to reverse a (finite) list, if the order matters.
If you want to base a solution entirely on functions you wrote yourself, you can try to implement your own iterate function. If you need a hint, then you can always peek at the source code of the built-in iterate function - it's open source.
Stop on duplicate
This one is probably the trickiest part, since you need to 'remember' all the previous elements. What you can do is to create a function that not only calculates the next element, but also keeps a list of all previous elements around. You could write a wrapper function around calcNext that has a type like this:
Prelude> :t calcNextAcc
calcNextAcc :: Integral a => (a, [a]) -> (a, [a])
Since this function still returns the same type as its input, you can still use it with iterate:
Prelude> take 15 $ iterate calcNextAcc (9, [])
[(9,[]),(26,[9]),(13,[26,9]),(38,[13,26,9]),(19,[38,13,26,9]),(56,[19,38,13,26,9]),
(28,[56,19,38,13,26,9]),(14,[28,56,19,38,13,26,9]),(7,[14,28,56,19,38,13,26,9]),
(20,[7,14,28,56,19,38,13,26,9]),(10,[20,7,14,28,56,19,38,13,26,9]),
(5,[10,20,7,14,28,56,19,38,13,26,9]),(14,[5,10,20,7,14,28,56,19,38,13,26,9]),
(7,[14,5,10,20,7,14,28,56,19,38,13,26,9]),(20,[7,14,5,10,20,7,14,28,56,19,38,13,26,9])]
Notice how the second element of each tuple contains all elements calculated so far. Now you just need to go look for duplicates.
You could, for example, write a function called containsDuplicates with this type:
Prelude> :t containsDuplicates
containsDuplicates :: Eq a => [a] -> Bool
With that, you can use a function like the built-in takeWhile to iterate until you find a duplicate:
Prelude> takeWhile (not . containsDuplicates . snd) $ iterate calcNextAcc (9, [])
[(9,[]),(26,[9]),(13,[26,9]),(38,[13,26,9]),(19,[38,13,26,9]),(56,[19,38,13,26,9]),
(28,[56,19,38,13,26,9]),(14,[28,56,19,38,13,26,9]),(7,[14,28,56,19,38,13,26,9]),
(20,[7,14,28,56,19,38,13,26,9]),(10,[20,7,14,28,56,19,38,13,26,9]),
(5,[10,20,7,14,28,56,19,38,13,26,9]),(14,[5,10,20,7,14,28,56,19,38,13,26,9])]
The last element there contains your solution:
Prelude> last $ takeWhile (not . containsDuplicates . snd) $ iterate calcNextAcc (9, [])
(14,[5,10,20,7,14,28,56,19,38,13,26,9])
You can easily turn that tuple into a list:
Prelude> uncurry (:) $ last $ takeWhile (not . containsDuplicates . snd) $ iterate calcNextAcc (9, [])
[14,5,10,20,7,14,28,56,19,38,13,26,9]
I've left the implementation of the various functions as an exercise.
threeN consumes a list: xs, starting with a single element in it. It produces a new element: x' based on the value of the head: x of xs; it prepends x' to xs if x' hasn't occurred in xs.
threeN :: [Int] -> [Int]
threeN [] = []
threeN l#(x:xs)
| x' `elem` l = l -- base case
| otherwise = threeN (x':l)
where
x' = if even x then x `div` 2 else x * 3 - 1
Why the signature is [Integer] -> [Integer]? the input is actually just a number. The following code works.
threeN :: Integer -> [Integer]
threeN n = threeN' n []
where threeN' n acc
| n `elem` acc = n:acc
| even n = threeN' (n `div` 2) (n:acc)
| odd n = threeN' (3 * n - 1) (n:acc)
If you are force to use [Integer] as input signature:
threeN :: [Integer] -> [Integer]
threeN [n] = threeN' n []
where threeN' n acc
| n `elem` acc = n:acc
| even n = threeN' (n `div` 2) (n:acc)
| odd n = threeN' (3 * n - 1) (n:acc)
But I think It does not make sense.
Regards!
You could use head and tail with elem to test whether the first element already exists in the list. Note however that head and tail are unsafe functions. They will crash if given an empty list.
threeN :: [Integer] -> [Integer]
threeN ns | n `elem` tail ns = ns
| even n = threeN ([n `div` 2]++ns)
| odd n = threeN ([3*n-1]++ns)
where
n = head ns
Also if you do not want the repeat number to be in the output, then have the first guard just equal tail ns instead of ns. There is probably a more efficient algorithm to generate these lists, but this just modifies what you've provided.

Every n-th element of a list in the form of a list

I went through a post for this problem but I do not understand it. Could someone please explain it?
Q: Find every n-th element of the list in the form of a list start from the n-th element itself.
everyNth :: Int -> [t] -> [t]
everyNth elt = map snd . filter (\(lst,y) -> (mod lst elt) == 0) . zip [1..]
Also, please explain how pattern matching can be used for this problem. That is using
[]->[]
It's easy to use pattern matching to 'select every nth element' for particular cases of n:
every2nd (first:second:rest) = second : every2nd rest
every2nd _ = []
-- >>> every2nd [1..12]
-- [2,4,6,8,10,12]
every3rd (first:second:third:rest) = third : every3rd rest
every3rd _ = []
-- >>> every3rd [1..13]
-- [3,6,9,12]
every4th (first:second:third:fourth:rest) = fourth : every4th rest
every4th _ = []
-- >>> every4th [1..12]
-- [4,8,12]
For the general case, though, we're out of luck, at least with that particular approach. Patterns like those above will need some definite length to be definite patterns. The composed function you mention starts from the thought that we do know how to find every nth member of [1..], namely if it's a multiple of n
multiple n m = m `mod` n == 0
-- >>> filter (multiple 3) [1..12]
-- [3,6,9,12]
So the solution you are trying to understand zips [1..] with the list
index xs = zip [1..] xs
-- >>> index [1..5]
-- [(1,1),(2,2),(3,3),(4,4),(5,5)]
-- >>> index "hello"
-- [(1,'h'),(2,'e'),(3,'l'),(4,'l'),(5,'o')]
Then it filters out just those pairs whose first element is a multiple of n
every_nth_with_index n xs = filter (\(m,a) -> multiple n m) (index xs)
-- >>> every_nth_with_index 3 [1..12]
-- [(3,3),(6,6),(9,9),(12,12)]
-- >>> every_nth_with_index 3 "stackoverflow.com"
-- [(3,'a'),(6,'o'),(9,'r'),(12,'o'),(15,'c')]
Then it gets rid of the ancillary construction, leaving us with just the second element of each pair:
every_nth n xs = map snd (every_nth_with_index n xs)
-- >>> every_nth 3 [1..12]
-- [3,6,9,12]
-- >>> every_nth 3 "stackoverflow.com"
-- "aoroc"
Retracinging our steps we see that this is the same as
everyNth elt = map snd . filter (\(lst,y) -> (mod lst elt) == 0) . zip [1..]
The notorious fold fan strikes again.
everyNth n xs = foldr go (`seq` []) xs n where
go x r 0 = x : r (n - 1)
go _ r k = r (k - 1)
This is very similar to chepner's approach but it integrates the dropping into the recursion. Rewritten without the fold, it's pure pattern matching:
everyNth n = go n where
go k [] = k `seq` []
go 0 (x : xs) = x : go (n - 1) xs
go k (_ : xs) = go (k - 1) xs
With a little cheating, you can define everyNth using pattern matching. Really, we're abstracting out the part that makes pattern matching difficult, as pointed out in Michael's answer.
everyNth n lst = e (shorten lst)
where shorten = drop (n-1) -- here's the cheat
e [] = []
e (first:rest) = first : e (shorten rest)
If you have never seen Haskell before then this takes a bit of explaining.
everyNth :: Int -> [t] -> [t]
everyNth elt = map snd . filter (\(lst,y) -> (mod lst elt) == 0) . zip [1..]
First, note that the type has two arguments, but the definition has only one. This is because the value returned by everyNth is in fact another function. elt is the Int, and the expression in the second line creates a new function that does the job.
Second, note the "." operators. This is an operator that joins two functions together. It is defined like this:
(f . g) x = f (g x)
Here is an equivalent version of the definition with the second argument made explicit:
everyNth elt xs = map snd (filter (\(lst y) -> (mod lst elt) == 0) (zip xs))
When you see a bunch of functions in a chain linked by "." operators you need to read it from right to left. In my second version pay attention to the bracket nesting. zip [1..] xs is the inner-most expression, so it gets evaluated first. It turns a list like ["foo", "bar"] into [(1, "foo"),(2, "bar")]. Then this is filtered to find entries where the number is a multiple of elt. Finally the map snd strips the numbers back out to return just the required entries.

Haskell: Double every 2nd element in list

I just started using Haskell and wanted to write a function that, given a list, returns a list in which every 2nd element has been doubled.
So far I've come up with this:
double_2nd :: [Int] -> [Int]
double_2nd [] = []
double_2nd (x:xs) = x : (2 * head xs) : double_2nd (tail xs)
Which works but I was wondering how you guys would write that function. Is there a more common/better way or does this look about right?
That's not bad, modulo the fixes suggested. Once you get more familiar with the base library you'll likely avoid explicit recursion in favor of some higher level functions, for example, you could create a list of functions where every other one is *2 and apply (zip) that list of functions to your list of numbers:
double = zipWith ($) (cycle [id,(*2)])
You can avoid "empty list" exceptions with some smart pattern matching.
double2nd (x:y:xs) = x : 2 * y : double2nd xs
double2nd a = a
this is simply syntax sugar for the following
double2nd xss = case xss of
x:y:xs -> x : 2 * y : double2nd xs
a -> a
the pattern matching is done in order, so xs will be matched against the pattern x:y:xs first. Then if that fails, the catch-all pattern a will succeed.
A little bit of necromancy, but I think that this method worked out very well for me and want to share:
double2nd n = zipWith (*) n (cycle [1,2])
zipWith takes a function and then applies that function across matching items in two lists (first item to first item, second item to second item, etc). The function is multiplication, and the zipped list is an endless cycle of 1s and 2s. zipWith (and all the zip variants) stops at the end of the shorter list.
Try it on an odd-length list:
Prelude> double_2nd [1]
[1,*** Exception: Prelude.head: empty list
And you can see the problem with your code. The 'head' and 'tail' are never a good idea.
For odd-lists or double_2nd [x] you can always add
double_2nd (x:xs) | length xs == 0 = [x]
| otherwise = x : (2 * head xs) : double_2nd (tail xs)
Thanks.
Here's a foldr-based solution.
bar :: Num a => [a] -> [a]
bar xs = foldr (\ x r f g -> f x (r g f))
(\ _ _ -> [])
xs
(:)
((:) . (*2))
Testing:
> bar [1..9]
[1,4,3,8,5,12,7,16,9]

How to remove an element from a list in Haskell?

The function I'm trying to write should remove the element at the given index from the given list of any type.
Here is what I have already done:
delAtIdx :: [x] -> Int -> [x]
delAtIdx x y = let g = take y x
in let h = reverse x
in let b = take (((length x) - y) - 1) h
in let j = g ++ (reverse b)
in j
Is this correct? Could anyone suggest another approach?
It's much simpler to define it in terms of splitAt, which splits a list before a given index. Then, you just need to remove the first element from the second part and glue them back together.
reverse and concatenation are things to avoid if you can in haskell. It looks like it would work to me, but I am not entirely sure about that.
However, to answer the "real" question: Yes there is another (easier) way. Basically, you should look in the same direction as you always do when working in haskell: recursion. See if you can make a recursive version of this function.
Super easy(I think):
removeIndex [] 0 = error "Cannot remove from empty array"
removeIndex xs n = fst notGlued ++ snd notGlued
where notGlued = (take (n-1) xs, drop n xs)
I'm a total Haskell noob, so if this is wrong, please explain why.
I figured this out by reading the definition of splitAt. According to Hoogle, "It is equivalent to (take n xs, drop n xs)". This made me think that if we just didn't take one extra number, then it would be basically removed if we rejoined it.
Here is the article I referenced Hoogle link
Here's a test of it running:
*Main> removeIndex [0..10] 4
[0,1,2,4,5,6,7,8,9,10]
deleteAt :: Int -> [a] -> [a]
deleteAt 0 (x:xs) = xs
deleteAt n (x:xs) | n >= 0 = x : (deleteAt (n-1) xs)
deleteAt _ _ = error "index out of range"
Here is my solution:
removeAt xs n | null xs = []
removeAt (x:xs) n | n == 0 = removeAt xs (n-1)
| otherwise = x : removeAt xs (n-1)
remove_temp num l i | elem num (take i l) == True = i
| otherwise = remove_temp num l (i+1)
remove num l = (take (index-1) l) ++ (drop index l)
where index = remove_temp num l 1
Call 'remove' function with a number and a list as parameters. And you'll get a list without that number as output.
In the above code, remove_temp function returns the index at which the number is present in the list. Then remove function takes out the list before the number and after the number using inbuilt 'take' and 'drop' function of the prelude. And finally, concatenation of these two lists is done which gives a list without the input number.

Setting upper limit to the input set according to the output function

I'm currently stuck on setting upper limits in list comprehensions.
What I'm trying to do is to find all Fibonacci numbers below one million.
For this I had designed a rather simple recursive Fibonacci function
fib :: Int -> Integer
fib n
n == 0 = 0
n == 1 = 1
otherwise = fib (n-1) + fib (n-2)
The thing where I'm stuck on is defining the one million part. What I've got now is:
[ fib x | x <- [0..35], fib x < 1000000 ]
This because I know that the 35th number in the Fibonacci sequence is a high enough number.
However, what I'd like to have is to find that limit via a function and set it that way.
[ fib x | x <- [0..], fib x < 1000000 ]
This does give me the numbers, but it simply doesn't stop. It results in Haskell trying to find Fibonacci numbers below one million further in the sequence, which is rather fruitless.
Could anyone help me out with this? It'd be much appreciated!
The check fib x < 1000000 in the list comprehension filters away the fib x values that are less than 1000000; but the list comprehension has no way of knowing that greater values of x imply greater value of fib x and hence must continue until all x have been checked.
Use takeWhile instead:
takeWhile (< 1000000) [ fib x | x <- [0..35]]
A list comprehension is guaranteed to look at every element of the list. You want takeWhile :: (a -> Bool) -> [a] -> [a]. With it, your list is simply takeWhile (< 1000000) $ map fib [1..]. The takeWhile function simply returns the leading portion of the list which satisfies the given predicate; there's also a similar dropWhile function which drops the leading portion of the list which satisfies the given predicate, as well as span :: (a -> Bool) -> [a] -> ([a], [a]), which is just (takeWhile p xs, dropWhile p xs), and the similar break, which breaks the list in two when the predicate is true (and is equivalent to span (not . p). Thus, for instance:
takeWhile (< 3) [1,2,3,4,5,4,3,2,1] == [1,2]
dropWhile (< 3) [1,2,3,4,5,4,3,2,1] == [3,4,5,4,3,2,1]
span (< 3) [1,2,3,4,5,4,3,2,1] == ([1,2],[3,4,5,4,3,2,1])
break (> 3) [1,2,3,4,5,4,3,2,1] == ([1,2,3],[4,5,4,3,2,1])
It should be mentioned that for such a task the "canonical" (and faster) way is to define the numbers as an infinite stream, e.g.
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
takeWhile (<100) fibs
--[0,1,1,2,3,5,8,13,21,34,55,89]
The recursive definition may look scary (or even "magic") at first, but if you "think lazy", it will make sense.
A "loopy" (and in a sense more "imperative") way to define such an infinite list is:
fibs = map fst $ iterate (\(a,b) -> (b,a+b)) (0,1)
[Edit]
For an efficient direct calculation (without infinite list) you can use matrix multiplication:
fib n = second $ (0,1,1,1) ** n where
p ** 0 = (1,0,0,1)
p ** 1 = p
p ** n | even n = (p `x` p) ** (n `div` 2)
| otherwise = p `x` (p ** (n-1))
(a,b,c,d) `x` (q,r,s,t) = (a*q+b*s, a*r+b*t,c*q+d*s,c*r+d*t)
second (_,f,_,_) = f
(That was really fun to write, but I'm always grateful for suggestions)
The simplest thing I can think of is:
[ fib x | x <- [1..1000000] ]
Since fib n > n for all n > 3.

Resources