Haskell newbie question
Haskell newbie question
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"?
Archived topic from Iceteks, old topic ID:3451, old post ID:27888
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"?
Archived topic from Iceteks, old topic ID:3451, old post ID:27888
Haskell newbie question
For the first question, this should work.
Code: Select all
import IO
myfunc nbr =
do
putStr ("This is " ++ show nbr)
hFlush stdout
if nbr < 10 then myfunc (nbr + 1)
else return ()[code]
However, Haskell already makes this pretty easy. You could simply write:
[code]mapM_ (
br -> do { putStr ("This is " ++ show nbr); hFlush stdout }) [0..9][code]
:)
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[code]
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[code]
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][code]
I would get in return:
[code]Just 3[code]
But if I run:
[code]findPos 7 0 [1,2,3,4,5][code]
I get:
[code]Nothing[code]
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)[code]
[color=#888888][size=85]Archived topic from Iceteks, old topic ID:3451, old post ID:27898[/size][/color]
Haskell newbie question
Thank you 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
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: Select all
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
[code]
but when trying it by typing pow (2,4) or something, I get an error
[color=#888888][size=85]Archived topic from Iceteks, old topic ID:3451, old post ID:27913[/size][/color]
Haskell newbie question
Using "div", which does integer math... (div 3 2 == 1, for instance)
Code: Select all
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[code]
Oh, and you'll notice that I used a new definition of "pow" for an exponent of 1. :)
[color=#888888][size=85]Archived topic from Iceteks, old topic ID:3451, old post ID:27940[/size][/color]
Haskell newbie question
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.
Archived topic from Iceteks, old topic ID:3451, old post ID:28483
: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.
Archived topic from Iceteks, old topic ID:3451, old post ID:28483
Haskell newbie question
What's the first line of main.hs?Endow wrote: 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.
Archived topic from Iceteks, old topic ID:3451, old post ID:28486
Haskell newbie question
module Main where
Just like in the tutorial
Archived topic from Iceteks, old topic ID:3451, old post ID:28487
Just like in the tutorial
Archived topic from Iceteks, old topic ID:3451, old post ID:28487
Haskell newbie question
Module "Main" should be in "Main.hs". If I indicated otherwise in the turorial, then I apologize.
Archived topic from Iceteks, old topic ID:3451, old post ID:28488
Archived topic from Iceteks, old topic ID:3451, old post ID:28488
Haskell newbie question
Ah yes but it doens't have anything to do with the capitalized M.I checked.I still get the same error message.
Archived topic from Iceteks, old topic ID:3451, old post ID:28659
Archived topic from Iceteks, old topic ID:3451, old post ID:28659
Haskell newbie question
Odd.
On my Linux box:
On my Linux box:
Code: Select all
$ 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]
$[code]
[color=#888888][size=85]Archived topic from Iceteks, old topic ID:3451, old post ID:28664[/size][/color]
Haskell newbie question
The worst part is that WinHugs isn't working alright for me.
Archived topic from Iceteks, old topic ID:3451, old post ID:28675
Archived topic from Iceteks, old topic ID:3451, old post ID:28675
Haskell newbie question
You should try GHCi.
http://haskell.org/ghc/
Archived topic from Iceteks, old topic ID:3451, old post ID:28678
http://haskell.org/ghc/
Archived topic from Iceteks, old topic ID:3451, old post ID:28678
Haskell newbie question
"i"?
I think I already have GHC correctly installed.
Archived topic from Iceteks, old topic ID:3451, old post ID:28679
I think I already have GHC correctly installed.
Archived topic from Iceteks, old topic ID:3451, old post ID:28679
Haskell newbie question
GHCi is the interactive version of GHC. Quite like Hugs, but in my opinion, better.
Archived topic from Iceteks, old topic ID:3451, old post ID:28680
Archived topic from Iceteks, old topic ID:3451, old post ID:28680