parse error on input ‘::’ when making an explicit type definition for a function which accepts multiple arguments - haskell

I am working on a project in Haskell for the first time and I am working on translating over my ADT into the code properly, however when I am writing the explicit type definitions for my functions and I load my code in GHCi I get the following error:
Blockquote parse error on input ‘::’
The line in question is for a function called type which accepts a character and a tuple and returns a tuple as shown below:
type :: validChars -> tuple -> tuple
where validChars is the list of valid characters, the definitions for my lists are shown here if this helps:
tuple = (l, r, b, k)
l = [l | l <- validChars]
m = [m | m <- validChars]
b = [b | b <- validChars]
k = [k | k <- validChars]
validChars = [ chr c | c <-alphanumericChars , c >= 32, c <= 122]
alphanumericChars = [ a | a <- [0..255]]
I checked to make sure it wasn't validChars causing the error by replacing it with the Charstype as shown:
type :: Chars -> tuple -> tuple
But I still get the same error, I am a complete beginner at Haskell so I'm probably missing something important, but I am not sure what that would be exactly; I've looked at answers for similar questions I have been unsuccessful thus far. Any help with this would be appreciated.

type is a keyword in Haskell, so you can't use it as the name of your function.
Furthermore type names start with a capital letter in Haskell and anything starting with a lower case letter in a type is a type variable. So if you define myFunction :: validChars -> tuple -> tuple, that defines a function that takes two arguments of arbitrary types and produces a result of the same type as the second argument. It's the same as myFunction :: a -> b -> b.
If you write myFunction :: Chars -> tuple -> tuple, you get a function whose first argument needs to be of type Chars (which needs to exist) and the second argument is of an arbitrary type that is also the type of the result. Again it's the same as myFunction :: Chars -> a -> a.
Note that for this to work, you'll actually have to have defined a type named Chars somewhere. If you want to take a list of Chars, the type should be [Char] instead.
And if you want the second argument and result to actually be tuples (rather than just a type variable arbitrarily named tuple), you need to specify a tuple type like (a,b,c,d), which would accept arbitrary 4-tuples, or something specific like (Integer, String, String, String), which would accept 4-tuples containing an Integer and three Strings.

Related

What are the Function types

The question is to determine function type of this : second xs = head (tail xs)
I tried everything
:t second gives me:
*Main> :type second
second :: [a] -> a --- is this the function type?
,then I tried :type second; :type "second xs = head (tail xs)".
It still does not work. How to determine Function type using Haskell
As you already know, you can use GHCi to find the type of a Haskell identifier by using the :type command (or its shorter version :t). In this case, GHCi gives you the answer second :: [a] -> a. The :: symbol means 'type-of', so this answer is just GHCi's way of telling you that 'the type of second is [a] -> a'.
But there's still another question here: what does this type mean? Well, let's pull it apart:
Any type of the form x -> y is the type of a function which takes as input one parameter of type x, and returns a value of type y.
In this case, we have a type [a] -> a, so the input type is [a] (i.e. a list of values of type a), and the output type is a (i.e. a single value of type a).
Thus, the statement second :: [a] -> a means that second is a function which takes as input a list of as, and gives as output a single value of the same type a. This ties in with what we know of the function: given a list, it returns a single value from that list.
EDIT: As #chepner pointed out in the comments, it is important to realise that a is a stand-in for any type. The only constraint is that, if the input is a list of as, then - no matter what a is - the return type must also be of type a. (This sort of indeterminate type is called a type variable.)

Haskell Programming Assignment, "Couldn't match expected type ‘Int’ with actual type ‘[a0] -> Int’ "and a few more Errors

The assignment I have: A function numOccurences that takes a value and a list, returning the number of times that value appears in the list. I am learning haskell and am getting frustrated, this is my code for this:
numOccurences:: b -> [a] -> Int
numOccurences n [ls]
|([ls] !! n==True) = (numOccurences(n (tail [ls])))+1
|otherwise = 0
The errors I am getting are as follows:
https://imgur.com/a/0lTBn
A few pointers:
First, in your type signature, using different type variables (i.e. b and a) creates the possibility that you could look for occurrences of a value of one type, in a list with another type, which in this case is not what you want. So instead of two type variables, you just want to use one.
Second, whatever the concrete type of your list is, whether it's [Char], [Int], etc., it needs to be equatable (i.e. it needs to derive the Eq typeclass), so it makes sense to use the class constraint (Eq a) => in your type signature.
Third, since we're traversing a list, let's use pattern matching to safely break off the first element of the list for comparison, and let's also add a base case (i.e. what we do with an empty list), since we're using recursion, and we only want the recursive pattern to match as long as there are elements in our list.
Lastly, try to avoid using indexing (i.e. !!), where you can avoid it, and use pattern matching instead, as it's safer and easier to reason about.
Here's how your modified function might look, based on the above pointers:
numOccurences :: (Eq a) => a -> [a] -> Int
numOccurences _ [] = 0
numOccurences n (x:xs)
| n == x = 1 + numOccurences n xs
| otherwise = numOccurences n xs

