haskell

edit AssocList by key

允我心安 提交于 2020-01-22 02:21:07
问题 I'm trying to build a recursive function that replaces the CellType by the Cell. Just like this: > editBoard [((2,2),Mine),((2,3),Mine),((3,2),Mine)]((2, 4), Flag) > [((2,2),Mine),((2,3),Flag),((3,2),Mine)] This is what I have so far: editBoard :: Board -> (Cell, CellType) -> Board editBoard (Board ((x, y):xs)) (a, b) | x == a = (Board ((x, b) : xs)) | otherwise = ((x, y) : editBoard (Board xs) (a, b)) I keep getting an error that says Couldn't match expected type ‘[(Cell, CellType)]’ with

Haskell 笔记 (五) 递归

余生长醉 提交于 2020-01-22 01:48:00
haskell 递归 递归 就是将问题展开为同样的子问题,并不断的对子问题展开,直到抵达问题的基准条件为止。 递归2个要点: 问题如何展开为子问题 定义基准条件 在程序上,问题的展开表现就是函数调用函数自己。基准条件就是结束展开的条件。 求列表最大值 maxNumOfList :: (Ord a)=> [a] -> a maxNumOfList [] = error "empty list" maxNumOfList [x] = x maxNumOfList (x:xs) = max x (maxNumOfList xs) 递归练习 使用递归实现几个haskell已有的函数 replicate 生成一个列表有n个元素的列表,列表个数为第一参数,列表元素为第二参数。 Prelude > replicate 3 5 [ 5,5,5 ] 实现 replicate' :: Int -> a -> [a] replicate' n x | n <= 0 = [] | otherwise = x:replicate' (n-1) x take 从列表中去一定数量的元素, 第一个参数为元素个数,第二个参数为列表 Prelude> take 3 [9,8,7,6,5,4,3] [9,8,7] 实现 take' :: (Num i, Ord i) => i -> [a] -> [a] take' n

How to create a list of list of numbers in Haskell

狂风中的少年 提交于 2020-01-21 19:32:09
问题 I need to create a function that makes a board of size row and column and then populate that with zeros. mkBoard 2 3 would make [[0,0,0],[0,0,0]] I don't really know where to start as I am new to Haskell programming I was thinking that the function would be something like this: mkBoard m n= [m | ???? take n (repeat 0)] But I am not sure if this would be the correct approach. Thank you for your help. 回答1: As @arrowd already mentioned, there is a replicate function for take n (repeat x) . You

How to create a list of list of numbers in Haskell

萝らか妹 提交于 2020-01-21 19:32:06
问题 I need to create a function that makes a board of size row and column and then populate that with zeros. mkBoard 2 3 would make [[0,0,0],[0,0,0]] I don't really know where to start as I am new to Haskell programming I was thinking that the function would be something like this: mkBoard m n= [m | ???? take n (repeat 0)] But I am not sure if this would be the correct approach. Thank you for your help. 回答1: As @arrowd already mentioned, there is a replicate function for take n (repeat x) . You

Haskell Converting Int to Float

青春壹個敷衍的年華 提交于 2020-01-21 11:31:07
问题 I'm having some problem with one of the functions which I'm new at, it's the fromIntegral function. Basically I need to take in two Int arguments and return the percentage of the numbers but when I run my code, it keeps giving me this error: Code: percent :: Int -> Int -> Float percent x y = 100 * ( a `div` b ) where a = fromIntegral x :: Float b = fromIntegral y :: Float Error: No instance for (Integral Float) arising from a use of `div' Possible fix: add an instance declaration for

Acess a servant server with a reflex-dom client

元气小坏坏 提交于 2020-01-21 10:03:10
问题 I'm using version 0.4 of reflex-dom and I have a tiny reflex-dom client: {-# LANGUAGE OverloadedStrings #-} import Reflex.Dom import qualified Data.Text as T import Data.Monoid main :: IO () main = mainWidget body body :: MonadWidget t m => m () body = el "div" $ do pb <- getPostBuild snd <- button "Send" -- Use one of the following URL's: let defReq = "http://localhost:8080/name/3" -- let defReq = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY" let req = XhrRequest "GET" defReq (def {

Acess a servant server with a reflex-dom client

≡放荡痞女 提交于 2020-01-21 10:02:33
问题 I'm using version 0.4 of reflex-dom and I have a tiny reflex-dom client: {-# LANGUAGE OverloadedStrings #-} import Reflex.Dom import qualified Data.Text as T import Data.Monoid main :: IO () main = mainWidget body body :: MonadWidget t m => m () body = el "div" $ do pb <- getPostBuild snd <- button "Send" -- Use one of the following URL's: let defReq = "http://localhost:8080/name/3" -- let defReq = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY" let req = XhrRequest "GET" defReq (def {

Swap the first two element of a list, (Haskell)

拈花ヽ惹草 提交于 2020-01-21 09:10:10
问题 In order to swap the first two elements of a list, I've written the following code: swap_first_two_elements :: [a]->[a] swap_first_two_elements list=case list of x:y:_ -> y:x:_ [x] -> Nothing []-> Nothing However, the terminal shows the error shown below: [1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:3:16: Pattern syntax in expression context: _ Failed, modules loaded: none. Prelude> Who likes to tell me what is wrong with it? Btw, ive also tried to combine the last two row into:

When can eta reduction change a function's type?

喜欢而已 提交于 2020-01-21 07:03:51
问题 What exactly is going on with the following? > let test = map show > :t test test :: [()] -> [String] > :t (map show) (map show) :: Show a => [a] -> [String] I am wondering how I failed to notice this before? I actually encountered the problem with "map fromIntegral" rather than show - my code doesn't compile with the pointfree form, but works fine without eta reduction. Is there a simple explanation of when eta reduction can change the meaning of Haskell code? 回答1: This is the monomorphism

Char to string function

爱⌒轻易说出口 提交于 2020-01-21 06:08:32
问题 I have a very simple question: Given a function accepting a char and returning a string test :: Char -> [String] how can one convert the char into a string? I'm confused over the two types. 回答1: In Haskell String is an alias for [Char] : type String = [Char] If you just want a function that converts a single char to a string you could e.g. do charToString :: Char -> String charToString c = [c] If you prefer pointfree style you could also write charToString :: Char -> String charToString = (:[