Haskell - Putting out result of function

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

46 Replies - 4564 Views - Last Post: 24 July 2016 - 11:47 AM

#46 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2507
  • View blog
  • Posts: 3,961
  • Joined: 21-June 11

Re: Haskell - Putting out result of function

Posted 23 July 2016 - 10:13 AM

View PostO, on 22 July 2016 - 10:45 PM, said:

And would someone bother to explain me what those | at line 5 and 6 are?
I researched it and found an answer (by coincidence, also from you (sepp2k) at SO) that it had to do with functional dependencies. When I researched that, I found something about restricting function parameters, and that one can determine the value of another parameter. And return a parameter, which not necessary needs the type of the function??? I didn't got that.


The pipe in that code has nothing to do with functional dependencies. A | after a pattern adds pattern guard to the pattern. If a pattern with guards matches, it goes into the first branch whose guard evaluated to true. It's basically a shortcut for if-then-else. So this:

maximum' (x:xs)
    | x > maximumInTail = x
    | otherwise = maximumInTail
    where maximumInTail = maximum' xs



is equivalent to this:

maximum' (x:xs) =
    if x > maximumInTail
    then x
    else maximumInTail
  where maximumInTail = maximum' xs



Quote

And another thing,
Why won't print work at line 12? Because it's the same as
putStrLn $ show $, and the show ('converting' to string) causes an error?


Can you show the code that didn't work and what the error message was? print should work whenever putStrLn + show works.
Was This Post Helpful? 1
  • +
  • -

#47 O'Niel  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 378
  • Joined: 13-September 15

Re: Haskell - Putting out result of function

Posted 24 July 2016 - 11:47 AM

Thanks.
And I guess I was wrong concluding the print-thing, now, it indeed is just working.
Guess I made a syntax-error previous time or something related.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4