简体   繁体   中英

Simple Haskell Code List of numbers

I don't know why this code does not run. I just simply wanted to fill 0's with a number like 4 and return the results. I am new in Haskell sorry if my question is very basic.

fill [] = []
fill (x:xs) = if x==0 then 0 else 4 : fill xs

 main = do
  fill [0,1,0]

Let's see what the compiler is actually looking at when it sees your function fill : (I don't have ghc at my disposal right now, but it should look something like below)

> :t fill
fill :: (Num a) => [a] -> [a] -- or fill:: [Integer] -> [Integer] for simplicity

Okay, that's a function that takes a list of numerics to return another list of numerics. Let's look at main:

> :t main
main :: IO ()

Wait, what's IO doing there? Well, main the entry point for all standalone haskell programs. It exposes your functions out into the real world modelled by the poorly named IO wrapper.

Now, what did you actually want to accomplish here?

I just simply wanted to fill 0's with a number like 4 and return the results.

Right, so let's get down to it. Here's my type definition - all I'm saying is that, whatever be the type of lists that I get here, characterised by a - it should conform to numbers, that's what I mean why I constrain the types to Num . Num here is a typeclass, which you can look more about here .

fill :: (Num a) => [a] -> [a] 

Now, when I see an empty list, I return back an empty list. Easy -

fill [] = []

In your function definition, you're not replacing zeroes at all - let's fix that:

fill (x:xs) = if x == 0 
              then 4:fill xs 
              else x:fill xs

Okay, we're still not done here - how do we expose fill to our outside world? Cometh the main, cometh the world. Cheesy, I know :-) But main wraps everything into an IO , how do we wrap our little function into it? Ah, how do I display strings out into IO ? putStrLn or print ?

main :: IO ()
main = putStrLn "Hello World!"

We're now safely esconsced in our little echo chambers muttering "hello world" to ourselves. Let's make it a bit more useful. Now, I'm just going to print out our list:

> :t print
print :: Show a => a -> IO ()

Like Num , Show is also another typeclass. I leave you to figure this out as homework. :-)

main = print $ fill [0,1,0,1]

which prints:

[4,1,4,1]

This should work:

fill [] = []
fill (x:xs) = if x==0
              then 4:fill xs
              else x:fill xs

main = do
  putStrLn $ show (fill [0,1,0])

When you check for 0, you should just not return 0 but along with 0 you should call the function fill recursively.

And in main function, show is used for taking a type and returning the String equivalent for it so that it can be printed in the do block.

Just wrap if-then-else in parentheses:

fill [] = []
fill (x:xs) = (if x==0 then 0 else 4) : fill xs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM