haskell - How to make the different instances of class, depending on context?

I have some simple class and some instances for various data types and context. This is a partially working case:{-# LANGUAGE FlexibleInstances, UndecidableInstances, IncoherentInstances #-}class Neighbors a where (~~) :: a -> a -> Boolinstance Neighbors Int where x ~~ y = (abs (x-y) == 1)instance (RealFloat a) => Neighbors a where x ~~ y = (abs(x-y) < 1.0)There is a disadvantage that is I had to use type restriction for Int:*Main> (1::Int) ~~ (2::Int)TrueBut, if I want use only contexts, my second script isn’t working at ...Read more

data structures - Why won't my test file import my datastructure? Haskell

I am currently writing unit tests for my (very simple) blackjack game and my testfile (Tests.hs) does not seem to import my datastructures that I have declared in the file I am doing unit tests for (HelpFunctions.hs). I can acces the functions/methods in this file but not the datastructures. Can someone please help me find the problem? This is the top of my testfile: module Tests(performTests) whereimport Test.HUnitimport HelpFunctionscardList = [(Hearts, Ace)](...)and this is the top of the file that i am going to write tests formodule HelpFun...Read more

haskell - Accessing documentation for a function from Hoogle command line

When searching for a function on the Hoogle website, one sees the documentation associated with it, e.g.:mod :: a -> a -> a infixl 7 integer modulus, satisfying (x `div` y)*y + (x `mod` y) == xHoogle also exists as a command line executable. As far as I know, it only shows the signature of the function:~ ❯❯❯ hoogle --info Prelude.modPrelude mod :: Integral a => a -> a -> aFrom package basemod :: Integral a => a -> a -> aIs there a way to get the associated documentation through the command line, as in th...Read more

haskell - Recursively calling in a show function

I am taking in a data type MyList which has a tail with list and a head. Need to turn it into a reversed string like the haskell data type list. showList :: MyList a -> Stringshowlist (MyList h t) = show(showlist(t) ++ show( h : [] ))I am getting out this craziness for showList [1,2,3]"\"[3][2]\"[1]"...Read more

functional programming - Getting started with Haskell

For a few days I've tried to wrap my head around the functional programming paradigm in Haskell. I've done this by reading tutorials and watching screencasts, but nothing really seems to stick.Now, in learning various imperative/OO languages (like C, Java, PHP), exercises have been a good way for me to go. But since I don't really know what Haskell is capable of and because there are many new concepts to utilize, I haven't known where to start.So, how did you learn Haskell? What made you really "break the ice"? Also, any good ideas for beginnin...Read more

functional programming - Large-scale design in Haskell?

What is a good way to design/structure large functional programs, especially in Haskell?I've been through a bunch of the tutorials (Write Yourself a Scheme being my favorite, with Real World Haskell a close second) - but most of the programs are relatively small, and single-purpose. Additionally, I don't consider some of them to be particularly elegant (for example, the vast lookup tables in WYAS).I'm now wanting to write larger programs, with more moving parts - acquiring data from a variety of different sources, cleaning it, processing it in...Read more

functional programming - Haskell function application and currying

I am always interested in learning new languages, a fact that keeps me on my toes and makes me (I believe) a better programmer. My attempts at conquering Haskell come and go - twice so far - and I decided it was time to try again. 3rd time's the charm, right?Nope. I re-read my old notes... and get disappointed :-(The problem that made me lose faith last time, was an easy one: permutations of integers.i.e. from a list of integers, to a list of lists - a list of their permutations:[int] -> [[int]]This is in fact a generic problem, so replacing...Read more

if statement - Haskell: Check if all conditions are true. If they are return true otherwise false

I was writing some Haskell this afternoon and I have a list of conditions that must be satisfied. If they are all true I want to return true, if one of them is false then return false.I have a method that is working but I was just wondering if there is a better way to implement it for readability/efficiency.Here is what I have:checkMatch :: Person -> Person -> BoolcheckMatch seeker candidate | gender candidate == preferedGender seeker && gender seeker == preferedGender candidate && minAcceptableAge seeker <= ...Read more

Haskell - isBread function

I have a project I am working on in Haskell that has me creating functions for a DSL. We are creating our own Types and using them to write various functions. data Layer = Bread Bread | Meat Meat | Cheese Cheese | Vegetable Vegetable | Condiment Condiment deriving (Eq,Show)data Bread = White | Wheat | Rye deriving (Eq,Show)What I am trying to do right now is implement an isBread function that takes a layer and returns true or false if it is bread or not.isBread :: Layer -> BoolisBread b | White = True ...Read more

If n is divisible by 2 return true - haskell

im trying to do a question in Haskell. I've just begun learning the language and can't get my head around this question. I need to return a boolean if n is divisible by 2, it will be true, otherwise false. Heres what i've gotten so far and it doesnt work...is_even :: Int -> Bool is_even n | n `mod` 2 == 0 return True | otherwise return False...Read more

equality - Haskell: Confusion over 'foldl' with parenthesized operator (==)

Here are a few results of applying 'foldl' to this particular equality operator. I do not understand them - I thought that each line should return true, since "False == False == False ..." is true regardless of the length of the list. I am fairly new to Haskell.Prelude> foldl (==) False [False]TruePrelude> foldl (==) False [False,False,False]TruePrelude> foldl (==) False [False,False,False,False]FalsePrelude> foldl (==) False [False,False,False,False,False]TruePrelude> foldl (==) False [False,False,False,False,False,False]FalseI ...Read more

Determine if binary tree is BST haskell

I'm trying to write a bool function to return True if a binary tree is a bst using recursion, and I need a little guidance on haskell syntax. I understand that for a binary tree to be a bst, the left subtree must always contain only nodes less than the head. and the right subtree must always contain only nodes greater than the head. I was structuring my function as such:isBST :: Tree -> Bool --recieve Tree, return boolisBST (Lead i) = True --return true if its only one leaf in treeisBST (Node h l r) = if (((isBST l) < h) &...Read more