haskell

Should I use folds in Turtle or Foldl packages?

谁说胖子不能爱 提交于 2020-01-04 04:00:51
问题 I'm having some difficulty using Turtle and only after several minutes staring at incomprehensible error messages realized that I was using wrong fold function. https://hackage.haskell.org/package/turtle-1.5.8/docs/Turtle-Shell.html#v:fold https://hackage.haskell.org/package/foldl-1.4.0/docs/src/Control.Foldl.html#fold Why is there a name collision? I don't believe that's coincidence but I cannot figure it out. Are these inherently different kinds of folds? To be concrete, I wanted to fold a

Does Haskell/Frege ever recalcuate elements of a lazy list?

删除回忆录丶 提交于 2020-01-04 03:54:08
问题 Suppose I have a list of all primes, defined as primes :: (Enum α, Integral α) => [α] primes = sieve [2..] where sieve :: (Integral α) => [α] -> [α] sieve [] = undefined sieve (x:xs) = x : (sieve $ filter ((/= 0) . (flip mod x)) xs) and I want to feed primes through multiple, different functions like: sumOfPrimesLessThan :: (Integral α) => α -> α sumOfPrimesLessThan n = sum $ takeWhile (< n) primes or productOfPrimesLessThan :: (Integral α) => α -> α productOfPrimesLessThan n = foldl (*) 1 $

Does Haskell/Frege ever recalcuate elements of a lazy list?

╄→гoц情女王★ 提交于 2020-01-04 03:54:05
问题 Suppose I have a list of all primes, defined as primes :: (Enum α, Integral α) => [α] primes = sieve [2..] where sieve :: (Integral α) => [α] -> [α] sieve [] = undefined sieve (x:xs) = x : (sieve $ filter ((/= 0) . (flip mod x)) xs) and I want to feed primes through multiple, different functions like: sumOfPrimesLessThan :: (Integral α) => α -> α sumOfPrimesLessThan n = sum $ takeWhile (< n) primes or productOfPrimesLessThan :: (Integral α) => α -> α productOfPrimesLessThan n = foldl (*) 1 $

Real World Haskell Chapter 3 excercise: Binary Tree with 1 value constructor - follow up

倖福魔咒の 提交于 2020-01-04 02:44:05
问题 This question is not a duplicate A question with the same title already exists, but the answer only partially addressed it, in my opinion, and I am interested also in what it left unaswered. Foreword Real World Haskell proposes, in Chapter 3, page 58, the following definition for a binary tree datatype, data Tree a = Node a (Tree a) (Tree a) | Empty deriving (Show) which provides two constructors (for empty and non-empty Tree s). On the other hand, at page 60, an exercise challenges the

Haskell: why following run sequentially?

ⅰ亾dé卋堺 提交于 2020-01-04 02:42:05
问题 Good day. Given code import Control.DeepSeq import Control.Exception import Control.Parallel import Control.Parallel.Strategies import System.Environment import Text.Printf l = [34,56,43,1234,456,765,345,4574,58,878,978,456,34,234,1234123,1234,12341234] f x = Just (sum [1..x]) fun1 :: [Maybe Integer] fun1 = map f l `using` parList rdeepseq fun2 :: [Maybe Integer] fun2 = map f l `using` evalList (rparWith rdeepseq) fun3 :: [Maybe Integer] fun3 = map f l `using` evalList (rpar . force) main ::

How do you import Data.Heap?

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:20:08
问题 Sorry for such a dumb question. I'm trying to import Data.Heap, but I get this error message: > import Data.Heap <no location info>: error: Could not find module ‘Data.Heap’ Perhaps you meant Data.Map (from containers-0.5.7.1@containers-0.5.7.1) Haven't had trouble with other imports. Thanks. 回答1: As was described in the comments, you need to install the package which contains the module Data.Heap . (See this related question on the difference between packages and modules in Haskell) On the

HASKELL : Solving Towers of Hanoi

和自甴很熟 提交于 2020-01-04 02:12:12
问题 The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI and swapLID. MoveLOD: moves 1 disc from the first position to third the pin in the third position of the triplet. Additionally a string with information about the movement is piling on list of strings. type Pin = (Char, Int) -- Represents a rod, named for a character and the number of disks in it. type Plate = (Pin, Pin, Pin) -- Represents the configuration of the three rods.(Origin,Intermediate

Haskell writes '\n' instead of a newline

早过忘川 提交于 2020-01-04 02:03:28
问题 I have this code and instead of it printing out "\n", I want it to put the next string on a new line, but cannot seem to figure it out. Any pointers? onSeparateLines :: [String] -> String onSeparateLines [] = "" onSeparateLines ( x:[] ) = x onSeparateLines ( x:xs ) = x ++ "\n" ++ onSeparateLines xs what I get is "AAAA\nAAAA" which should be: "AAAA" "AAAA" 回答1: The given function and your use of "\n" are correct, so the error must be elsewhere. Without knowing the details, I suspect that you

How can I have Show display the name of a function?

混江龙づ霸主 提交于 2020-01-04 01:56:08
问题 As a simple exercise to get me acquainted with Haskell, after idling around on Youtube and stumbling into the American Countdown game show, I wanted to make a solver for the Numbers game. You get 6 numbers and need to combine them with (+) (-) (*) (/) in order to get a given result. What I've got so far is the very brain-dead, let operands = [75, 2, 6, 3, 8, 7] :: [Double] let goal = 623 :: Double let operations = [(+), (-), (*), (/)] show (head [(a, x, b, y, c, z, d, t, e) | a <- operands, b

Combining multiple functions

扶醉桌前 提交于 2020-01-04 01:55:47
问题 I'm trying to make a DNA transcription program but I'm having trouble with the way I'm doing it, I'm sure there's an easier way to do this but this was the first thing that came to my head but it's not working the way I want it to. dnaToRna :: [Char] -> [Char] dnaToRna [] = [] dnaToRna xs = reverse(transc xs) where transc = (replaceA . replaceT . replaceC . replaceG) replaceA = map(\c -> if c == 'A' then 'U' else c) replaceT = map(\c -> if c == 'T' then 'A' else c) replaceC = map(\c -> if c =