absolute values in Haskell
Both of them seem to work just fine: Main> myabs 1 1 Main> myabs (-1) 1 Main> abs 1 1 Main> abs (-1) 1
Both of them seem to work just fine: Main> myabs 1 1 Main> myabs (-1) 1 Main> abs 1 1 Main> abs (-1) 1
Did error happen when you type the function type in GHCi? If it is the case, you have to use multiple line input And noted , before mod Alternatively, for better workflow, you can save your code to a file and load in GHCi using :load
The true quicksort has two beautiful aspects: Divide and conquer: break the problem into two smaller problems. Partition the elements in-place. The short Haskell example demonstrates (1), but not (2). How (2) is done may not be obvious if you don’t already know the technique!
Be sure to indent the guards; Haskell has significant whitespace.
The others are right that the problem is the : operator. I would say that your areTheySame function that returns a list is the wrong approach anyway, though. Rather than switch to the ++ operator, a better implementation of that function would be: As you can see, this is a pretty simple implementation. Also, consing … Read more
For people new to functional programming map may be one of the first concepts that they struggle with. map is a function that takes two parameters: a function and a list of elements. The type signature of map is (a -> b) -> [a] -> [b]. The (a -> b) part is the function you pass to map, we will call it f. f takes one value and … Read more
I understand what most of this means apart from Cons. When I try :t Cons and :i Cons in ghci I get a not in scope error. You need to load the Haskell source file with the data declaration before you can have Cons in scope. Or, alternatively, you can enter that data line directly in GHCi. For serious code, it’s easier if you put it in … Read more
In a nutshell, patterns are like defining piecewise functions in math. You can specify different function bodies for different arguments using patterns. When you call a function, the appropriate body is chosen by comparing the actual arguments with the various argument patterns. Read A Gentle Introduction to Haskell for more information. Compare: with the equivalent Haskell: Note … Read more
Your problem is with the (** a) syntactic sugar. The thing is that (elem b) is just the partial application of elem, that is: However when we use back ticks to make elem infix, we get a special syntax for infix operators which works like this: So therefore, while So in the latter case your arguments are in the … Read more