Rakib Hasan
Rakib Hasan

Reputation: 21

No instance for (Num [t0]) arising from the literal `1'

create_subseq size xs = 
  if (length xs) == size
  then [ [ x | x <- s] | s <- xs] 
  else [ [ i | i <- subxs] | subxs <- (take size xs)] ++ create_subseq size (tail xs)

I keep trying to run this code in ghci with the line below however I get the error below

create_subseq 3 [1,2,3,4]

No instance for (Num [t0]) arising from the literal `1'
Possible fix: add an instance declaration for (Num [t0])
In the expression: 1
In the second argument of `create_subseq', namely `[1, 2, 3, 4]'
In the expression: create_subseq 3 [1, 2, 3, 4]

Upvotes: 0

Views: 1117

Answers (2)

Rakib Hasan
Rakib Hasan

Reputation: 21

Here is my code, I starting to figure out haskell. I like it so far, I hope to keep learning.

-- Program will read a file with a 1000 digit number. The goal of the program
-- is to find a sequence within that number that is 13 digits long and has
-- the largest product. This program is not safe to run on 32 bit machines
-- you will run into number overflow and get inaccurate results

import System.Environment
import Data.Char

create_sub_seq :: Int->[Int]->[[Int]]
create_sub_seq n xs = if length xs == n
              then [xs]
              else [[i | i <- take n xs]]++create_sub_seq n (tail xs)

prod :: [Int]->Int
prod xs = foldl (*) 1 xs 

getIntArray :: [Char] -> [Int]
getIntArray xs = [digitToInt i |i <- xs, not(i=='\n')]


max_sub_seq :: ([Int]->Int)->[[Int]]->[Int]
max_sub_seq  f [] = []
max_sub_seq  f (xs:xxs) = if (f b) > (f xs) 
                  then b
                  else xs
                  where b=max_sub_seq f xxs

main = do [f] <- getArgs
      s <- readFile f

--    print( create_sub_seq 4 (getIntArray s))
      print( max_sub_seq prod (create_sub_seq 13 (getIntArray s)) )
      print( prod ( max_sub_seq prod (create_sub_seq 13 (getIntArray s)) ) )  

Upvotes: 0

Ganesh Sittampalam
Ganesh Sittampalam

Reputation: 29110

If you look at the type of create_subseq in ghci, you get this:

*Main> :t create_subseq
create_subseq :: Int -> [[t]] -> [[t]]

In other words the second argument should be a list of lists of something, but you've only passed a list of numbers. ghci then tries to interpret the numbers as lists, but it can't, hence the error message.

If you want to know why create_subseq takes a list of lists, look at the list comprehensions you've written. They each assume that the original argument xs is a list, and that when you look at an element in the list xs you have another list that you can write a nested list comprehension on.

Upvotes: 4

Related Questions