haskell

Get function as parameter in haskell

我的未来我决定 提交于 2020-01-06 23:00:10
问题 I can't figure this, I have a type called Enumeration > type Enumeration a = Int -> [a] And I need to map over it. I wrote this following function: > imapE :: (a -> b) -> Enumeration a -> Enumeration b > imapE f (m fa) = \n -> imapF f fa where imapF is defined like this: > imapF :: (a -> b) -> [a] -> [b] > imapF _ [] = [] > imapF f (x:xs) = f x : imapF f xs but when I try to load my code I get the following error BinaryTrees.lhs:91:14: Parse error in pattern: m regarding my imapE function. I

postgresql-simple query error

会有一股神秘感。 提交于 2020-01-06 19:41:06
问题 EDIT: I now have a better idea of what is going wrong. When I perform that query in plain old psql , I get the following result: lwm@verbos =# SELECT * FROM gerund LIMIT1; infinitive │ gerund │ gerund_english ────────────┼─────────────┼──────────────── abandonar │ abandonando │ abandoning So, I am getting back 3 strings? However, I say that I am getting back IO [Only String] . I am sure it is my type signature here that is messing things up ... I am trying to make a simple query using the

How to convert IO int to String in Haskell?

核能气质少年 提交于 2020-01-06 19:36:50
问题 I'm learning to use input and output in Haskell. I'm trying to generate a random number and output it to another file. The problem is that the random number seems to be returning "IO int" that I can't covert it to String using "show". Could someone give me a pointer here?? 回答1: It's helpful if you show us the code you've written that isn't working. Anyway, you are in a do block and have written something like this, yes? main = do ... writeFile "some-file.txt" (show generateRandomNumberSomehow

how to resolve use of operators in type declaration (Repa) ?

空扰寡人 提交于 2020-01-06 19:33:00
问题 I am playing around with Repa, and the code below can compile and run. import qualified Data.Array.Repa as R --t:: R.Array R.U (R.Z R.:. Int) Float --t = R.fromListUnboxed (R.Z R.:. (10::Int)) ([1.0..10]::[Float]) main = do let x = R.fromListUnboxed (R.Z R.:. (10::Int)) ([1.0..10]::[Float]) print x I believe (from check in ghci) that x has the type signature that I have declared t to have, but I get this error if I uncomment everything associated with t: Illegal operator ‘R.:.’ in type ‘R.Z R

Writer monad and unsequence

你说的曾经没有我的故事 提交于 2020-01-06 18:10:14
问题 I am using the Writer monad to keep track of an error ("collision") flag on arbitrary values (such as Int ). Once the flag is set it is "sticky" and attaches itself to all values produced as a result of any operation with the marked one. Sometimes the collision flag is associated with individual values, sometimes I would like to associate with composite structures such as lists. Of course, once the collision flag is set for a whole list, it also makes sense to assume it is set for an

什么是“咖喱”?

梦想与她 提交于 2020-01-06 17:40:49
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我在几篇文章和博客中都看到过关于咖喱函数的引用,但是我找不到很好的解释(或者至少有一个合理的解释!) #1楼 在函数的代数中,处理带有多个自变量(或等效的一个自变量为N元组)的函数有些微不足道-但是,正如MosesSchönfinkel(以及独立的Haskell Curry)证明的那样,不需要它:所有人需要的是带有一个参数的函数。 那么如何处理自然表达为 f(x,y) 呢? 好吧,您认为这等效于 f(x)(y) f(x) ,称它为 g 是一个函数,然后将该函数应用于 y 。 换句话说,您只有带有一个参数的函数-但是其中一些函数会返回其他函数(也带有一个参数;-)。 像往常一样, 维基百科 对此有一个很好的总结条目,其中包含许多有用的指针(可能包括与您喜欢的语言有关的指针;-)以及更为严格的数学处理方法。 #2楼 我发现本文及其引用的文章对更好地了解currying很有用: http : //blogs.msdn.com/wesdyer/archive/2007/01/29/currying-and-partial-function-application.aspx 正如其他人提到的那样,这只是拥有一个参数函数的一种方式。 这很有用,因为您不必假设要传入多少个参数,因此您不需要2个参数,3个参数和4个参数函数。

Typeclass representing functions

戏子无情 提交于 2020-01-06 15:31:20
问题 Forgive me if this is a dumb question, but is there a typeclass that represents all functions? Like, lets say I have a type like this data Foo a = Thing a instance (Show a) => Show (Foo a) where show (Thing a) = show a And I want to display Thing (\x -> 1) . Maybe do some sort o reflection on the method to pretty print some metadata about this? Trying it I get the following error > Thing (\x -> 1) <interactive>:113:1: No instance for (Show (t0 -> a0)) arising from a use of `print' Possible

Typeclass representing functions

梦想与她 提交于 2020-01-06 15:30:26
问题 Forgive me if this is a dumb question, but is there a typeclass that represents all functions? Like, lets say I have a type like this data Foo a = Thing a instance (Show a) => Show (Foo a) where show (Thing a) = show a And I want to display Thing (\x -> 1) . Maybe do some sort o reflection on the method to pretty print some metadata about this? Trying it I get the following error > Thing (\x -> 1) <interactive>:113:1: No instance for (Show (t0 -> a0)) arising from a use of `print' Possible

failing cabal install MissingH and network on Windows

孤人 提交于 2020-01-06 15:08:08
问题 I just installed a new Haskell version: HaskellPlatform-8.0.1-minimal-x86_64-setup-a, I need the package MissingH, which needs the package network. However cabal install network yields: $ cabal install network Resolving dependencies... cabal: Entering directory 'C:\cygwin64\tmp\cabal-tmp-6136\network-2.6.2.1' Configuring network-2.6.2.1... configure: WARNING: unrecognized options: --with-compiler checking build system type... x86_64-unknown-cygwin checking host system type... x86_64-unknown

Converting binary to negative binary

丶灬走出姿态 提交于 2020-01-06 14:50:45
问题 I need to convert a positive binary to a negative binary. My approach seems somehow wrong. Adding an element to the end of a list does not work too. Please help me! twoComplement :: [Int] -> [Int] twoComplement (x:xs) | (x:xs) == [] = [x] | last (x:xs) == 0 = (twoComplement (init (x:xs))) ++ [1] | last (x:xs) == 1 = (twoComplement (init (x:xs))) ++ [0] as u see I'm new to haskell. 回答1: Looking on Wikipedia, there's an explanation of how to perform the two's complement conversion "From LSB to