haskell

Problem with Fibonacci Haskell Implementation

邮差的信 提交于 2019-12-25 01:34:12
问题 Just started re-learning Haskell (did it at uni but forgot most of it) and thought I would implement a Fibonacci function to start of with. However, I keep getting a stackoverflow, even for very small n . Can anyone spot any problems with my function? fib :: Integer -> Integer fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n+1) 回答1: You have an error in your fibonacci formula: fib :: Integer -> Integer fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) Note the very last term where there is n-2

Multiple input values with upper range

試著忘記壹切 提交于 2019-12-25 01:32:28
问题 So I'm in need of a little assistance or at least a point in the right direction! I'm new to Haskell but I'm familiar with C# and PHP. I'm trying to create a FizzBuzz function that allows 3 parameters to be entered. I understand the general concept of FizzBuzz but I'm trying to be able to create a function that allows you to put the first divisor, second divisor and the last parameter as an upper range. The part where I'm struggling to understand is how you assign input values to variables in

Speeding up binary tree traversal with multiple processors Haskell (parallel)

你离开我真会死。 提交于 2019-12-25 01:22:22
问题 Following the examples in Chapter 24 of "Real World Haskell" and Chapter 2 of "Parallel and Concurrent Programming in Haskell", I've managed to build the following functions for building and traversing a binary tree quickly using multiple processors. import Control.Parallel (par, pseq) import Control.DeepSeq data Tree x = Empty | Node x (Tree x) (Tree x) deriving (Show, Read, Eq) -- Create instance of NFData for Tree data type (defining "normal form") instance NFData a => NFData (Tree a)

Function to generate the unique combinations of a list in Haskell

房东的猫 提交于 2019-12-25 01:04:12
问题 Is there a Haskell function that generates all the unique combinations of a given length from a list? Source = [1,2,3] uniqueCombos 2 Source = [[1,2],[1,3],[2,3]] I tried looking in Hoogle but could not find a function that did this specifically. Permutations does not give the desired result. Has anybody used a similar function before? 回答1: I don't know a predefined function either, but it's pretty easy to write yourself: -- Every set contains a unique empty subset. subsets 0 _ = [[]] --

Project euler problem 3 in haskell

拜拜、爱过 提交于 2019-12-24 23:43:00
问题 I'm new in Haskell and try to solve 3 problem from http://projecteuler.net/. The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? My solution: import Data.List getD :: Int -> Int getD x = -- find deviders let deriveList = filter (\y -> (x `mod` y) == 0) [1 .. x] filteredList = filter isSimpleNumber deriveList in maximum filteredList -- Check is nmber simple isSimpleNumber :: Int -> Bool isSimpleNumber x = let deriveList = map (\y -> (x

Extract numbers as strings from list of numbers

感情迁移 提交于 2019-12-24 23:08:22
问题 I'm really not a star with regular expressions, especially with Haskell and even after reading some tutos. I have a list of numbers like this: let x = [1, 2, 3.5] My goal is to have the string "1.0 2.0 3.5" from this input. My idea was to use regular expressions. But the way I use is tedious. First, I do let xstr = show x Then I remove the first bracket like this: import Text.Regex let regex1 = mkRegex "\\[" let sub1 = subRegex regex1 xstr "" -- this gives "1.0,2.0,3.5]" Then I remove the

Command line autocompletion based on context

徘徊边缘 提交于 2019-12-24 22:53:06
问题 I have a following program (and here is the link to the program in an online IDE), purpose of which is to explore Haskell command line autocompletion capabilities: {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-} import System.Console.Haskeline import System.IO import System.IO.Unsafe import Control.Monad.State.Strict import qualified Data.ByteString.Char8 as B import Data.Maybe import Data.List import qualified Data.Map as M data MyDataState = MyDataState {

Issue with recursion writing a tiny parser in Haskell. Check variables

懵懂的女人 提交于 2019-12-24 21:54:29
问题 I'm still working on a tiny parser for a tiny language defined in a task at school. The parser that generates an AST(Abstract syntax tree) is working. What I want is to check the defined variables, they must be bounded by the let expression. First the method that is defined in the task(suggestion, not needed): checkVars :: Expr -> Char data Expr = Var Char | Tall Int | Sum Expr Expr | Mult Expr Expr | Neg Expr | Let Expr Expr Expr deriving(Eq, Show) A valid sentence would be "let X be 5 in *

Haskell: Parsing an object that could be multiple types into one single type

天涯浪子 提交于 2019-12-24 20:53:23
问题 I'm a haskell beginner going through aeson, learning more about both by parsing some data files. Usually when there's a data file, may it be .json , a lua table, .csv format or others, and you want to parse them, there's always a chance of error. For example, a simple .json file like this "root": { "m1": { "key1": "value1", "key2": 2 }, "m2": { "key1": 1 }, } Has two oddities: "m1" has two subkeys, one has a value in String and one in Int . "m2" has only one subkey, and it has same key as the

Haskell type error on compilation

China☆狼群 提交于 2019-12-24 19:40:38
问题 I'm not sure why the following code is causing the following error. Code: type Symbol = Char symbols :: [Symbol] symbols = ['a'..'f'] type Code = [Symbol] members :: Code -> Bool members xs = and [ b | x <- xs, b <- map (elem x) symbols ] Compilation Error: Couldn't match type ‘Char’ with ‘t0 Symbol’ Expected type: [t0 Symbol] Actual type: [Symbol] • In the second argument of ‘map’, namely ‘symbols’ In the expression: map (elem x) symbols In a stmt of a list comprehension: b <- map (elem x)