haskell

Haskell - Do literal backslashes always have to be escaped in a string?

In Haskell, in order to represent the literal string "\", one would normally write: "\\" However, is there a way to escape the string such that a single backslash can be written by itself without needing to be escaped? For example, I can do exactly this in C# by pre-pending @ to the string: @"\" Does Haskell have an equivalent? ...

Haskell - Currying? Need further explanation.

So something like addList :: [int] -> int addList = foldl1 (+) Why does this work? The Currying part. Why no variable? Thanks ...

-package Cabal-1.8.0.6 breaks pango installation on Mac

When installing Haskell pango on mac (sudo cabal install pango), the first problem is that it tries to install packages I've already installed. I've install haskell98 (successfully), but pango tries to install it again, and when the compilation is complete it gives this error: Registering haskell98-1.0.1.1... Installing library in /User...

Haskell - Create Set (unique sorted list) - no recursion, no nub

is it Possible to create a function that will create a set with an input of a list. I just can't think of any way without using recursion. I can use high order functions like fold, filter, map, zip. I just can't have recursion in my function. Obviously i can't use nub. I've been banging my head trying to figure out how to get rid of...

Haskell: Why do the Maybe and Either types behave differently when used as Monads?

I'm trying to get my head around error handling in Haskell. I've found the article "8 ways to report errors in Haskell" but I'm confused as to why Maybe and Either behave differently. For example: import Control.Monad.Error myDiv :: (Monad m) => Float -> Float -> m Float myDiv x 0 = fail "My divison by zero" myDiv x y = return (x / y)...

GHCi usage question

I am studying Haskell and use Emacs+Haskell mode as my editor. After playing some simple expressions in GHCi, I am wondering whether these IDE/editor functionality that exist in Visual Stuido for F#: Can I send the content in the clipboard into the interpreter? Currently I can only :load the file into the interpreter. This is inconv...

Integer time complexity in Haskell

Hello! I had an assignment in school last week to implement a function for calculating the n:th number in the fibonacci sequence. A 'sub-assignment' was to implement it using accumulation(Might not be a correct translation) in order to give the function O(n) time complexity. This all worked fine until I tried making the function (Int ->...

Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function?

I'm working my way through the book The Haskell Road to Logic, Maths and Programming. (I'm only mid-way through chapter 1, but I'm enjoying it so far and intend to continue.) I've read through the section 1.5 "Playing the Haskell Game" which "consists of a number of further examples to get you acquainted with [Haskell]". So far I've l...

Haskell - wildcard use on right side of guard in patterns

Lets say i have a piece of code like this: test pattern | pattern == (_,NOT (WIRE _)) = 1 | pattern == (_,AND (WIRE _) (WIRE _)) = 2 | otherwise = 0 Where i am trying to match it against one of several possibilities, some with one (WIRE ""), some with two. I have actual input as follows e.g.: ("p",NOT (WIRE "x")). I would like...

In Haskell, why non-exhaustive patterns are not compile-time errors?

This is a follow-up of http://stackoverflow.com/questions/3799359/why-am-i-getting-non-exhaustive-patterns-in-function-when-i-invoke-my-haskel I understand that using -Wall, GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not making it a compile-time error by default given that it's always possible ...

Reverse data constructor

How can I define a function that will accept my type and return its primitive "synonym"? For example: newtype MyInt = MakeInt Int And i want a function: unMyInt :: MakeInt -> Int ...

Haskell's if statement for error checking

I wrote a function for the Haar wavelet transformation given that the input is a List with a power of 2. I was trying to error check by making sure that the length of the List is a power of 2 before preforming the transformation. I am comparing the log base 2 of the length of the list to see if it comes out evenly (nothing to the right o...

Haskell - get items that occur only once

Im trying to figure out how to create a new list that has only the elements that occur only once. I can't use recursion either. This is a small part of a bigger function. Im trying to write a function to get the Intersect of two sets. Basically i combined the sets. Then sorted. Then i want to combine that with set B and get rid of all t...

basic haskell control statements - statement that always runs possible?

If i wanted to do the following: function1 stuff | condition1 = "yay" | condition2 = "hey" | condition3 = "nay" and i wanted to have a statement that always ran, unconditionally, (e.g. "because"), how could i do this? The strings are placeholders. Thanks! Edit: Before i give up on this, i want to try and rephrase it in terms of ...

How do I get the OverloadedStrings language extension working?

I've enabled overloaded strings, but I can't get them to work: $ cat overloadedstrings.hs {-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString as B import qualified Data.ByteString.Lazy as BL lazy :: BL.ByteString lazy = "I'm a lazy ByteString" strict :: B.ByteString strict = "I'm a strict ByteString" $ ghci GHCi, ve...

Represent sequence of tetrahedral numbers in Haskell

I've been wanting to learn some Haskell for a while now, and I know it and similar languages have really good support for various kinds of infinite lists. So, how could I represent the sequence of tetrahedral numbers in Haskell, preferably with an explanation of what's going on? 0 0 0 1 1 1 2 3 4 3 6 10 4 10 20 5 1...

using Hood with Show instance

How can I use any user data type which derives from Show with Debug.Observe.observe? data MyData = Foo | Bar deriving (Show) myFunc = consumMyData . observe "Debug: " . produceMyData consumMyData :: MyData -> ... produceMyData :: ... -> MyData main = runO $ myFunc So I can avoid writing: instance Observable MyData where observe...

parser for Data.ByteString.Lazy.Char8 in Haskell ?

Hi i'm facing the following problem, i have to re-write an existant code to improve his performances. the old version was using a parser defined like this : newtype Parser Char a = Parser {runParser :: [Char] -> [(a,[Char])]} to parse lines from files. but it was too slow and required a lot of memory to achieve the computation upon l...

Haskell - Concat a list of strings

Im trying to create a list of strings using some recursion. Basically i want to take a part of a string up to a certain point. Create a list from that and then process the rest of the string through recursion. type DocName = FilePath type Line = (Int,String) type Document = [Line] splitLines :: String -> Document splitLines [] = [] s...

Haskell split lines into list including empty lines

So have this type DocName = FilePath type Line = (Int,String) type Document = [Line] splitLines :: String -> Document splitLines [] = [] splitLines str = zip [0..(length listStr)] listStr where listStr = [getLine] ++ map snd (splitLines getRest) ...