haskell

***Exception: Prelude.head empty List. What is the base case?

百般思念 提交于 2020-01-15 05:03:46
问题 I am writing a function of getting the product of 2 matrix. transpose' :: [[a]] -> [[a]] transpose' [[]] = [] transpose' m = (map head m) : transpose' (map tail m) dotProduct :: [Int] -> [Int] -> Int dotProduct [] [] = 0 dotProduct (x:xs) (y:ys) = (x*y) + dotProduct xs ys matrixProduct :: [[Int]] -> [[Int]] -> [[Int]] matrixProduct [] _ = [] matrixProduct (x:xs) y = (map (dotProduct x) (transpose' y)):matrixProduct xs y I always get something like [[2,2, ***Exception: Prelude.head empty List

Iteratively printing every integer in a List

末鹿安然 提交于 2020-01-15 04:29:05
问题 Say I have a List of integers l = [1,2] Which I want to print to stdout . Doing print l produces [1,2] Say I want to print the list without the braces map print l produces No instance for (Show (IO ())) arising from a use of `print' Possible fix: add an instance declaration for (Show (IO ())) In a stmt of an interactive GHCi command: print it `:t print print :: Show a => a -> IO () So while I thought this would work I went ahead and tried: map putStr $ map show l Since I suspected a type

Iteratively printing every integer in a List

人走茶凉 提交于 2020-01-15 04:28:07
问题 Say I have a List of integers l = [1,2] Which I want to print to stdout . Doing print l produces [1,2] Say I want to print the list without the braces map print l produces No instance for (Show (IO ())) arising from a use of `print' Possible fix: add an instance declaration for (Show (IO ())) In a stmt of an interactive GHCi command: print it `:t print print :: Show a => a -> IO () So while I thought this would work I went ahead and tried: map putStr $ map show l Since I suspected a type

GHC Overlapping instances when generalising addition

会有一股神秘感。 提交于 2020-01-15 03:27:11
问题 Trying to generalise (+) to more than just Num s, I wrote a up an Addable class: {-# LANGUAGE FlexibleContexts, FlexibleInstances, UndecidableInstances #-} class Addable a where (+) :: Addable a => a -> a -> a instance Addable [a] where (+) = (++) instance Num a => Addable a where (+) = (Prelude.+) When trying to add (concatenate) lists, GHC complains about overlapping instances: *Test> "abc" + "defghi" <interactive>:84:7: Overlapping instances for Addable [Char] arising from a use of `+'

Find Sum of leaves

六月ゝ 毕业季﹏ 提交于 2020-01-15 03:13:29
问题 I am supposed to write a code such that A polymorphic tree type with nodes of an arbitrary number of children might be represented as follows (note that the leaves store a list, and interior nodes store list of “ListTree”s): data ListTree a = ListLEAF [a] | ListNODE [(ListTree a)] deriving (Show, Read, Eq) Write a function foldListTree that takes a function ( f ), a base value ( base ), and a ListTree ( t ) and combines the values in the lists of the leaf notes in tree t by applying function

How to filter list elements in Haskell based on previous value in the list?

柔情痞子 提交于 2020-01-15 02:55:31
问题 I'm working on creating a function in Haskell that filters the numbers of a list on a condition based on the previous element in the list. Example the previous number is a multiple of 2 myFunction [1, 2, 5, 6, 3] # expected output: [5,3] I know how to apply filter but so far I have seen that the filters take only one argument at a time. I tried with scanl1 , foldl1 , and map but I'm new to Haskell and I have not been able to do so; any clue? 回答1: Edit It should be: myFunction [] = []

SDL-Mixer audio stops upon starting Reactive-Banana input loop

我们两清 提交于 2020-01-14 22:52:43
问题 I've been working on a game that uses multiple audio tracks whose volumes are adjusted in realtime based on mouse motion. I'm using SDl-Mixer for audio, and Reactive-Banana for the game in general. The problem is that the tracks, which have all been started at the beginning, stop playing when the input-loop starts. The cause may be something else, but I wonder if there's some strange interaction between SDL and Reactive-Banana that I don't understand. I've been trying to puzzle this out for a

SDL-Mixer audio stops upon starting Reactive-Banana input loop

孤人 提交于 2020-01-14 22:51:33
问题 I've been working on a game that uses multiple audio tracks whose volumes are adjusted in realtime based on mouse motion. I'm using SDl-Mixer for audio, and Reactive-Banana for the game in general. The problem is that the tracks, which have all been started at the beginning, stop playing when the input-loop starts. The cause may be something else, but I wonder if there's some strange interaction between SDL and Reactive-Banana that I don't understand. I've been trying to puzzle this out for a

SDL-Mixer audio stops upon starting Reactive-Banana input loop

♀尐吖头ヾ 提交于 2020-01-14 22:51:27
问题 I've been working on a game that uses multiple audio tracks whose volumes are adjusted in realtime based on mouse motion. I'm using SDl-Mixer for audio, and Reactive-Banana for the game in general. The problem is that the tracks, which have all been started at the beginning, stop playing when the input-loop starts. The cause may be something else, but I wonder if there's some strange interaction between SDL and Reactive-Banana that I don't understand. I've been trying to puzzle this out for a

Run Length Encoding in Haskell

空扰寡人 提交于 2020-01-14 19:46:35
问题 import Data.List data Encoding = Multiple Int Char | Single Char deriving (Eq,Show,Ord) Encoding of run length encode :: String -> [Encoding] encode inputString =encoding (group inputString) [] encoding :: [String] -> [Encoding] -> [Encoding] encoding groupString xs= if (length groupString == 0) then xs else case (head groupString) of ([c]) ->encoding (tail groupString) (xs ++ [Single c]) (x) -> encoding (tail groupString) (xs ++ [Multiple (length x) (head x)]) Decoding of run length decode :