Haskell list type conversion

Support I have a list of type [Char], and the values enclosed are values of a different type if I remove their quotations which describe them as characters. e.g. ['2','3','4'] represents a list of integers given we change their type.
I have a similar but more complicated requirement, I need to change a [Char] to [SomeType] where SomeType is some arbitrary type corresponding to the values without the character quotations.
Assuming you have some function foo :: Char -> SomeType, you just need to map this function over your list of Char.
bar :: [Char] -> [SomeType]
bar cs = map foo cs
I hope I get this correctly and there is a way (if the data-constructors are just one-letters too) - you use the auto-deriving for Read:
data X = A | B | Y
deriving (Show, Read)
parse :: String -> [X]
parse = map (read . return)
(the return will just wrap a single character back into a singleton-list making it a String)
example
λ> parse "BAY"
[B,A,Y]

What does the type of "+" mean in Haskell

Prelude> :t (+)
(+) :: (Num a) => a -> a -> a
My lecture slide says that
a -> a -> a
means a function take two parameters and return one, and all of them are the same type. Which two are the parameters and which one is the return value?
Thank you.
There are some levels you have to master here:
level 0
a -> b -> c
is a function taking one a and one b and producing one c
level 1
well there is more to it:
a -> b -> c
which is really
a -> (b -> c)
is a function taking one a and producing another function, that takes a b and produces a c
level 2
f :: (Num a) => a -> a -> a
Adds a constraint to a (here Num - this means that a should be a number - a is an instance of the Num type-class)
So you get a function that takes an a and produces a function that takes another a and returns a a, and a needs to be an instance of Num
so every input to f has to be of the same type of number:
f 1 2 is ok
f 'a' 'b' is not ok
f (1::Int) (2::Int) is ok
f (1::Float) (2::Float) is ok
f (1::Int) (2::Float) is not ok
level 3 (understanding (+))
The last thing you have to understand here is that, (+) is defined as a part of Num so there are different + based on the used types ... and the same is true for the number literals like 0, 1, ... thats why 0 can be a Float or a Int or whatever type that is a instance of Num
The first two are parameters, the last one is the return value.
In fact, due to currying, it can be read like this: the + function (which only accepts numeric values) takes a parameter a and returns a function that takes a parameter of the same type and returns the result of the same type.
Here's a contrived example:
let addTwo = (+) 2 -- the + function takes one argument and returns a function
addTwo 3 -- we can add the second argument here and obtain 5 as returned value
Suppose we have a type like this:
a -> b -> c -> d -> e
The last thing in the sequence is the return type. So this function returns something of type e. Everything else is the argument types. So this function takes 4 arguments, who's types are a, b, c and d.
Lower-case letters denote "type variables" — variables which can stand for any type. (It doesn't have to be a single letter, but it often is.) Anything beginning with an upper-case letter is a specific type, not a variable. (For example, Int is a type, int is a type variable.)
The Num a part means that a stands for any type, but that type must implement the Num type-class. Other common contexts are Eq (defines the == operator), Ord (defines <, >, and so forth) and Show (defines the show function that converts stuff into a string).

Regarding list of tuples and file

I need to take the list of tuples from a file and multiple 2nd a 3rd values of each tuple. For example: [(1,"A", 100,2),(2,"B", 50,3)] . I need to find 100*2=200 and 50* 3=150. I only want to display the final total. That is 350. I am taking the list of tuples from a file. I am getting an error lik this:
- Type error in generator
*** Term : generator c
*** Type : Int
*** Does not match : IO a
Code is given below.
type Code=Int
type Price=Int
type Quantity=Int
type Name=String
type ProductDatabase=(Code,Name,Price,Quantity)
bill=do
b<-cart_list_returner
let c :: [ProductDatabase]
c = b
w<-generator c
let r :: String
r = w
putStrLn r
generator::[ProductDatabase]->Int
generator c=foldl (\a (id,x, y, z) -> a + y*z) 0 c
I just want the program to take the list of tuples in file and produce the total amount. Some one plz help me. thanks in advance
Use the <- binding operator only when you want to run an IO function.
For giving names to results of pure functions, use let.
generator is a pure function --- its result is of type Int, not of type IO Int.
So replace the line
w<-generator c
with
let w = generator c

Resources