I've downloaded WinHugs 2 hours ago and still can't figure out how to declare simple things. I'm trying to follow the book "7 languages in 7 weeks", but stuff like let x = 10 and double x = x * 2 gives syntax errors.
I'm not 100% sure what you're trying to do that doesn't work. You can't declare bindings in a WinHugs session, you can only evaluate full expressions. So you could do things like let x = 10 in x * x + x, but you can't say let x = 10 in an interactive session. In other words, you can't make the declaration 'stick'.
To get around this, either put your declarations in a .hs file and load it in WinHugs, or use GHCi instead (this is the better option, in my opinion - WinHugs is pretty dated). You can install GHCi by downloading Haskell Platform.
in winhugs the following gives a syntax error
let double x = x * 2
but the following works:
let double x = x * 2 in double 10
however in ghc they have the interactive environment ghci where everything works
let double x = x * 2
works
double 10
works
this link explains how to work with ghci environment: https://downloads.haskell.org/~ghc/7.2.2/docs/html/users_guide/interactive-evaluation.html
One minor issue is that on windows you need the presence of cygwin - otherwise ghci as compiled for windows will not work.
Related
This is my program:
modify :: Integer -> Integer
modify a = a + 100
x = x where modify(x) = 101
In ghci, this compiles successfully but when I try to print x the terminal gets stuck. Is it not possible to find input from function output in Haskell?
x = x where modify(x) = 101
is valid syntax but is equivalent to
x = x where f y = 101
where x = x is a recursive definition, which will get stuck in an infinite loop (or generate a <<loop>> exception), and f y = 101 is a definition of a local function, completely unrelated to the modify function defined elsewhere.
If you turn on warnings you should get a message saying "warning: the local definition of modify shadows the outer binding", pointing at the issue.
Further, there is no way to invert a function like you'd like to do. First, the function might not be injective. Second, even if it were such, there is no easy way to invert an arbitrary function. We could try all the possible inputs but that would be extremely inefficient.
Okay, I've looked on about 4-5 websites that offered to teach Haskell and not one of them explained the keyword aux. They just started using it. I've only really studied Java and C (never saw it in either if it exists), and I've never really encountered it before this class that I'm taking on Haskell. All I can really tell is that it provides the utility to create and store a value within a function. So what exactly does it do and how is it properly used and formatted? In particular, could you explain its use while recursing? I don't think that its use is any different, but just to make sure I thought I would ask.
There is no keyword aux, my guess is this is just the name they used for a local function.
Just like you can define top-level values:
myValue = 4
or top-level functions:
myFunction x = 2 * x
you can similarly define local values:
myValue =
let myLocalValue = 3 in
myLocalValue + 1
-- or equivalently:
myValue = myLocalValue + 1
where myLocalValue = 3
or a local function:
myValue =
let myLocalFunction x = 2 * x in
myLocalFunction 2
-- or equivalently:
myValue = myLocalFunction 2
where myLocalFunction x = 2 * x
Your teacher simply named the local function aux instead of myLocalFunction.
Search for "vim haskell indent" on SO. There are lot of answers for how to configure Vim for Haskell indentation. None of them really "work". They don't provide code as is recommended by the Haskell indentation wiki page. For example, alignment of statements in a do or let block, the = and | of a data type, etc.
Does a Vim solution exist that generates code like the wiki?
This might not be the answer your are looking for, but there is a way you can follow the indentation wiki guide and be compatible with most editors.
For example, do-blocks
Instead of
myFunc x = do y <- bar
return $ x + y
You can indent it like this
myFunx x = do
y <- bar
return $ x + y
This is explicitly mentioned as an acceptable alternative in the indentation wiki.
In the same way, you can format data types
data FooBar
= Foo
| Bar
| Asdf
Guards
myFunc x
| x < 0 = 0
| otherwise = x
Where-clauses
myFunc x = x + y + c where
y = x + 5
c = x * y
And so on...
I personally started to use this kind of style because, like you said, no editor could reliable indent the code otherwise. This works better in all editors, as the indentation is always a multiple of four (or whatever else you pick for your base indentation level). As I used this style, I also started to prefer this consistent indentation level visually, so I wouldn't go back at this point even if editors got smarter.
I just started with Haskell and tried to do write some tests first. Basically, I want to define some function and than call this function to check the behavior.
add :: Integer -> Integer -> Integer
add a b = a+b
-- Test my function
add 2 3
If I load that little script in Hugs98, I get the following error:
Syntax error in declaration (unexpected `}', possibly due to bad layout)
If I remove the last line, load the script and then type in "add 2 3" in the hugs interpreter, it works just fine.
So the question is: How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.
Others have said how to solve your immediate problem, but for testing you should be using QuickCheck or some other automated testing library.
import Test.QuickCheck
prop_5 = add 2 3 == 5
prop_leftIdentity n = add 0 n == n
Then run quickCheck prop_5 and quickCheck prop_leftIdentity in your Hugs session. QuickCheck can do a lot more than this, but that will get you started.
(Here's a QuickCheck tutorial but it's out of date. Anyone know of one that covers QuickCheck 2?)
the most beginner friendly way is probably the doctest module.
Download it with "cabal install doctest", then put your code into a file "Add.hs" and run "doctest Add.hs" from the command line.
Your code should look like this, the formatting is important:
module Add where
-- | add adds two numbers
--
-- >>> add 2 3
-- 5
-- >>> add 5 0
-- 5
-- >>> add 0 0
-- 0
add :: Integer -> Integer -> Integer
add a b = a+b
HTH Chris
Make a top level definition:
add :: Integer -> Integer -> Integer
add a b = a + b
test1 = add 2 3
Then call test1 in your Hugs session.
How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.
In short, you can't. Wrap it in a function and call it instead. Your file serves as a valid Haskell module, and having "flying" expression is not a valid way to write it.
You seem to come from a scripting language background, but don't try treating Haskell as one of them.
If you have ghc installed, then the runhaskell command will interpret and run the main function in your file.
add x y = x + y
main = print $ add 2 3
Then on the command line
> runhaskell Add.hs
5
Not sure, but hugs probably has a similar feature to the runhaskell command. Or, if you load the file into the hugs interpreter, you can simply run it by calling main.
I was trying to do the same thing and I just made a function that ran through all my test cases (using guards) and returned 1 if they all passed and threw an error if any failed.
test :: Num b => a->b
test x
| sumALL [1] /= 1 = error "test failed"
| sumALL [0,1,2] /= 3 = error "test failed"
...
| otherwise = 1
What is a valid name for a function?
Examples
-- works
let µ x = x * x
let ö x = x * x
-- doesn't work
let € x = x * x
let § x = x * x
I am not sure, but my hunch is that Haskell doesn't allow Unicode function names, does it?
(Unicode like in http://www.cse.chalmers.se/~nad/listings/lib-0.4/Data.List.html)
From the Haskell report:
Haskell uses the Unicode character set. However, source programs are currently biased toward the ASCII character set used in earlier versions of Haskell .
Recent versions of GHC seem to be fine with unicode (at least in the form of UTF-8):
Prelude> let пять=5; два=2; умножить=(*); на=id in пять `умножить` на два
10
(In case you wonder, «пять `умножить` на два» means «five `multiplied` by two» in Russian.)
Your examples do not work because those character are «symbols» and can be used in infix operators but not in function names. See "uniSymbol" category in the report.
Prelude> let x € y = x * y in 2 € 5
10