haskell

How can I determine if one Enum value is the successor of another?

╄→尐↘猪︶ㄣ 提交于 2020-05-28 20:49:10
问题 I'm trying to write a function that tells me whether one Enum is the successor of another. Here was my first attempt: isSuccessorOf x y = x == succ y Looks reasonable. Let's try it: λ> isSuccessorOf 3 2 True λ> isSuccessorOf 1 5 False λ> isSuccessorOf 3 (maxBound :: Int) *** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound Whoops. That should have been False . Let's make sure we don't try to do succ maxBound : isSuccessorOf x y = y /= maxBound && x == succ y Let's try it

iterating through a list in haskell

我的梦境 提交于 2020-05-26 16:26:41
问题 I have a list of list of characters ::[[Char]] . I need to iterate both over the list of strings and also over each character in each string. Say, my list is present in this variable. let xs Please suggest an easy way to iterate. 回答1: If you want to apply a function f to every element of a list like this: [a, b, c, d] → [f a, f b, f c, f d] then map f xs does the trick. map turns a function on elements to a function on lists. So, we can nest it to operate on lists of lists: if f transforms a

“resource busy (file is locked)” error in Haskell

心不动则不痛 提交于 2020-05-26 10:29:10
问题 I'm very new to Haskell. In fact, I'm working through this section of this tutorial. I came across this piece of code: import System.IO import Data.Char main = do contents <- readFile "girlfriend.txt" writeFile "girlfriendcaps.txt" (map toUpper contents) Which reads the contents of the file called "girlfriend.txt" and writes the upper-cased version of the file to a new file called "girlfriendcaps.txt". So, I wanted to modify the code a bit to take the name of the file to act on. I changed the

How does <*> derived from pure and (>>=)?

烈酒焚心 提交于 2020-05-26 03:30:06
问题 class Applicative f => Monad f where return :: a -> f a (>>=) :: f a -> (a -> f b) -> f b (<*>) can be derived from pure and (>>=) : fs <*> as = fs >>= (\f -> as >>= (\a -> pure (f a))) For the line fs >>= (\f -> as >>= (\a -> pure (f a))) I am confused by the usage of >>= . I think it takes a functor f a and a function, then return another functor f b . But in this expression, I feel lost. 回答1: Lets start with the type we're implementing: (<*>) :: Monad f => f (a -> b) -> f a -> f b (The

Difference between where bindings, let bindings and the single assignment operator (<-)

江枫思渺然 提交于 2020-05-24 21:22:27
问题 I do not understand the difference between the three syntaxes: where a = f (b) do a <- f (b) do let a = f (b) I do understand somehow though that a <- f(b) is different from the other two, in most cases where I tried all three worked. Also I read somewhere on the net that per block you should try to get along with one let binding only in order to be "idiomatic". But I never seem to manage. How do I decide what to use? 回答1: let foo = bar in ... simply defines foo to be the exact same thing as

Use cases for functor/applicative/monad instances for functions

被刻印的时光 ゝ 提交于 2020-05-24 16:37:08
问题 Haskell has Functor , Applicative and Monad instances defined for functions (specifically the partially applied type (->) a ) in the standard library, built around function composition. Understanding these instances is a nice mind-bender exercise, but my question here is about the practical uses of these instances. I'd be happy to hear about realistic scenarios where folks used these for some practical code. 回答1: A common pattern that involves Functor and Applicative instances of functions is

Use cases for functor/applicative/monad instances for functions

十年热恋 提交于 2020-05-24 16:35:12
问题 Haskell has Functor , Applicative and Monad instances defined for functions (specifically the partially applied type (->) a ) in the standard library, built around function composition. Understanding these instances is a nice mind-bender exercise, but my question here is about the practical uses of these instances. I'd be happy to hear about realistic scenarios where folks used these for some practical code. 回答1: A common pattern that involves Functor and Applicative instances of functions is

What is the most production-level Haskell to JavaScript compiler, to write code running in the browser? [closed]

≯℡__Kan透↙ 提交于 2020-05-24 08:14:08
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I am not looking for a necessarily super-robust solution with a 10-year track record, but for something that can be used in a real applications, and goes beyond just being able to run an Hello World example. My preference is to run the compiler on the server, so I can compile Haskell

How to use variable from do block assignment line in a where clause?

孤人 提交于 2020-05-23 13:01:40
问题 I have the following sample of code: {-# LANGUAGE ScopedTypeVariables #-} main = do putStrLn "Please input a number a: " a :: Int <- readLn print a putStrLn "Please input a number b: " b :: Int <- readLn print b putStrLn ("a+b+b^2:" ++ (show $ a+b+c)) where c = b^2 For some reason I cannot use variable b in a where clause, the error I get is the following: Main3.hs:13:15: error: Variable not in scope: b | 13 | where c = b^2 | ^ Any ideas how to make b available in the where clause? 回答1: Use

What exactly is the kind “*” in Haskell?

我怕爱的太早我们不能终老 提交于 2020-05-20 11:14:09
问题 In Haskell, (value-level) expressions are classified into types , which can be notated with :: like so: 3 :: Int , "Hello" :: String , (+ 1) :: Num a => a -> a . Similarly, types are classified into kinds . In GHCi, you can inspect the kind of a type expression using the command :kind or :k : > :k Int Int :: * > :k Maybe Maybe :: * -> * > :k Either Either :: * -> * -> * > :k Num Num :: * -> Constraint > :k Monad Monad :: (* -> *) -> Constraint There are definitions floating around that * is