[Site Home] [Forum Home] [Articles] [File DB] [News Archives]

Programming -> Haskell newbie question


(View original topic)


wtd - Jul-25-2005 server time
GHCi is the interactive version of GHC. Quite like Hugs, but in my opinion, better.

Endow - Jul-25-2005 server time
"i"?

I think I already have GHC correctly installed.

wtd - Jul-25-2005 server time
You should try GHCi.

http://haskell.org/ghc/

Endow - Jul-25-2005 server time
The worst part is that WinHugs isn't working alright for me.

wtd - Jul-24-2005 server time
Odd.

On my Linux box:

CODE
$ cat > Main.hs
module Main where

main = putStrLn "Hello world"
$ hugs
__   __ __  __  ____   ___      _________________________________________
||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-2003
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Report bugs to: hugs-bugs@haskell.org
||   || Version: November 2003  _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions

Type :? for help
Prelude> :load Main
Main> main
Hello world

Main> :q
[Leaving Hugs]
$

Endow - Jul-24-2005 server time
Ah yes but it doens't have anything to do with the capitalized M.I checked.I still get the same error message.

wtd - Jul-23-2005 server time
Module "Main" should be in "Main.hs". If I indicated otherwise in the turorial, then I apologize.

Endow - Jul-23-2005 server time
module Main where


Just like in the tutorial smile.gif

wtd - Jul-23-2005 server time
QUOTE (Endow @ Jul 23 2005, 12:23 PM)
Okay I'm new here and I'm also new to functional programming.But my problem isn't with the actual programming.I'm reading that evry helpful Haskell tutorial but when I'm in Hugs and I want to load a module and I do this :

:load main

this appears :


ERROR "main.hs":1 - Syntax error in input (unexpected backslash (lambda) )

The truth is I never even used the command prompt before but it doens't look liekt hat's the problem.

What's the first line of main.hs?

Endow - Jul-23-2005 server time
Okay I'm new here and I'm also new to functional programming.But my problem isn't with the actual programming.I'm reading that evry helpful Haskell tutorial but when I'm in Hugs and I want to load a module and I do this :

:load main

this appears :


ERROR "main.hs":1 - Syntax error in input (unexpected backslash (lambda) )

The truth is I never even used the command prompt before but it doens't look liekt hat's the problem.

wtd - Jul-04-2005 server time
Using "div", which does integer math... (div 3 2 == 1, for instance)

CODE
pow _ 0 = 1
pow x 1 = x
pow x n =
if rem n 2 == 0
 then pow x (div n 2) ^ 2
 else x * pow x (div n 2) ^ 2


Oh, and you'll notice that I used a new definition of "pow" for an exponent of 1. smile.gif

WhoDoo - Jul-03-2005 server time
Thank you smile.gif I have another question and I cant understand why it doesnt work. I wanna calculate x^n by using:


x^0 = 1
x^n = (x^(n/2))^2 if n is even
x^n = x * (x^(n/2))^2 if n is odd

I use this code

CODE

pow (x,0) = 1  
pow (x, n) =
if rem n 2 == 0
 then (pow (x, n/2))^2
 else x * (pow (x, n/2))^2

but when trying it by typing pow (2,4) or something, I get an error

wtd - Jul-02-2005 server time
For the first question, this should work.

CODE
import IO

myfunc nbr =
 do
   putStr ("This is " ++ show nbr)
   hFlush stdout
   if nbr < 10 then myfunc (nbr + 1)
   else return ()


However, Haskell already makes this pretty easy. You could simply write:

CODE
mapM_ (\nbr -> do { putStr ("This is " ++ show nbr); hFlush stdout }) [0..9]


smile.gif

As for your second question... the closest thing to nothing in Haskell is ().

However, it sounds more like you're thinking of the Maybe data type. Let's consider a fairly simple example. You want to find the position of an element in a list.

First, we know that nothing we could possibly be located anywhere in an empty list. So the item and the counter don't even matter.

CODE
findPos _ _ [] = Nothing


But it might be in a non-empty list.

CODE
findPos item counter (x:xs) =
 if x == item then
   Just counter
 else
   findPos item (counter + 1) xs


Here we used the Just constructor for the Maybe data type, whereas we had used the Nothing constructor previously.

Now if I were to run:

CODE
findPos 4 0 [1,2,3,4,5]


I would get in return:

CODE
Just 3


But if I run:

CODE
findPos 7 0 [1,2,3,4,5]


I get:

CODE
Nothing


I can use pattern matching to discern this at run-time.

CODE
case findPos n 0 [1..9] of
 Nothing -> putStrLn (show n ++ " wasn't found.")
 Just pos -> putStrLn (show n ++ " found at index " ++ show pos)

WhoDoo - Jul-01-2005 server time
I've just started learning Haskell and I have two questions. First of all, what if I want to do two things in a function? (like first I want to print something, then I would like to do a function call). I want to make a simple recursive "for-loop" like this


myfunc nbr =
putStr "This is " ++ nbr;
if nbr < 10
then myfunc(nbr+1)
else return

somethings like that, but the code above isnt working. In C it would be something like

void function(int nbr) {
cout<<"This is" << nbr;
if(nbr < 10)
function(nbr+1);
else
return;
}

my second question is, what if I dont want a function to return something? Mabye I have a function that should print something if I pass a certain argument. Since I need an else-statement, what should I put there in order to do "nothing"?

(Showing 50 last posts, newest on top)