haskell

Does Hask form a theoretically valid category? Or it just pretends to be one?

点点圈 提交于 2019-12-24 18:33:58
问题 Hask looks like a subcategory of the SET - category of all sets and single-argument functions between them. However, seems like it fails to preserve id when it comes down to the undefined : seq undefined () fails with exception as it suppose to, however seq (undefined . id) () = seq (id . undefined) () = () . Obviously, id acts wrongly. Is there a way to (theoretically at least) resolve this issue and make Hask a category indeed? Except throwing undefined away? 来源: https://stackoverflow.com

Haskell display Tree without Data.Tree or deriving(show)

点点圈 提交于 2019-12-24 18:05:33
问题 Hi guys I'm having an issue with my instance to show a tree in Haskell: here is my code: data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a instance (Show a, Show b) =>Show (Tree a b) where show (Leaf x) = " "++show x ++"\n" show (Branch val l r) = show val ++ "\n" ++" " ++ show l ++ " " ++ show r here is my test case and output( which is wrong): Branch "<" (Branch "<" (Leaf 'a') (Leaf 'c')) (Branch "<" (Leaf 'g') (Branch "<" (Leaf 'n') (Leaf 'y')))) "<" "<" 'a' 'c' "<" 'g' "<" 'n' 'y'

Haskell display Tree without Data.Tree or deriving(show)

大城市里の小女人 提交于 2019-12-24 18:05:07
问题 Hi guys I'm having an issue with my instance to show a tree in Haskell: here is my code: data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a instance (Show a, Show b) =>Show (Tree a b) where show (Leaf x) = " "++show x ++"\n" show (Branch val l r) = show val ++ "\n" ++" " ++ show l ++ " " ++ show r here is my test case and output( which is wrong): Branch "<" (Branch "<" (Leaf 'a') (Leaf 'c')) (Branch "<" (Leaf 'g') (Branch "<" (Leaf 'n') (Leaf 'y')))) "<" "<" 'a' 'c' "<" 'g' "<" 'n' 'y'

Existential Types. Writing the instance of a class for an heterogeneous map

只谈情不闲聊 提交于 2019-12-24 17:39:25
问题 Using the following type and class definitions, I do not understand why I get and error when creating the instance below. I need MyMap to hold a map of heterogeneous values. {-# LANGUAGE ExistentialQuantification #-} module Scratch.SO_ExtistentialTypes where import Data.Map type MyMap a = Map String a class MyClass c where getMyMap :: forall a. c -> MyMap a data MyData = forall a. MyData { myMap :: MyMap a } instance MyClass MyData where getMyMap = myMap -- <= ERROR 回答1: For one thing, the

Any way to add patterns, type signature, to a function in GHCi?

℡╲_俬逩灬. 提交于 2019-12-24 17:19:01
问题 ^-- No, it doesn't entirely. My question covers ADDING patterns and type signatures interactively...which is apparently impossible. The most basic things you might try do from early tutorials won't work in GHCi: foo [] = [] foo (x:xs) = x : foo xs That works if you put it into foo.hs and at the GHCi prompt type :load foo.hs . You can then invoke foo on a list and get the list back. Early Google searches tell you that in GHCi you need a let statement. But in this case (a function defined with

split elements into groups haskell

拈花ヽ惹草 提交于 2019-12-24 16:53:25
问题 Hey Im new to functional programming and learning haskell. I'm wondering whether will i be able to split elements in a list and grouping them in two's. I already saw the splitAt operations and it only splits at specified index value/ position splitAt 3 [1,2,3,4,5] -> [1,2,3][4,5] Now I'm wondering say I have a list where random characters [A,S,D,F,G,H,J,K,U,Y,R,E,W,V,B,N] , I want to split this as [A,S][D,F][G,H][J,K].... and so on .. I' m totally stuck up in this ! Please help me out ! 回答1:

Haskell function to check if two values are identical

让人想犯罪 __ 提交于 2019-12-24 16:42:09
问题 In Haskell we have the function (==) :: Eq a => a->a->Bool which is fine for the large number of datatypes for which an instance of Eq can be defined. However there are some types for which there is no reasonable way to define an instance of Eq . one example is the simple function type (a->b) I am looking for a function that will tell me if two values are actually the same -- not equal but identical. For example f(id,id) ==> True f((+),(-)) = False Clarification: I don't want to know if two

solution or workarounds for haskell-src-exts parsing modules with CPP failing

◇◆丶佛笑我妖孽 提交于 2019-12-24 16:38:59
问题 I'm trying to do some parsing of a bunch of haskell source files using haskell-src-exts but ran into trouble in the first file I tested on. Here is the first bit: {-# LANGUAGE CPP, MultiParamTypeClasses, ScopedTypeVariables #-} {-# OPTIONS_GHC -Wall -fno-warn-orphans #-} ---------------------------------------------------------------------- -- | -- Module : FRP.Reactive.Fun -- Copyright : (c) Conal Elliott 2007 -- License : GNU AGPLv3 (see COPYING) -- -- Maintainer : conal@conal.net --

Keep variable inside another function in Haskell

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 16:35:18
问题 I am lost in this concept. This is my code and what it does is just asking what is your name. askName = do name <- getLine return () main :: IO () main = do putStrLn "Greetings once again. What is your name?" askName However, How can I access in my main the variable name taken in askName? This is my second attempt: askUserName = do putStrLn "Hello, what's your name?" name <- getLine return name sayHelloUser name = do putStrLn ("Hey " ++ name ++ ", you rock!") main = do askUserName >>=

How to fix this Conduit code invovling the appearance of a list type where I do not expect one?

£可爱£侵袭症+ 提交于 2019-12-24 15:57:00
问题 I've been struggling with this Conduit code for a while, any help would be extremely appreciated. It is sort of like this code has been evolving by random mutation while the type checker is enforcing natural selection. Here is one of the fittest candidates I have so far: import Conduit import qualified Data.Conduit.Combinators as DCC import Data.CSV.Conduit import Data.Function ((&)) import Data.List.Split (splitOn) import Data.Map as DM import Data.Text (Text) import qualified Data.Text as