haskell

Haskell, IO, monads, quickcheck

拟墨画扇 提交于 2019-12-25 07:59:17
问题 Beginner at Haskell here. I have a function, and a bunch of properties to test it with which I've written using quickcheck. The properties work when I run them individually in the interpreter, and the function checks out fine. However, manually quickchecking with each property in the interpreter by hand (quickCheck prop_this, quickCheck prop_that) is boring, monotonous, and time-consuming. I would like to stick all of this in a program, and have that program run all of the quickchecks. This

Simple type error on parsing the result of getArgs

廉价感情. 提交于 2019-12-25 07:48:58
问题 I'm trying a very simple argument parser patterned off this example. The first argument ought to be a double, the second ought to be an integer, and if they aren't both of those types, I'd like to use default arguments for both specified in the else. Here's what I have: parseArgs :: [String] -> (Double, Integer) parseArgs args = do if length(args) == 2 then do let v1 = read (args !! 0) :: Double let v2 = read (args !! 1) :: Integer return (v1, v2) else do let v1 = read ("0.5") :: Double let

Why does `traceId` return the message string itself?

允我心安 提交于 2019-12-25 07:39:53
问题 I found it really weird that traceId returns the message string itself. Why would anyone want to do that? Is there a common scenario where traceId would come in handy? 回答1: EDIT: Credits to David who drew attention on the fact that my traceId' is exactly traceShowId . Hence, traecId is simply the String -> String monomorphic version of the polymorphic traceShowId :: Show a -> a -> a , which also strips the printed string from quotes. Old answer: One way to interpret traceId is that it's a

How can I find the indexes of an element in Haskell list

拈花ヽ惹草 提交于 2019-12-25 07:00:44
问题 Here is a Haskell list: [[1],[3,5],[],[1,9],[3,5],[9,7],[1,9]] I want to find the exact index of its elements. I tried to use elemIndex but it doesn't give the correct index when I try to find duplicated element like [3,5] and [1,9] . 回答1: Is this what you want? > elemIndices [3,5] [[1],[3,5],[],[1,9],[3,5],[9,7],[1,9]] [1,4] 来源: https://stackoverflow.com/questions/27514828/how-can-i-find-the-indexes-of-an-element-in-haskell-list

Data Processing, how to approach

柔情痞子 提交于 2019-12-25 07:00:06
问题 I have the following Problem, given this XML Datastructure: <level1> <level2ElementTypeA></level2ElementTypeA> <level2ElementTypeB> <level3ElementTypeA>String1Ineed<level3ElementTypeB> </level2ElementTypeB> ... <level2ElementTypeC> <level3ElementTypeB attribute1> <level4ElementTypeA>String2Ineed<level4ElementTypeA> <level3ElementTypeB> <level2ElementTypeC> ... <level2ElementTypeD></level2ElementTypeD> </level1> <level1>...</level1> I need to create an Entity which contain: String1Ineed and

How does the function embeded in runParse of RWH get parameter with type ParseState?

半腔热情 提交于 2019-12-25 06:57:38
问题 After reading LYAH's Chapter 08 and SO questions (1, 2) about Real World Haskell's Parse definition: newtype Parse a = Parse { runParse :: ParseState -> Either String (a, ParseState) } For me, now I think it is a key understanding how runParse is evaluated when runParse is viewed as a function. (1) ghci> :t runParse will get runParse :: Parse a -> ParseState -> Either String (a, ParseState) (2) For trying runParse , similar to RWH's The identity Parser , I used the following example: ghci>

Clear file contents in haskell language

独自空忆成欢 提交于 2019-12-25 06:45:17
问题 How to clear all the file contents in haskell.?Can anyone help this,I want to remove all the data from a text file and make it empty.Is their any function to do so? Or can i make any such function? 回答1: You can write nothing on the file, overwriting what was there before. main = writeFile yourFile "" But is this really what you want to do? 来源: https://stackoverflow.com/questions/20062595/clear-file-contents-in-haskell-language

multiple declaration errors of algebraic data type

筅森魡賤 提交于 2019-12-25 06:13:42
问题 some algebraic data types.. data Cell0=Cell0 {c0str::Text,c0uid::Uid} deriving (Data,Typeable,Show) data Cell1=Cell1 {c1start::Uid,c1end::Uid,c1str::Text,c1uid::Int} deriving (Data,Typeable,Show) data Cell2=Cell2 {c2start::Uid,c2end::Uid,c2str::Text,c2uid::Int} deriving (Data,Typeable,Show) data Acell=Cell0|Cell1 but the last line induces a compile error "multiple declarations of Cell0" I also tried sth like this: data A=Aasdfdsf {sdf::Text} deriving (Data,Typeable,Show) data B=Bsdfsd data AB

Add a element at the end of list in Haskell

纵然是瞬间 提交于 2019-12-25 05:52:44
问题 i'm a beginner in Haskell and i'm trying to add an element at the end of a list. I enter a list like [1,2,3,4] and a number 10. I want an output like this [1,2,3,4,10]. My code: func a [] = a func a (x:xs) = x : func a (x:xs) But i'm getting that error: Non type-variable argument in the constraint: Num [a] (Use FlexibleContexts to permit this) When checking that ‘it’ has the inferred type it :: forall a. (Num a, Num [a]) => [a] Can anyone help with a explanation ? 回答1: You need to understand

Create an xml file in Haskell without using XML library

守給你的承諾、 提交于 2019-12-25 05:12:01
问题 I am a complete noob at Haskell and I am trying to do a project. I need to be able to create an xml based on the user input. For example I ask a user if they want to enter a name, if they choose yes, then it should create this as a simple xml <person> <name>Chosen name</name> </person> Any idea on how I can achieve this? This has to be done without using xml libraries Thank you so much in advance for any answers I havent really done much as I am not sure how to proceed, this is what I've done