I have some haskell code Im trying to work my way thourgh but I dont have understand what is going in it.
type Bag a = a -> Int
emptyB :: Bag a
emptyB = \e -> 0
countB :: Eq a => Bag a -> a -> Int
countB b e = b e
I understand that the Bag type is a function that takes in a generic object and returns a Int and countB is basically a wrapper for Bag that gets the number of generic objects in that Bag. But I dont really understand anything past that. How do I modify whats in the bag? Or the bag itself? From what I figure adding to the bag would be something like
addB :: Eq a => Bag a -> a -> Bag a
addB bag num = bag (num+bag)
But this returns a int, when the add function requires a bag be returned. Can anyone explain to me how this works?
Terms and Discussion
type Bag a = a -> Int
Here Bag is not an object. It is just a type - an alias for a -> Int. If you have a value of type a it will compute and return a value of type Int. That's it. There is no Bag, no structure to which you can add things. It would be better to not even call this a Bag.
emptyB :: Bag a
emptyB = \e -> 0
A function from any type to the constant number zero.
countB :: Eq a => Bag a -> a -> Int
countB b e = b e
In short, this is just function application. Apply the function named b to the input e.
Rewriting for fun and learning
I appreciate that you can use functions to imitate structures - it's a common programming language class assignment. You can take a Bag a and another Bag a then union them, such as returning a new countB by adding the counts of the two individual bags - cool.
... but this seems too much. Before moving on with your assignment (did I guess that right?) you should probably become slightly more comfortable with the basics.
It might be easier if you rewrite the functions without the type alias:
emptyB :: a -> Int
emptyB = \e -> 0
-- or: emptyB e = 0
-- or: emptyB _ = 0
-- or: emptyB = const 0
Bag or no bag, it's just a function.
countB :: Eq a => (a -> Int) -> a -> Int
countB b e = b e
A function that takes an a and produces an Int can... be given a value (the variable e is of type a) and produce an Int.
Related
As the questions states, I am having a little trouble with defining a function with parameters different types. (array of Num, two parameters Int and returning Int).
This is function title:
_sum_divide :: (Num a) => [a] -> (Int b) => b -> b -> b
And I get this error I cannot figure out
`Int' is applied to too many type arguments
In the type signature for `_sum_divide':
_sum_divide :: Num a => [a] -> Int b => b -> b -> b
Sorry for the silly error, I am a noob with Haskell.
Thanks, have a good day/evening.
This seems to be a basic confusion between the concepts of type classes and of types. OO languages throw these all together, but in Haskell they are fundamentally different things.
A type is a set† of values. For example, the type Bool contains the values False and True. The type Int contains the values 0, 1 ... 9223372036854775807 and their negatives.
A type class is a set of types. For example, the class Num contains the type Int, the type Double, the type Rational... and whatever type T of your own, if you just define an instance Num T.
Generally, types are used in function signatures just by naming them. For instance,
foo :: [Int] -> [Int]
foo = map (*3)
is a function accepting a list of Int numbers (i.e. values of type Int), and gives another such list as the result (wherein each entry is tripled).
There is no constraint at all in the signature of foo. I could actually add one, like
foo :: Num Int => [Int] -> [Int]
This would express that the function needs Int to be an instance of the Num class to work. Well, it does need that in order to be able to calculate *3, but the constraint is superfluous, because we know that Int is a Num instance, the compiler doesn't just forget about that.
Where constraints are really useful is in polymorphic functions. For example, the following function triples every entry in a list of numbers, and doesn't care what particular type the numbers have:
foo :: Num n => [n] -> [n]
foo = map (*3)
This notation with type variables like a is actually shorthand for
foo :: ∀ n . Num n => [n] -> [n]
meaning, for all numerical types n, the function foo maps lists of n-values to lists of n-values.
It's important that constraints are completely separate‡ from the actual type specification in a signature. For instance, to specify for a polymorphic function [a] -> b -> b -> b that a should be a Num instance and b an Integral instance (the class of whole-number types, containing amongst other Int), you'd write
sum_divide :: (Num a, Integral b) => [a] -> b -> b -> b
Alternatively, if you really mean Int – that's just a single type, no reason to introduce a type variable for it.
sum_divide :: Num a => [a] -> Int -> Int -> Int
...although, you can still introduce the b variable if you like. You'll need an equational constraint (those are basically ad-hoc type classes containing only a single type)
{-# LANGUAGE TypeFamilies #-}
sum_divide :: (Num a, b ~ Int) => [a] -> b -> b -> b
†Mathematicians would object about several levels of differences between types, sets and classes. Read my notion of “set” as just “collection of things”.
‡In more complicated settings you can actually mix constraints and types, but that's advanced stuff.
The signature for a function that takes list of Nums and 2 int, and returns an int is:
_sum_divide :: (Num a) => [a] -> Int -> Int -> Int
The part before the thick arrow specifies the constraints. The part after it is where you use the constraints.
You had 2 main issues:
There should only 1 list of constraints in a signature.
Int isn't a Typeclass. (Num a) means a can be any type that supports the Num Typeclass. Int however is a concrete type, not a Typeclass, so (Int b) doesn't make sense.
I want to be able to make operators such as found in VB(.net) like shown below
Console.WriteLine ( 16 Mod 2 )
produces an output shown below
0
As you can see, this makes code slightly easier to read. How would I go about making functions that do this?
I have tried the following format
equal :: Integer -> Integer -> bool
x `equal` y = x == y
and I get the following error
*Main> 5 equal 5
<interactive>:1:1: error:
* Non type-variable argument
in the constraint: Num ((Integer -> Integer -> Bool) -> t -> t1)
(Use FlexibleContexts to permit this)
* When checking the inferred type
it :: forall t t1.
(Num ((Integer -> Integer -> Bool) -> t -> t1), Num t) =>
t1
*Main>
What is going wrong and how do I do it correctly?
You need to use backticks around equal when you call it, exactly as you did to define it.
5 `equal` 5
The way you wrote it, Haskell thinks you're trying to pass equal as an argument to 5, rather than the other way around.
First off, I would like to make you all aware that I'm very new to Haskell, so to increase knowledge etc, I've been trying out questions and I'm pretty stuck on one. I think I'm nearly there but some more experienced advice would be appreciated. Here's the question:
A sporting team is represented by it's name and the amount of points they scored in their last games like so ("Newcastle",[3,3,3,0]). This data is modelled by the type definitions:
type TName = String
type Points = [Int]
type Team = (TName,Points)
From this I have to define the following function that orders one team higher than another if their points sum is greater:
sortPoints :: [Team] -> [Team]
This is what I've tried:
sortPoints :: [Team] -> [Team]
sortPoints [_,()] -> []
sortPoints [_,(x:xs)] = sum[x|x<-xs]
Once I get to here, I'm not too sure how to go about adding the conditions to check the points sum, any pointers would be greatly appreciated as I'm still coming to terms with a lot of Haskell features.
Note: This post is written in literate Haskell. You can save it as Team.lhs and try it. That being said, it's basically a longer version of Carsten's comment. If you still try to figure things out, use hoogle and look for the functions, although it's fine if you manage things with sortBy solely first.
First of all, we're going to work on lists, so you want to import Data.List.
> module Team where
> import Data.List
It contains a function called sortBy:
sortBy :: (a -> a -> Ordering) -> [a] -> [a]
The first argument of sortBy should be a function that compares two elements of your list and returns
LT if the first is less than the second,
EQ if the both are equal,
GT if the first is greater than the second.
So we need something that that takes two teams and returns their ordering:
> -- Repeating your types for completeness
> type TName = String
> type Points = [Int]
> type Team = (TName, Points)
>
> compareTeams :: Team -> Team -> Ordering
Now, you want to compare teams based on their sum of their points. You don't need their name, so you can capture just the second part of the pair:
> compareTeams (_, s1) (_, s2) =
We need the sum of the points, so we define sum1 and sum2 to be the respective sums of the teams:
> let sum1 = sum s1
> sum2 = sum s2
Now we can compare those sums:
in if sum1 < sum2
then LT
else if sum1 == sum2
then EQ
else GT
However, that's rather verbose, and there's already a function that has type Ord a => a -> a -> Ordering. It's called compare and part of the Prelude:
> in sum1 `compare` sum2
That's a lot more concise. Now we can define sortTeams easily:
> sortTeams :: [Team] -> [Team]
> sortTeams = sortBy compareTeams
And that's it, we're done!
Fine, I lied, we're not 100% done. The module Data.Ord contains a function called comparing that's rather handy:
comparing :: Ord b => (a -> b) -> a -> a -> Ordering
comparing f x y = f x `compare` f y -- or similar
Together with snd and sum you can define sortTeams in a single line:
sortTeams = sortBy (comparing $ sum . snd)
The alternative on mentioned by Carsten is on from Data.Function:
sortTeams = sortBy (compare `on` sum . snd)
In OCaml, I have a list of strings that contains names of towns (Something like "1-New York; 2-London; 3-Paris"). I need to ask the user to type a number (if they want London they have to type 2).
I want to raise an exception message saying that the town is not valid, if the person types for example "4", in the example.
I tried this, but it doesn't work :
let chosenTown = match int_of_string (input_line stdin) with
| x > (length listOfTowns) -> raise (Err "Not a valid town")
What's the good way to code "if the chosen number is bigger than the length of the list then raise the error" ??
Pattern can't contain arbitrary expressions. It can be a constant, a constructor name, record field inside curly braces, list, array, etc.
But patterns can be guarded, e.g.
match int_of_string (input_line stding) with
| x when x >= length listOfTowns ->
invalid_arg "the number is too large"
| x -> List.nth listOfTowns x
To complete the answer, patter matching relies on unification and does not expect assertion (it is not the equivalent of a switch in C or so).
The idea is that you provide different "shapes" (patterns) that your term (the thing you match on) could have.
For a list for instance:
match l with
| e :: e' :: r -> (*...*)
| e :: r -> (*...*)
| [] -> (*...*)
It also had a binding effect, if you pass on, say, [1] (a very small list indeed), it won't match e :: e' :: r, but will match e :: r and then e = 1 and r = [].
As ivg said, you can add conditions, as booleans this time, thanks to the keyword when.
However, when manipulating lists like this, I would go for a recursive function:
let rec find_town n l =
match l with
| t :: _ when n = 1 -> t
| _ :: r -> find_town (n-1) r
| [] -> raise (Err "Not a valid town")
This is basically writing again List.nth but changing the exception that it raises.
I'm reviewing an old exam in my Haskell programming course and I can't seem to wrap my head around this function (I think there is just too little information given).
The code given is
myId x = x
function n f
| n > 0 = f . function (n-1) f
| otherwise = myId
I know that if I for example call the function with the input 2 (*2), I will get a function as result. And if I call it with (-2) (*2) 1 I will get the result 1.
I just don't know how? Also I can't wrap my head around the typecast of the function.
I know that these two options are correct but I don't understand why (probably parentheses that confuse me at the moment).
function :: (Num a, Ord a) => a -> (a -> a) -> a -> a
function :: (Num a, Ord b) => a -> (b -> b) -> b -> b
Anyone that can clarify how I should "read" this function and how I should understand how the typecast works (been reading my Programming in Haskell literature and from Learn You a Haskell but been going in circle for a few days now).
function takes some number n and a function f :: a -> a, and composes that function with itself n times, returning another function of type a -> a. When the returned function is applied to a value of type a, the result is essentially the same as executing f in a loop n times, using the output of each previous step as the input for the next.
Perhaps it is easier to see the similarity if the final parameter is made explicit:
function :: (Ord a, Num a) -> a -> (b -> b) -> b -> b
function n f x
| n > 0 = f (function (n-1) f x)
| otherwise = x
This is functionally equivalent to your point-free function.
In Haskell, a function f :: a -> b -> c can be interpreted either as "a function that takes an a and a b and returns a c" or "a function that takes an a and returns a function from b to c". When you apply a function to one or more inputs, think of each input as eliminating one of the function's arguments. In this instance, function 10 returns a new function with type (a -> a) -> a -> a, and function 2 (*2) returns a function with type Num a => a -> a.
When you think of it this way, it should hopefully be clear why function (-2) (*2) 1 returns a number while function 2 (*2) returns a function. There is no type casting going on; when you are applying the three argument function to two inputs, you get another function back instead of a value, since you didn't provide the final input needed to compute that value.