haskell

Loop thread hangs without `hFlush stdout` even there are no `print` things

假装没事ソ 提交于 2019-12-24 12:45:22
问题 When I test some simple cases about threaded codes, I found some loop hang without hFlush stdout even it does not use any print things. import Control.Concurrent import System.IO import Data.IORef delay :: Int -> IO () delay = threadDelay . (* 1000000) wait sw = loop where loop = do v <- readIORef sw --hFlush stdout -- without this, hang if v then return() else loop monitor sw = forkIO $ loop where loop = do v <- readIORef sw print v delay 1 loop main = do sw <- newIORef False forkIO $ do

Function Composition Types

点点圈 提交于 2019-12-24 12:44:19
问题 I struggle understanding function composition type results e.g ghci> :t (id . const) (id . const) :: a -> b -> a ghci> :t ((:) . (+)) ((:) . (+)) :: a -> [a -> a] -> [a -> a] How do you guys generally derive function composition types? 回答1: Lets see that > :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c So in general: Given two functions. Consider them as curried functions of one parameter, i.e. function of type a -> b -> c -> ... -> z we'll consider as a function a -> ( b -> c -> ... -> z ) of

Creating a list of substrings of a specified length in Haskell

南笙酒味 提交于 2019-12-24 12:37:18
问题 I have written a function which should return all substrings of a specified length, n, of a string. It will be called from another function and x will always be initially 0 ( I have only written in C and a counter is all I could think of). allSubs :: Int -> Int -> String -> [String] allSubs x n s |n>x = (take n (drop x (tail s))) ++ (allSubs (x+1) n s) |otherwise = I hope the above function makes sense, in this function if the input was allSubs 0 2 "john" It should create a list ["jo","oh",

Implementing a memoization function in Haskell

主宰稳场 提交于 2019-12-24 12:34:54
问题 I'm fairly new to Haskell, and I'm trying to implement a basic memoization function which uses a Data.Map to store computed values. My example is for Project Euler Problem 15, which involves computing the number of possible paths from 1 corner to the other in a 20x20 grid. This is what I have so far. I haven't tried compiling yet because I know it won't compile. I'll explain below. import qualified Data.Map as Map main = print getProblem15Value getProblem15Value :: Integer getProblem15Value =

Running join on Maybe Relation

浪尽此生 提交于 2019-12-24 12:27:00
问题 I have a model Assignment blah Text .... and a model File assignmentId AssignmentId Maybe ... and I want to get all the files associated with an assignment in a join query. I have tried Esqueleto and runJoin with selectOneMany but haven't had any luck, so I am considering not using a join, or using rawSql. That really doesn't seem like a good idea, but I can't figure this out. Is there any support for that feature? 回答1: Update, working example: {-# LANGUAGE PackageImports, OverloadedStrings,

Converts a double space into single one anywhere in a String in Haskell

寵の児 提交于 2019-12-24 12:16:16
问题 I've been trying to complete a function that converts double space in a String into a single space in Haskell. normaliseSpace:: String -> String normaliseSpace (x:y:xs)= if x==' ' && y==' ' then y:xs else xs The problem with my code is that only converts double spaces in the beginning of a String. I suppose it has something to do with pattern matching, but as I am completely new to Haskell I really don't have an idea how to do it. Any help will be appreciated! 回答1: The reason this happens is

infinite lists, lazy evaluation and length

烂漫一生 提交于 2019-12-24 12:09:55
问题 Haskell noob here: I'm still trying to understand the mechanics of the language, so if my question is plain stupid, forgive me and point me to some link which I can learn from (I've searched awhile in similar topics here on stackoverflow, but still I can't get this). I came out with this function: chunks :: Int -> [a] -> [[a]] chunks n xs | length xs <= n = [xs] | otherwise = let (ch, rest) = splitAt n xs in ch:chunks n rest so that ghci> chunks 4 "abracadabra" ["abra","cada","bra"] ghci>

Isn't map takes a function and a list return a list?

被刻印的时光 ゝ 提交于 2019-12-24 12:03:31
问题 map2_List :: (a -> b -> c) -> [a] -> [b] -> [c] map2_List f [] _ = [] map2_List f (a:as) bs = map (f a) bs ++ map2_List f as bs This is an example from my lecture, which try to apply a binary function to all pairs of elements of two lists. The part (f a) makes me confused. Does it suppose to be a value but not a function? Then what does map value bs do? 回答1: "The part (f a) makes me confused." What is happening here is called currying and if you are coming to Haskell from imperative languages

Unwrap value from IO operation at a later time

安稳与你 提交于 2019-12-24 12:02:12
问题 Hello i was wondering how can you unwrap a value at a later time in the IO monad? If a<-expression binds the result to a then can't i use (<-expression) as a parameter for a given method eg: method (<-expression) where method method accepts the result of the evaluation? Code let inh=openFile "myfile" WriteMode let outh=openFile "out.txt" WriteMode hPutStrLn (<-outh) ((<-inh)>>=getLine) I have not entered the Monad chapter just basic <- and do blocks but i suppose it has to do with monads.

Using Haskell's Quickcheck to check transitivity

房东的猫 提交于 2019-12-24 12:01:32
问题 How can I get Quickcheck to check the transitive property in the code below? The code represents a stack of physical blocks. Blocks can only be placed on an empty table or another block using the operation MoveOnto bl b2 s , which is read as b2 is moved into and onto bl on stack or table s . My Quickcheck attempt just hangs and produces no result. import Test.QuickCheck data Block = Block Int deriving (Show,Eq) data Stack = EmptyTable | MoveOnto Block Block Stack deriving (Show,Eq) isOn ::