Talk:Haskell/Truth values

Apfelmus' outline: Calculating with Truth
Numbers are not the only thing we can calculate with. In the following, we are going to calculate with truth (also called boolean) values.


 * Introduction to Types,  is ill-typed.


 * Pattern Matching
 * Infix operators?

True || x = True False || y = y


 * Defining more functions: Guards

abs x    | x <= 0    = 0 - x     | otherwise = x

Duplode's comment
In my attempt to write this page I took some liberties with Apfelmus' outline. One particularly important change was the omission of definitions for (||) and (&&). I feel that introducing pattern matching on the top of all the other things discussed on this very early module might not work as well as we would like. Furthermore, it won't be intuitive to a newbie that pattern matching with pseudo-literal values like True and False is the same thing as pattern matching with, say, :; so it may be better to defer this explanation a little. One possible place for that would be the discussion on piece-wise definitions, if it is going to remain within Basics (I assume we will need to do so, since it is a prerequisite for Recursion at the very least). And finally, comments, criticism and bold rearrangements are always welcome :) --Duplode (talk) 07:46, 29 May 2010 (UTC)

Possible Update Needed
With ghci v7.10.2 I get the following error with the guard function when the first conditional line applies:

Non type-variable argument in the constraint: Num (a1 -> a)   (Use FlexibleContexts to permit this) When checking that ‘it’ has the inferred type it :: forall a a1. (Num a, Num a1, Num (a1 -> a), Ord a1) => a1 -> a

I am sure that I am copy-typing the code exactly into a text editor and module importation succeeds with no errors.

I don't know enough aoout Haskell (yet) to know why I'm getting this error, but in any case maybe the page needs to be updated to reflect ghc 7.10.2. Miroslav65 (discuss • contribs) 14:29, 11 November 2015 (UTC)


 * I can't reproduce the error − both the  implementation and   work as expected. How did you use the function? The type error might be due to some issue with the arguments you are giving to it.--Duplode (discuss • contribs) 00:28, 12 November 2015 (UTC)


 * With values > 0 and 0 the function executes normally. With values < 0 such as -1 and -37 it gives the above error. This is true in both Linux and Windows 7 using Haskell Platform. Miroslav65 (discuss • contribs) 00:30, 12 November 2015 (UTC)


 * The issue is caused by the minus sign:  is handled as if you were trying to subtract 1 from , leading to the error.   will work.--Duplode (discuss • contribs) 00:39, 12 November 2015 (UTC)


 * Correct, thank you! But the rorrollary issue I forgot to mention is that Prelude raises an exception unless I specify Main.abs or use a function name other than abs. Miroslav65 (discuss • contribs) 00:43, 12 November 2015 (UTC)


 * Well spotted. I have renamed the function to avoid the name clash.--Duplode (discuss • contribs) 00:51, 12 November 2015 (UTC)