haskell

Why does Haskell's `head` crash on an empty list (or why *doesn't* it return an empty list)? (Language philosophy)

折月煮酒 提交于 2020-01-28 13:20:25
问题 Note to other potential contributors: Please don't hesitate to use abstract or mathematical notations to make your point. If I find your answer unclear, I will ask for elucidation, but otherwise feel free to express yourself in a comfortable fashion. To be clear: I am not looking for a "safe" head , nor is the choice of head in particular exceptionally meaningful. The meat of the question follows the discussion of head and head' , which serve to provide context. I've been hacking away with

Shorthand way for assigning a single field in a record, while copying the rest of the fields?

僤鯓⒐⒋嵵緔 提交于 2020-01-28 13:16:29
问题 Let's say I have the following record ADT: data Foo = Bar { a :: Integer, b :: String, c :: String } I want a function that takes a record and returns a record (of the same type) where all but one of the fields have identical values to the one passed as argument, like so: walkDuck x = Bar { a = a x, b = b x, c = lemonadeStand (a x) (b x) } The above works, but for a record with more fields (say 10 ), creating a such function would entail a lot of typing that I feel is quite unnecessary. Are

Nice representation of primitive recursive functions in haskell

安稳与你 提交于 2020-01-28 09:56:08
问题 I argued in the answer to a previous question that it's possible to represent in Haskell the union of the primitive recursive functions (PRFs) and the single extra value of ⊥ or undefined . This argument was based on a direct translation of an axiomatic construction of primitive recursive functions; it required a number of language extensions and type-level reasoning about the arity of functions. Is it possible to represent an equivalent set of primitive recursive functions in more idiomatic

Huffman Tree decoding

我怕爱的太早我们不能终老 提交于 2020-01-26 04:58:26
问题 Given a Huffman tree and a stream of bits, return a pair containing (1) the -- string of symbols encoded by the bits (according to the Huffman tree), and -- (2) a Bool indicating whether the output stream contains every bit from the -- input (that is, return False if there were any bits left over). Here is the code, it only returns the first symbol in the tree. What's the problem? data BTree a = Leaf a | Fork (BTree a) (BTree a) deriving (Show, Eq) traT :: BTree a -> BTree a -> [Bool] -> [a]

How do I combine two list in Haskell?

那年仲夏 提交于 2020-01-26 04:50:47
问题 I have the following two lines of code. I've tried merge and append but keep getting errors of type. The goal is to have one list that's BOTH a multiple of 5 or has exactly three factors. For example : (function name) 100 --> [4,5,9,10,15,20,25,30,35,40,45,49,50,55,60,65,70,75,80,85,90,95,100] My two lines of code are : mulitof5 x = [x | x <- [1..x], (x `mod` 5 == 0)] isPrimes n = [n^2 | (n) <- [2..n-1], (all (\a -> mod n a /= 0) [2..n-1])] Are merge and append the way to go? Things I've

How do I combine two list in Haskell?

拜拜、爱过 提交于 2020-01-26 04:50:26
问题 I have the following two lines of code. I've tried merge and append but keep getting errors of type. The goal is to have one list that's BOTH a multiple of 5 or has exactly three factors. For example : (function name) 100 --> [4,5,9,10,15,20,25,30,35,40,45,49,50,55,60,65,70,75,80,85,90,95,100] My two lines of code are : mulitof5 x = [x | x <- [1..x], (x `mod` 5 == 0)] isPrimes n = [n^2 | (n) <- [2..n-1], (all (\a -> mod n a /= 0) [2..n-1])] Are merge and append the way to go? Things I've

Haskell Syntax: Parse Error On Input

↘锁芯ラ 提交于 2020-01-26 01:18:10
问题 As part of a mini-haskell compiler that I'm writing, I have a function named app . What I want this function to do is take in these arguments epp (App e1 e2) . The first step would be to evaluate e1 recursively ( epp e1 ) and check if the output would be an error. If not then evaluate e2 and then call another function eppVals to evaluate the outputs of the calls on e1 and e2 which I defined as v1 and v2 respectively. 回答1: In your hpaste you have a function appVals which has been renamed to

Clumsy Looking Type Signature for ByteStrings in Haskell HTTP Response

↘锁芯ラ 提交于 2020-01-26 01:10:54
问题 I'm experimenting with the http-conduit library and have this simple example: #!/usr/bin/env stack {- stack --resolver lts-7.12 --install-ghc runghc --package http-conduit -} {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Conduit import Data.ByteString.Lazy.Internal getUrl :: IO (Data.ByteString.Lazy.Internal.ByteString) ---eeew! getUrl = do resp <- simpleHttp "http://www.stackoverflow.com" return resp I understand from this post that I should prefer a response as a ByteString to a

basic haskell : Copying elements

岁酱吖の 提交于 2020-01-25 10:27:08
问题 I'm trying to recursively call a function that copies any type a specified amount of times for example copy 3 'a' would give me ['a','a','a'] while copy 3 2 would give me [2,2,2] This is what I have so far but I'm not sure if my typeline is correct as I think my code should run fine. Can anyone see what's wrong? copy :: Int->a->[a] copy x [] = [] copy y a = a:(copy (y-1) a) edit: updated to this: copy :: Int->a->[a] copy 0 a = [] copy y a = [a]++(copy (y-1) a) However this gives me "aaa"

`fail “zero”` as a way to generate Nothing in simple `a -> Maybe a` example

…衆ロ難τιáo~ 提交于 2020-01-25 09:16:20
问题 I'm reading a revealing example of using a bind operator: Just 5 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) returns Just 6 . I'm confused by the behaviour of fail and its usefulness in the example. When looking at the code I thought fail "zero" may have a meaning: program never gets to that point laziness something else. Then I realised that after a type cohesion, an exception becomes Nothing (documented here). Still confusing for me that without type enforcement fail is