Lambda expression in haskell has different type
I am trying to learn Haskell.
We have very simple functions. As I understood all the following are equivalent.
But I don't understand why f2
and f2'
have different types, and why exactly Integer
.
Also, if I specify the type of f2'
manually, it accepts it and works same as f2
.
f x = x + 1
 f :: Num a => a > a
f' = \x > x + 1
 f' :: Num a => a > a
f2 a b = a + b
 f2 :: Num a => a > a > a
f2' = \a b > a + b
 f2 :: Integer > Integer > Integer
f2'' = \a > \b > a + b
 f2 :: Integer > Integer > Integer
Any ideas?
See also questions close to this topic

Haskell, stuck on understanding type synonyms
From LYAH book, parameterizing type synonyms: I would understand:
type MyName = String
But this example I don't get:
type IntMap v = Map Int v  let alone it can be shortened
'Map' is a function, not a type, right? This got me in loops in the book constantly now. Next to that: Map would require a function and a list to work, correct? If so, and 'v' is the list, what is the 'Int'?

haskel foldl function with multiple parameters
hi i have data type like this:
data Tree = Null  Node Tree String [String] Tree
so a tree essentially has a structure  Node Tree key [vals] Tree
and i have a function
insert :: String > String > Tree > Tree
im trying to use foldl in a function that given a tree, iterate through the tree and for each [vals] insert it into a new tree. im a little confused about using foldl to apply insert to each value in [vals] especially if insert function has multiple parameters.
i understand in foldl, my function to step through would be insert, my initial val would be Null and my list to fold would be [vals]. The thing that is confusing me is that the function insert takes in a (string, string, Tree) and I don't have that tree yet. what i have below only inserts the first value in the list but not the rest and im not sure where to go from here. any tips/clarifications?
also im a little confused on the lambda \y ys, i was following the documentation but would y be the head of the list and ys be the tail?
createTree :: Tree > Tree createTree (Node left key vals right) = let (v:vs) = vals foldl (\y ys > (insert v key (createTree left))) Null (v:vs)

Is there a zipWith analogue for tuples?
Preliminary note: this is a respin of a deleted question by SeanD.
Just like there is
zipWith
for lists...GHCi> zipWith (+) [1,2] [3,4] [4,6]
... it feels like there should be something analogous for tuples, in the spirit of...
tupleZipWith (+) (1,2) (3,4)
... but there doesn't seem to be anything obviously like that in base. Which options do I have?

