-2

I am learning Haskell and after grasping some of the basics I decided to solve some easy problems in HackerRank. But I soon found myself stuck.

Problem 7 in Functional Problems is called "Array of N elements"

We are supposed to return any array of n elements for a given n. This would have fairly simple if n was an Int but it's read as IO Int. And I haven't really grasped the concept of monads. I hoogled for functions with type signature IO Int -> Int and found unsafePerform IO but it threw this:

Couldn't match expected type ‘t0 -> t’
              with actual type ‘[Integer]’

I dont't quite understand what types t0 and t are.

Any help is appreciated.

Link: https://www.hackerrank.com/challenges/fp-array-of-n-elements/problem

rjpj1998
  • 297
  • 1
  • 6
  • 23
  • 5
    `unsafePerformIO` is almost certainly the wrong answer. What solution did you attempt that gave you an `IO` where you weren't expecting it? – Silvio Mayolo Dec 29 '17 at 03:49
  • It's called "Array of N elements", Link: https://www.hackerrank.com/challenges/fp-array-of-n-elements/problem the haskell version atleast. – rjpj1998 Dec 29 '17 at 03:51
  • 2
    Yes, I understand that. It's a programming challenge, which means you have to have attempted it with some code. What code did you use? Simply running the challenge with a blank function is not going to accomplish anything. – Silvio Mayolo Dec 29 '17 at 03:52
  • fn n = [1..(unsafePerformIO n)] – rjpj1998 Dec 29 '17 at 03:55
  • Okay, I think I understand your confusion now. Does my answer help? – Silvio Mayolo Dec 29 '17 at 03:55
  • Possible duplicate of ["<-" bindings in do notation](https://stackoverflow.com/questions/9932913/bindings-in-do-notation) – Silvio Mayolo Dec 29 '17 at 04:01

1 Answers1

4

The input to fn on that website is an Int. You're fundamentally misunderstanding the do-notation used here.

main = do
 n <- readLn :: IO Int
 print (fn(n))

The second line does not say n has type IO Int. It says readLn has type IO Int, which is then bound inside the IO monad to n. So n has type Int. Write your function as fn :: Int -> [Int] and you'll be just fine. No need to worry about IO in this challenge.

You can read more about do-notation here, but it probably won't make too much sense unless you've been through a Haskell tutorial that covers monads.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • Oh I understand now. So the code would be same if it was just `n <- readLn` instead of `n<-readLn : IO Int`? Also I think I'll have to write `fn :: Int->[Int]` – rjpj1998 Dec 29 '17 at 04:01
  • Yes. The type signature is only there to remind you, the coder, what type you need to input. And you're correct about the output type; that was an oversight on my part. – Silvio Mayolo Dec 29 '17 at 04:02
  • I tried this: `fn :: Int -> [Int] fn n = [1..n]` but it threw `Couldn't match expected type ‘t0 -> [Int]’ with actual type ‘[Int]’` What does the type t0 mean? Any ambigous type? – rjpj1998 Dec 29 '17 at 04:04
  • Hey. I just figured it out. Apparently I had to remove the underlines before submitting. :) – rjpj1998 Dec 29 '17 at 04:06
  • Ah okay. Glad you figured it out! – Silvio Mayolo Dec 29 '17 at 04:07