Prefix form of unary operator in Haskell
Haskell’s grammar doesn’t allow you to use – like that. Use the subtract function instead: (subtract 3) 2
Haskell’s grammar doesn’t allow you to use – like that. Use the subtract function instead: (subtract 3) 2
To use those definition in GHCi exactly as they were written (i.e. with multiple equations or type signatures in separate lines), you need to use multiline input in GHCi through the :{ and :} delimiters: GHCi> :{ GHCi| foo 0 = print 999 GHCi| foo n = print n GHCi| :} GHCi> foo 0 999 … Read more
If you use a let in each line, each definition will make a new function named myLast, shadowing all the previous definitions. So what you end up with is equivalent to GHCi> let myLast (x:xs) = myLast xs alone. What you probably want is to make a haskell file, say MyLast.hs, containing module MyLast where … Read more
Most of the time, you can rely on type inference to work out a signature for you. In your example, the following is sufficient: Prelude> let addTwo x y = x + y If you really want a definition with a type signature, or your definition spans over multiple lines, you can do this in … Read more
The gist is that the with the polymorphic xs it has a type of the form xs :: Num a => [a] typeclasses under the hood are really just functions, they take an extra argument that GHC automatically fills that contains a record of the typeclasses functions. So you can think of xs having the … Read more
GHCi now has a multiline-input mode, enabled with :set +m. For example, Prelude> :set +m Prelude> let fac 0 = 1 Prelude| fac n = n * fac (n-1) Prelude| Prelude> fac 10 3628800