haskell

Should I learn Haskell or F# if I already know OCaml?

I am wondering if I should continue to learn OCaml or switch to F# or Haskell. Here are the criteria I am most interested in: Longevity Which language will last longer? I don't want to learn something that might be abandoned in a couple years by users and developers. Will Inria, Microsoft, University of Glasgow continue to support...

Haskell: Printing out the contents of a list of tuples

Basically what I need to do is write a function that takes in a list of type [(String,String)] and print out the contents so that, line-by-line, the output looks like this: FirstString : SecondString FirstString : SecondString ..etc, for every item in the list. I've got the following code and it prints it out, but for some reason it p...

Can 2 or more equations defining a function in Haskell share the same where / let block?

Can 2 or more equations defining a function in Haskell share the same where / let block? Let me present a contrived example to illustrate the question. First, consider the following code as a starting point: someFunction v1 v2 v3 = difference ^ v3 where difference = v1 - v2 So far, so good. But then, imagine I need to deal...

Guidelines for applying DRY in Haskell function definitions

I have a question about whether or not a specific way of applying of the DRY principle is considered a good practice in Haskell.I'm going to present an example, and then ask whether the approach I'm taking is considered good Haskell style. In a nutshell, the question is this: when you have a long formula, and then you find yourself needi...

How do I install nano-hmac on Mac OS X?

"nano-hmac" is a Haskell package which can normally be installed using Cabal. However, Mac OS X has a different OpenSSL library than the one it expects, so it fails to compile bindings. I can install the version of openssl from openssl.org, but I'm not sure how to instruct Cabal to use this instead of the Mac OS X version. ...

Haskell: Writing text files and parsing them back to original format

Hi - I have a list of tuples of format [(String,String)] and I need a function to write the contents of the list to a text file, then another function to read this text file in as the same list of tuples. Here's what I've got for the saving function: save :: Table -> IO() save [] = writeFile "database.txt" "" save zs = do { writeFile "d...

List of Functional code snippets for Procedural Programmers?

Sometimes I still get stuck trying to translate procedural code into functional code. Is there a list of functional idioms/snippets that are mapped to procedural idioms/snippets? Edit Since there doesn't seem to be a centralized website of these snippets, I am turning this into a community wiki. Please paste any procedural -> function...

Haskell "do nothing" IO, or if without else

I want to do something in Haskell that looks like this: main1 = do s <- getLine if s == "foo" then putStr "You entered foo" Obviously this isn't legal since there's no else. One alternative I've thought of: nop :: IO () nop = sequence_ [] main2 = do s <- getLine if s == "foo" then putStr "You entered foo" else ...

Is it recommended to always have exhaustive pattern matches in Haskell, even for "impossible" cases?

Is it recommended to always have exhaustive pattern matches in Haskell, even for "impossible" cases? For example, in the following code, I am pattern matching on the "accumulator" of a foldr. I am in complete control of the contents of the accumulator, because I create it (it is not passed to me as input, but rather built within my func...

Why does this Haskell code work successfully with infinite lists?

I have some Haskell code that does work correctly on an infinite list, but I do not understand why it can do so successfully. (I modified my original code -- that did not handle infinite lists -- to incorporate something from some other code online, and suddenly I see that it works but don't know why). myAny :: (a -> Bool) -> [a] -> Bo...

Please explain in the simplest, most jargon-free English possible, the "universal property of fold"?

I am working through "Real World Haskell", which led to to a free PDF called "A tutorial on the universality and expressiveness of fold". It makes the point that a "fold" is "universal". I am wrestling with his definition of "universal", and would like to hear the from those who have already invested time digesting it: Please explain in ...

How do you combine filter conditions

The filter class of functions takes a condition (a -> Bool) and applies it when filtering. What is the best way to use a filter on when you have multiple conditions? Used the applicative function liftA2 instead of liftM2 because I for some reason didn't understand how liftM2 worked within pure code. ...

How do I create two mutual producer/consumers with internal state in Haskell?

I've got an agent that takes in states and returns actions, while keeping an internal representation of the utility of state/action pairs. I've also got an environment that takes in actions and returns state/reward pairs. I need to be able to set the agent up with a start state and then continuously go from agent -(action)-> environment...

compare string contents in haskell

I have a 2 lists of strings eg: listx = ["name","age","rank"] input = ["name","age"] How can I compare the two lists to check whether the listx contains "name" & "age" given in input? ...

How to print a list of strings using 'unwords' in Haskell?

I want to print list of strings like shown below. |Name|Country|Age| ------------------ |1 |USA |20 | |2 |UK |19 | I was able to achieve this using the following. printfieldName :: [String] -> String printfieldName [] = [] printfieldName (x:xs) = "|" ++ x ++ "\t" ++ printfieldName (xs) Is it possible to achieve this usi...

Why does this first Haskell function FAIL to handle infinite lists, while this second snippet SUCCEEDS with infinite lists?

I have two Haskell functions, both of which seem very similar to me. But the first one FAILS against infinite lists, and the second one SUCCEEDS against infinite lists. I have been trying for hours to nail down exactly why that is, but to no avail. Both snippets are a re-implementation of the "words" function in Prelude. Both work fine...

how do i translate this Haskell to F#?

I'm trying to learn F# by translating some Haskell code I wrote a very long time ago, but I'm stuck! percent :: Int -> Int -> Float percent a b = (fromInt a / fromInt b) * 100 freqs :: String -> [Float] freqs ws = [percent (count x ws) (lowers ws) | x <- ['a' .. 'z']] I've managed this: let percent a b = (floa...

Why are Haskell algebraic data types "closed"?

Correct me if I'm wrong, but it seems like algebraic data types in Haskell are useful in many of the cases where you would use classes and inheritance in OO languages. But there is a big difference: once an algebraic data type is declared, it can not be extended elsewhere. It is "closed". In OO, you can extend already defined classes. Fo...

Is this a correct way of writing the Haskell foldr function?

I was doing the exercises from YAHT's Recursive Datatype section, and found writing the listFoldr function a bit challenging (mainly because I didn't really understand the difference between foldl and foldr at first). When I finally realized exactly how the foldr function worked, I decided that a simple swap of function arguments would b...

syntax in expression - Haskell

Im new to Haskell!! I wrote this code: import Data.List inputIndex :: [String] -> [String] -> Bool inputIndex listx input = and [x `elem` listx |x <- input] inputIndex = if inputIndex == true then putStrLn ("ok") It works fine without the if statement but when I put the if statement the following error is shown: Syntax e...