Questions tagged [haskell]

26015 questions
1

votes
2

answer
213

Views

Operator priority in haskell

I have a function g1 = flip map . (flip take $ iterate (+1) 1) I am wondering why i can't write g1 = flip map . $ flip take $ iterate (+1) 1 ? I know that $ has lowest priority but don't understand why we can have $ in flip take $ iterate (+1) 1 but not in g1 after the dot
user2975699
1

votes
2

answer
295

Views

Remove an item from a list in Haskell

How can I delete an item from a list in Haskell? I've got this code but it doesn't seems to work. The concept its, I've got 2 lists, the aList will always have just one element. bList will always have the element that its in aList. So I want to delete the element from bList and at the end, output th...
Bobys
1

votes
1

answer
332

Views

How to get Haskell Chart example1 working for Diagrams backend?

The example outputs for Haskell's chart library look great. They all use the Cairo backend which I cannot install properly, so I'd like to try out the library using the Diagrams backend. However, it is not obvious to me how to do this. In particular, def is undefined. Can someone help me get started...
Ana
1

votes
1

answer
240

Views

Why does GHCi complaining at where clause?

When I write a code like this: Prelude> let f = (+1) . (*k) Prelude| where k = 2 Prelude| I'm having such an error: :46:5: parse error on input 'where'
4

votes
2

answer
114

Views

Functional programming: How to convert an implementation of fibonacci written in python to haskell

So, for this task I have to convert python to haskell. The code I have to implement in haskell is a code that generates a fibonacci sequence using a top down iteration method. The problem is that i'm fairly new at haskell, and I don't quite know how to execute this problem. I have created a while-l...
teekinator359
1

votes
1

answer
713

Views

Haskell - Elapsed days between two dates

I'm working on a small haskell program that I gave the name the "Haskel-dator", hahaha....anyway, heres my current progress on the code for it, and the run-time errors that followed: Code data Date = Date Int Int Int type Date = (Int,Int,Int) sum :: Int -> Int -> Int sum x y | let x - y = z | w...
yamis7190
1

votes
1

answer
156

Views

Reusing sinkFile with Haskell Conduit

I want to send a list of Photos to a sink to save them on the disk. Each Photo structure contains a content field of type lazy ByteString. Basically I want to do something like that: CL.sourceList photos $$ sinkPhotos I would like to reuse CB.sinkFile in my sink since, if I understand correctly, it...
z1naOK9nu8iY5A
0

votes
1

answer
25

Views

How to convert Maybe a to a or -1 (for nothing)?

I'd line to convert Just a to a and Nothing to -1 where a is an Int. My function is: --Converts Just a to a && Nothing to -1 justToIntOnly :: (Integral a) => Maybe a -> a justToIntOnly Just a = a justToIntOnly Nothing = -1 The error i get is: "Equations for `justToIntOnly' have different numbers of...
FabolousPotato
1

votes
1

answer
241

Views

Rounding a float to an int in haskell

Problem 3 of Project Euler says: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? I made a lazy list of primes, then I do a takeWhile they are less than the square root of 600851475143, and test each prime to see if it is a factor. primes :...
Mark Karavan
4

votes
0

answer
42

Views

Create a new expression without pointer from previous one

I am reading the book https://www.packtpub.com/application-development/haskell-high-performance-programming and trying to figure out, what is the difference between those two functions: This functions does memoize the intermediate numbers: fib_mem :: Int -> Integer fib_mem = (map fib [0..] !!) wher...
zero_coding
1

votes
1

answer
382

Views

Hspec: discovery, custom main, and passing argument to spec

I am trying to use hspec-discover along with custom Main. Custom Main is a bracket that creates a file descriptor to be used by all Spec's. This is my Spec.hs: {-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-} This is my Main.hs: module Main (main) where import Control.Exception i...
crosser
1

votes
1

answer
90

Views

Simple Recursion in Haskell

I am trying to understand recursion in Haskell. I want to write a simple recursive function with the following properties: if x > 50 then rec(x) = x - 5 and rec(x) = rec(rec(x+10)) otherwise. This is what I came up with: rec x = if x > 50 then x-5 else rec (rec (x+10)) The not recursive part seems t...
user53073
1

votes
1

answer
330

Views

Why can't I import this Haskell module/package?

I installed the SourceGraph package using cabal: cabal install SourceGraph It was installed perfectly without any errors. Now, I've created a file called Test.hs to test if I am able to import this package and use it. These are the contents of Test.hs: module Test where import SourceGraph main = p...
John Red
-1

votes
4

answer
90

Views

What does % do in Haskell?

I'm used to using % to mean "modulo" in other languages. In Haskell, we have to use mod x y or x `mod` y. So, what is this symbol used for in Haskell?
Ryan
1

