Karim Tarek
Karim Tarek

Reputation: 401

Can't find what's wrong with the types here

i am working on an encryption algorithm and i have a type problem

i don't understand what's the problem with the types here:

multien :: [(Integer , Integer)] -> Integer
multien [] = 1
multien ((x,y):z) = y* multien z

enchelper2 :: [(Integer, Integer)] -> Integer -> Integer

the problem is here in enchelper in the (z:((mod x y),y)) expression:

Couldn't match expected type (Integer , Integer) with actual Type [(Integer, Integer)]

the z in the enchelper method here i passed it as [] (empty set) the ERROR IS IN COLUMN 40 exactly

enchelper :: Integer -> [Integer] -> [(Integer, Integer)] -> [(Integer, Integer)]
enchelper x (y:ys) z = if((enchelper2 (z:((mod x y),y)) (multien z:((mod x y),y))) == x) then z:[] else (z:((mod x y),y):(enchelper x ys z:((mod x y),y)))

Upvotes: 0

Views: 92

Answers (2)

dave4420
dave4420

Reputation: 47042

In this piece of code

z : (mod x y, y)

The types are

x :: Integer
y :: Integer
z :: [(Integer, Integer)]
(mod x y, y) :: (Integer, Integer)

And note that

(:) :: a -> [a] -> [a]

So perhaps you should have instead

(mod x y, y) : z

There are then similar errors later in the line.

Upvotes: 2

Nate
Nate

Reputation: 12819

The problem appears to be in your use of the cons operator. z is a list of tuples of two Integers, so if you really want z before ((mod x y),y), what you want to use instead is the ++ operator, followed by a list.

Try this on for size:

enchelper :: Integer -> [Integer] -> [(Integer, Integer)] -> [(Integer, Integer)]
enchelper x (y:ys) z = if((enchelper2 (z++[((mod x y),y)]) (multien (z++[((mod x y),y)]))) == x) then z else (z++[((mod x y),y)])++(enchelper x ys (z++[((mod x y),y)]))

Or, a bit more readable:

enchelper :: Integer -> [Integer] -> [(Integer, Integer)] -> [(Integer, Integer)]
enchelper x (y:ys) z =
    if ((enchelper2 foo (multien foo)) == x)
    then z
    else foo++(enchelper x ys foo)
    where foo = z++[((mod x y),y)]

Upvotes: 1

Related Questions