How to check type of a variable in Java?
I have seen many questions with similar title on StackOverflow but all those answers seem to suggest the
getClass().getSimpleName()
method. The problem is I don't wanna know the type of value that the variable holds, I am interested in the type of variable itself. In the following code, I want to printObject
, notString
:Object ob = "Some String"; System.out.println(ob.getClass().getSimpleName()); // prints "String"
Given that the type of variable would be a super type of the value that it holds, I can check if the value is
instanceof
some class. Butinstanceof
will return true for all the super classes up until we rich Object in the hierarchy. Is there a way to specifically check the type of variable? 
scala polymorphic type signatures with Ordering and ClassTag
I need help understanding this type signature:
def func[A : Ordering : ClassTag](a: A) = ???
I've come up with dummy examples using the type signature above... and also the type signature I'm more familiar with, that I believe is the close to the same thing based on my dummy example, but I can also come up with a toy example where they are clearly not the same.
These two seem similar:
import scala.reflect.ClassTag // type signature in question def func1[A : Ordering : ClassTag](elems: A*) = Array[A](elems: _*).sorted // my typical type signature def func2[A <% Ordered[A]](elems: A*)(implicit c: ClassTag[A]) = Array[A](elems: _*).sorted
Example use:
class BB(val i: Int) extends Ordered[BB] { def compare(that: BB): Int = that.i  i override def toString = s"BB(${i})" } func1(new BB(33), new BB(100), new BB(1)) func2(new BB(33), new BB(100), new BB(1))
Output for each is:
Array[BB] = Array(BB(100), BB(33), BB(1))
The one edge case I can come up with where they differ... indicating one is not simply syntactic sugar for the other... is the following, where the function has an implicit order for a class that differs from the class's natural sort order.
This example (below) works fine, and
implicit val ordering
overrides class BB's natural sort order as I (kind of) expected.def func3[A <% Ordered[A] : ClassTag](elems: A*) = { // opposite order defined in class BB implicit val ordering: Ordering[A] = Ordering.by{ case bb: BB => bb.i } Array[A](elems: _*).sorted }
This version (below) give me an error...
def func3[A : Ordering : ClassTag](elems: A*) = { // opposite order defined in class BB implicit val ordering: Ordering[A] = Ordering.by{ case bb: BB => bb.i } Array[A](elems: _*).sorted }
error: ambiguous implicit values: both value evidence$1 of type Ordering[A] and value ordering of type Ordering[A] match expected type scala.math.Ordering[A]
So based on this... I'm guessing
: Ordering
sort of convertsOrdered[BB]
into animplicit val ordering
... or something like that? Are there deeper differences that my toy examples fail to reveal?Thanks in advance.

Why is this simple indexing variable gettng Boxed in Julia?
In solving Problem 10 from Project Euler manually (instead of simply
using Primes
), I implemented a naive Sieve of Eratosthenes in Julia usingBitVector
. It produces the correct output, but when I checked its type stability using@code_warntype
, I found thati
was being allocated asCore.Box
, which affects the type ofpsum
, and that in turn throws the type stability of the whole function away. This is the code used:function primesum(limit::T = 2_000_000) where T<:Integer #easy way: using Primes; sum(primes(limit)) #hard way: is_prime_num = BitVector(limit) is_prime_num .= true psum = zero(limit) for i in 2:limit if is_prime_num[i] psum += i multiples_of_i = [k*i for k in 2:(limit÷i)] is_prime_num[multiples_of_i] .= false end end psum end
And here is the Variables part of the
code_warntype
output (full output here):Variables: #self#::#primesum limit::Int64 #46::##46#47 i::Core.Box multiples_of_i::Any #temp#@_6::Int64 is_prime_num::BitArray{1} psum::Any J::Any #temp#@_10::Any
Overall, a lot of types in the code (including the function's return type) are left as either
Any
or awhere _
type.I was able to improve speed (almost 10x) and memory (~3x) by changing the
for
loop line tofor i::T in 2:limit
 then,i
still gets set as aCore.Box
, but that getstypeassert
ed to Int64 and doesn't propagate the instability topsum
and others. But I'm more interested in why Julia couldn't inferi
's type than in speeding up this specific code. I'm used to type instabilities making sense at least in retrospect, but this one seems pretty clear and simple to infer, so I'd like to know where there is type ambiguity here. 
haddock fails because "'Main’ is defined in multiple files"
% ghc version; cabal version; haddock version; cabal haddock The Glorious Glasgow Haskell Compilation System, version 8.0.1 cabalinstall version 2.2.0.0 compiled using version 2.2.0.0 of the Cabal library Haddock version 2.17.4, (c) Simon Marlow 2006 Ported to use the GHC API by David Waern 20062008 Preprocessing library for opaleye0.6.1.0.. Running Haddock on library for opaleye0.6.1.0.. Warning: The documentation for the following packages are not installed. No links will be generated to these packages: StateVar1.1.0.4, ... <no location info>: error: module ‘opaleye0.6.1.054np2PrjcDbJYVV9W7uGXD:Main’ is defined in multiple files: src/Opaleye/Operators.hs src/Opaleye/Order.hs
This appears to not be an instance of cabal haddock failing because function is defined in multiple files because I'm not on Mac and
cabal haddock ghcoptions=optPP
does not work. 
How to define multiplication of Natural Numbers in Haskell commutatively?
I was trying to define natural number multiplication in Haskell, and kept getting the error below (corresponds to the second natMult definition below).
Prelude> natMult (S (S Z)) (S (S Z)) *** Exception: <interactive>:(4,5)(5,45): Nonexhaustive patterns in function natPlus
Here's my code:
data Nat = Z  S Nat deriving (Show) natPlus :: Nat > Nat > Nat natPlus Z a = a natPlus (S a) (S b) = natPlus a (S (S b))
After a bit of tinkering, I realized this definition works fine, whereas the second one below is broken. The only difference is the order of the input parameters for natPlus.
 works fine natMult :: Nat > Nat > Nat natMult Z a = Z natMult (S a) b = natPlus (natMult a b) b  gives gives the error above natMult :: Nat > Nat > Nat natMult Z a = Z natMult (S a) b = natPlus b (natMult a b)
Can anyone explain why this error arises?
Edit The below definition is complete and solves the problem. Thanks for the suggestions
natPlus Z a = a natPlus (S a) b = natPlus a (S b)

Defining strict application ($!) myself does not result in the same performance
I'm reading this blog post by Michael Snoyman recently. In an exercise suggested there, I tried to define
$!
operator by myself:import Prelude hiding ( ($!) ) ($!) :: (a > b) > a > b ($!) f x = x `seq` f x mysum :: [Int] > Int mysum list0 = go list0 0 where go [] total = total go (x:xs) total = go xs $! total + x main = print $ mysum [1..1000000]
I thought this works well, though the usage of memory was terrible. My first question is this. Why didn't this work well?
Then, I checked its definition in Prelude. It reads:
($!) :: (a > b) > a > b f $! x = let !vx = x in f vx  see #2273
So, I copied it into my code:
{# LANGUAGE BangPatterns #} import Prelude hiding ( ($!) ) ($!) :: (a > b) > a > b ($!) f x = let !vx = x in f vx mysum :: [Int] > Int mysum list0 = go list0 0 where go [] total = total go (x:xs) total = go xs $! total + x main = print $ mysum [1..1000000]
and the result was:
Linking mysum4 ... 500000500000 209,344,064 bytes allocated in the heap 130,602,696 bytes copied during GC 54,339,936 bytes maximum residency (8 sample(s)) 66,624 bytes maximum slop 80 MB total memory in use (0 MB lost due to fragmentation)
You can see how terrible this is compared to the result of using Prelude's
$!
operator:Linking mysum4 ... 500000500000 152,051,776 bytes allocated in the heap 41,752 bytes copied during GC 44,384 bytes maximum residency (2 sample(s)) 21,152 bytes maximum slop 1 MB total memory in use (0 MB lost due to fragmentation)
My second question is where did this difference come from?
Furthermore, I think it can be rewritten like this:
($!) :: (a > b) > a > b f $! !x = f x
Is there any reason not to do this? This is my third question.