votes
1

answer
108

Views

Haskell vector type declaration

I'm writing a comparator to pass to sortBy but I can't get the type declaration right. The input is two Data.Vector's, each containing two numbers. -- Comparator to sort a list of individuals by increasing order of fit-0 -- and for individuals with equal fit-0, with increasing order of fit-1 i...
tsorn
1

votes
0

answer
19

Views

'Infinite' precision reciprocal in Haskell

I need to generate an infinite Haskell list containing all bits (or words) from the fractional part of the reciprocal of an Integer, in MSB-first order. Is there a straightforward way to do this from standard libraries, or do I need to implement a Newton iteration function or similar? I considered u...
Mick Francis
0

votes
0

answer
7

Views

Calling functions on a state monad “Stack” in Haskell

I am studying in a Haskell course right now and there is a task which I am not able to complete. We have the following types given by the task newtype Trans state a = T {run :: state -> (a,state)} type Stack a = Trans [Int] a What I want is to write a function 1. push to put an Integer on the stack...
Jan v
0

votes
0

answer
37

Views

Flattening a binary tree in a specific manner

Consider the following definitions of binary and unary trees, a function flatten, which converts binary and unary trees to lists (e.g, flatten (Node (Leaf 10) 11 (Leaf 20)) is [10,11,20]) and a function, reverseflatten, which converts lists to binary trees (in the specific manner described here (Def...
user65526
1

votes
2

answer
110

Views

How to generate a table in Haddock documentation

I am writing some documentation with Haddock, and I need to put a multi-column table with some values in it. How can I do it with Haddock? I cannot find info about it. Embedding some html as alternative looks no possible too.
Randomize
1

votes
2

answer
111

Views

Why is `mfix` not total in `MaybeT`

The transformers implementation of MonadFix for MaybeT fails if the function ever evaluates to Nothing. Why is Nothing not propagating over mfix? mfix' :: MonadFix m => (a -> MaybeT m a) -> MaybeT m a mfix' f = MaybeT $ mfix $ \case Nothing -> return Nothing Just x -> runMaybeT $ f x There must be a...
fakedrake
4

votes
2

answer
29

Views

How binding works in function monads in Haskell?

From learn you a haskell: http://learnyouahaskell.com/for-a-few-monads-more Monad instance for function is this: instance Monad ((->) r) where return x = \_ -> x h >>= f = \w -> f (h w) w I am having trouble understanding the output of the following: import Control.Monad.Instances addStuff ::...
Md Imran Pavel
1

votes
0

answer
15

Views

Haskell - Is there a better way of evenly distributing an element over a list

Given a matrix like this matrix_table = [[ 0, 0, 0, 0] ,[ 0, 0, 0, 0] ,[ 0, 0, 0, 0] ,[ 0, 0, 0, 0] ] and a list position_list = [2, 3, 2, 10] output of a function distribute_ones :: [[Int]] -> [Int] -> [[Int]] distribute_ones matrix_table position_list should look like this [[ 0, 1, 0, 1] -- 2 '1'...
iovo
2

votes
1

answer
42

Views

is this piece of haskell code correct, if so why?

the haskell wiki (here : https://wiki.haskell.org/State_Monad ) says the state monad bind operator is defined like this : (>>=) :: State s a -> (a -> State s b) -> State s b (act1 >>= fact2) s = runState act2 is where (iv,is) = runState act1 s act2 = fact2 iv however it seems incorrect to me as the...
Azerty Poit
0

votes
0

answer
5

Views

Errors involving expected types

The following piece of code (which is taken from Graham Hutton's "Programming in Haskell" (Chapter 9)) Prelude> :{ Prelude| data Expr = Val Int | App Op Expr Expr Prelude| instance Show Expr where Prelude| show (Val n) = n Prelude| show (App o l r) = brak l ++ show o ++ brak r Prelude|...
user65526
1

votes
1

answer
36

Views

Benchmark in GHCi, how can I enable to show number of reductions for expression?

I am playing with GHCi and I wondering how can I enable in GHCi to raise the number of reduction in expression to compare different solutions?
mkUltra
0

votes
0

answer
2

Views

Stack.yaml vs package.yaml for adding dependencies

Possibly a dumb question: The Stack documentation mentions package.yaml as the right place to add dependencies, but all I see in my project directory is a single stack.yaml file; is that the right place, then? (I see an existing question that asks about redundancy between these two files, but I don'...
agam
8

votes
4

answer
143

Views

DataKind Unions

I'm not sure if it is the right terminology, but is it possible to declare function types that take in an 'union' of datakinds? For example, I know I can do the following: {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} ... data Shape' = Circle' | Square' | Triangle' data Shape :: Sh...
Allan W
-2

votes
0

answer
13

Views

How to list pairs of primes in increasing order of their sum in Haskell

As a learning exercise, I've been using Haskell to solve a few Project Euler problems. I would like to write a function in Haskell which outputs a list of pairs of distinct prime numbers, such that each pair of prime numbers is included in the list, and the list elements are increasing in the sum of...
Ben
2

votes
2

answer
41

Views

Zipping two HashMaps

Let's say I have two HashMaps: import Data.HashMap.Strict m1 :: HashMap Char Int m2 :: HashMap Char Int ??? :: (v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> HashMap k v3 -- or even ??? :: (k -> v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> HashMap k v3 I'd like some kind of zip to walk ov...
Norrius
1

votes
2

answer
45

Views

Filter haskell list based on another list

Let's say I have two lists: l1 = [True, False, True] l2 = [1, 2, 3] I want the result of my function to be [1, 3], as they map to True in the other list This is what I have thought of: map2 [] [] res = res map2 (o:os) (x:xs) res = if o then map2 (os xs (res ++ [x])) else map2 (os xs (res ++ [])) We...
K Split X
0

votes
0

answer
3

Views

Polymorphic type family instance in Haskell

I'm trying to write code to simulate random variables, and I want to keep things as polymorphic as possible. This may involve the use of type families, which are completely new to me. Here is a simplified version of my code: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE InstanceSigs #-} {-# LANGUA...
Jeremy Silver
2

votes
3

answer
69

Views

What is the difference between MultiWayIf and classic guards?

What does MultiWayIf extension offer that cannot be done using | guards and vice versa? For instance, one can write same function as f x | x == 0 = -1 | otherwise = x or f x = if | x == 0 -> -1 | otherwise -> x I don't see any difference between these two examples beside syntactic sugar used. Are t...
radrow
4

votes
2

answer
112

Views

Haskell - Recursion Stack Overflow

I am trying to sum all n from 1 to a very large number (10**9 for now) but it gives stack overflow. Also I don't think putting a stop at 1 and doing the sum n in different rows is the most efficient way but the code below is all my knowledge for Haskell. I really don't know much about functional pro...
Terobero
1

votes
0

answer
24

Views

What is the Purpose of the Item type in Foundation

Both the following compile fine with Foundation (and Prelude as qualified import P) and return the expected string. -- the inferred type blahhh :: Item [Char] blahhh = P.head $ toList ("blahh de blahh de blahh" :: [Item [Char]]) blahh2 :: [Char] blahh2 = blahhh : ": Should be a b" and blahhh :: Char...
John Walker
1

votes
0

answer
15

Views

Using GHC to compile Core

I want to write a compiler from another language to produce terms in Haskell Core, and call GHC on the result. Are there any methods to do this? The compiler is not written in Haskell, so it would have to communicate via a serialization format or FFI. I have read about .hcr files for getting core fr...
Mario Carneiro
5

votes
0

answer
41

Views

Explicit forall on a type class function

Since ghc-8.0 we have a very nice extension called TypeApplications. Which allows us instead of: λ> show (5 :: Int) "5" do so something like that: λ> :set -XTypeApplications λ> show @Int 5 "5" Which is really cool. It becomes a bit more involved when we add more type variables, but there are rule...
lehins
0

votes
3

answer
112

Views

Is Bool -> Maybe () isomorphic?

I am trying to figure out, if Bool -> Maybe () is isomorphic or not. The possible combination are: True -> Just () True -> Nothing False -> Just () False -> Nothing I would say, it is isomorphic and for every combination, it exists seperate functions to inverse it. How to prove that the above...
zero_coding
1

votes
1

answer
160

Views

Haskell gloss IO textures

I am making a game, for which i use textures. These textures are loaded in in Gloss from BMP files and give a IO Picture. Now i have a list of IO Pictures, which i want to render. The render takes a picture, and you can compose multiple pictures into one, with pictures Now Render :: IO() texture1...
BaasBartMans
0

votes
1

answer
29

Views

List of pairs into pair of Lists Haskell

Basically I have this exercise: Using list comprehensions, write a polymorphic function: split :: [(a, b)] -> ([a], [b]) which transforms a list of pairs (of any types) into a pair of lists. For example, split [(1, 'a'), (2, 'b'), (3, 'c')] = ([1, 2, 3], "abc") This was the way I wrote the function...
Nicekor
1

votes
2

answer
115

Views

Index a Heterogeneous List

I built a heterogeneous List in Haskell using some type level programming. data HList a where Singleton :: HList '[] Cons :: h -> HList t -> HList (h ': t) Now I would like to be able to index this list, however there are some problems with types that are making this very difficult for me. I can ge...
W W

View additional questions