I'm pretty new in Haskell and I have got a very strange mistake:
insertion el [] = [el]
insertion el (x:xs) = | el < x = el:x:xs
| otherwise = x:insertion el xs
Which gives me this error, on the second line at the caractere just after the pipe:
parse error on input `|'
Failed, modules loaded: none.
I don't really get it,
would you have a tips ?
Thanks in advance :)
When you use guards (the pipe symbol) with function definitions, you do not follow the function name and paramaters with an equal sign. It should be written like this:
insertion el [] = [el]
insertion el (x:xs)
| el < x = el:x:xs
| otherwise = x:insertion el xs
The first guard doesn't need to be on the next line, but that tends to be the general style.
Related
I have to extract user name and email from Either of AuthResponse.
I use case of construct for it:
let (uname, uemail) =
case getUserResponseJSON creds of
Right (GoogleUserResponse uname uemail) -> (uname, uemail)
_ -> ("", "")
But I have this warning for both uname and uemail:
This binding for ‘uname’ shadows the existing binding
bound at src/Foundation.hs:240:12
|
242 | Right (GoogleUserResponse uname uemail) -> (uname, uemail)
|
I expect that let (uname, uemail) is out of the scope of case of block.
How is it possible to have this warning from the case block if uname and uemail are not yet defined?
Haskell's let is actually letrec.
In let x = x in ..., the x on the right refers to the x on the left.
The scope of x, y and z in
let
x = ...
y = ...
z = ...
in ...
are all the code areas indicated with ....
How is possible to have this warning from the case block if uname and uemail still not defined?
These are defined, in the scope outside the case, two variables named uname and uemail are defined. The Haskell compiler can "tie a knot". Take for example the following expression where we define an endless list of ones:
ones :: [Int]
ones = x
where x = 1 : x
Here we thus define a list that looks like:
+-------+
| (:) |<,
+---+---+ |
| o | o---'
+-|-+---+
v
1
So you can define a variable in terms of itself, like you do here. You do not assign a value to variable, you declare a variable. Furthermore note that the order in which you define variables is not per se the order in which you will perform operations. So it is possible that the (uname, uemail) is never evaluated, etc.
Ignoring the reasons, you are reimplenting Data.Either.fromRight.
import Data.Either (fromRight)
...
let defaultResponse = GoogleUserResponse "" ""
toTuple (GoogleUserResponse x y) = (x,y) -- parameter names aren't important
gResponse = fromRight defaultResponse $ getUserResponseJSON creds
(uname, email) = toTuple gResponse
...
I was dealing with some Haskell function examples But I didn't understand this particular function. Now the function is:
func [n] = n
func (n:ns) = func [n+x|x<-ns]
I know that this is a list comprehension and i also learned that the syntax of the list comprehensions is like this :
[return-expression | elem <- collection, predicate]
but i still couldn't figure how this function works and what does it do ? Can you please explain it?
Also sorry for my English.
[n+x|x<-ns] is the list obtained by adding n to every element of ns. It is equivalent to map (+n) ns.
Hence, we have, e.g. :
func [a,b,c,d] =
func [a+b,a+c,a+d] =
func [a+b+a+c,a+b+a+d] =
func [a+b+a+c+a+b+a+d] =
a+b+a+c+a+b+a+d
I am getting this error:
sky.hs:3:5: error: parse error on input `|'
This is the code:
sky list
| (length list) /= 1 = reverse (last(even (sky list)) : reverse(odd list)
| otherwise = list
Does anyone have any idea why this is happening? I have made sure that there are no tabs, just 4 spaces before the | symbol. This might seem like a novice question but I am a beginner in Haskell.
I'm running GHC version 7.8.3 on Windows 7.
Ok, this is not about fancy code snippets. I'm just trying not be a noob here and actually compile something in a way that vaguely resembles the structure of side-effect languages.
I have the following code:
main =
do {
let x = [0..10];
print x
}
I've learned here, that the keyword do is a fancy syntactic sugar for fancy monadic expressions. When I try to compile it, I get the following error:
main.hs:4:1: parse error on input 'print'
And I've learned in this other question, that tabs in Haskell are evil, so I've tried to omit them:
main =
do {
let x = [0..10];
print x
}
And I've failed miserably, because the parse error persists.
I've also learned here, that print is a syntactic sugar for the fancy equivalent:
main =
do {
let x = [0..10];
putStrLn $ show x
}
But then I get this error instead:
main.hs:4:9: parse error on input 'putStrLn'
Trying to face my despair, I've tried to omit the let keyword, after reading this answer:
main =
do {
x = [0..10];
print x
}
And then I get:
main.hs:4:1: parse error on input '='
And in a final useless attempt, I've even tried to omit the ';' like this:
main =
do {
let x = [0..10]
print x
}
And got:
main.hs:4:1: parse error on input 'print'
So,
How to properly use monadic expressions in Haskell without getting parse errors? Is there any hope?
It took me a while to see what was actually going on here:
main =
do {
let x = [0..10];
print x
}
The above looks as if we have a do with two statements, which is perfectly fine. Sure, it is not common practice to use explicit braces-and-semicolons when indentation implicitly inserts them. But they shouldn't hurt... why then the above fails parsing?
The real issue is that let opens a new block! The let block has no braces, so the indentation rule applies. The block starts with the definition x = [0..10]. Then a semicolon is found, which promises that another definition is following e.g.
let x = [0..10] ; y = ...
or even
let x = [0..10] ;
y = ... -- must be indented as the x above, or more indented
However, after the semicolon we find print, which is even indented less than x. According to the indentation rule, this is equivalent to inserting braces like:
main =
do {
let { x = [0..10]; }
print x
}
but the above does not parse. The error message does not refer to the implicitly inserted braces (which would be very confusing!), but only to the next line (nearly as confusing in this case, unfortunately).
The code can be fixed by e.g. providing explicit braces for let:
main = do { let { x = [0..10] };
print x }
Above, indentation is completely irrelevant: you can add line breaks and/or spaces without affecting the parsing (e.g. as in Java, C, etc.). Alternatively, we can move the semicolon below:
main = do { let x = [0..10]
; print x }
The above semicolon is on the next line and is less indented than x, implicitly inserting a } which closes the let block. Here indentation matters, since let uses the indentation rule. If we indent the semicolon more, we can cause the same parse error we found earlier.
Of course, the most idiomatic choice is using the indentation rule for the whole code:
main = do let x = [0..10]
print x
I was about to say, with no useful information, that
main = do
let x = [0..10]
print x
Was working for me, but I'm now off to read about the in within the braces.
As a slight aside, I found http://echo.rsmw.net/n00bfaq.html quite handy for reading about identation/formatting.
main = do let x = [0..10]
print x
works for me
and so does
main = do { let x = [0..10]
in print x }
I think you're trying to mix some different syntax options up.
I'm solving the 8-queens problem in Haskell using only basic functions nothing fancy
this is the code:
queens = [[x1,x2,x3,x4,x5,x6,x7,x8]|x1<-[1..8],x2<-[1..8],x3<-[1..8],x4<-[1..8],x5<-[1..8],x6<-[1..8],x7<-[1..8],x8<-[1..8],safeH [x2,x3,x4,x5,x6,x7,x8] x1,safeD [x2,x3,x4,x5,x6,x7,x8] x1 [x1,x2,x3,x4,x5,x6,x7,x8] 1]
safeH l e = if elem e l then False
else if length (l)/=0 then safeH(tail l)(head l)
else True
safeD l e xs n = if last(xs)/=e || length xs == 0 then
if length(l)/=0 then
if (head(l)+n==e || head(l)-n==e) then False
else safeD(tail l)(e)(xs)(n+1)
else safeD(tail xs)(head xs)(tail xs)(1)
else True
To clarify the SafeH function checks that no queens are in the same row H stands for Horizantly while the SafeD is supposed to check for diagonal conflicts
I am sure that the SafeH function is okay the problem with SafeD
and when compiling the code it gives me no problems but when calling the queens function
it gives me this error:
[1 of 1] Compiling Main ( y.hs, interpreted )
Ok, modules loaded: Main.
*Main> queens
*** Exception: Prelude.last: empty list
can anyone please help me?? thanks in advance for every thing :)
You can fix the immediate problem by checking the length of xs before calling last:
safeD l e xs n = if length xs == 0 || last(xs)/=e then ...
However, you will then run into another problem because you call safeD(tail xs)(head xs)(tail xs)(1) inside the then part of this branch, and you can reach the then part of this branch when length xs == 0.
I strongly recommend learning a little bit about pattern matching (Gentle Intro section, Haskell Report section) and trying to write this entire code snippet without ever calling head, tail, init, last, or length. Instead, use the two patterns [] for matching empty lists and (x:xs) (or similar) for matching lists that start as x and end with xs; if necessary, a call to reverse once in a while would be okay.
Good luck, and let us know how you fare and where you get stuck!
Ever thought of solving this problem for zero queens first?
Then for one queen?
Then spot the inductive / recursive pattern?.. I am not quoting the 3-liner solution, as it looks like your homework.