haskell

Haskell parsing prefix evaluation

我只是一个虾纸丫 提交于 2020-01-07 11:33:53
问题 I'm struggling with this code. import Data.Char (isDigit) data Ast = V Int | Neg Ast | A Ast Ast | M Ast Ast deriving (Show,Eq) parseE ("+":xs) = let (e1,r1) = parseE xs; (e2,r2) = parseE r1 in (A e1 e2, r2) parseE ("*":xs) = let (e1,r1) = parseE xs; (e2,r2) = parseE r1 in (M e1 e2, r2) parseE ("-":xs) = let (a,r) = parseE r in (N a, r) parseE ("(":xs) = let (a,")":r) = parseE r in (a,r) parseE (x:xs) = (V (read x :: Int), xs) eval xs = parseE xs When my input is something like: * + 1 2 * 3 +

Haskell parsing prefix evaluation

拜拜、爱过 提交于 2020-01-07 11:29:59
问题 I'm struggling with this code. import Data.Char (isDigit) data Ast = V Int | Neg Ast | A Ast Ast | M Ast Ast deriving (Show,Eq) parseE ("+":xs) = let (e1,r1) = parseE xs; (e2,r2) = parseE r1 in (A e1 e2, r2) parseE ("*":xs) = let (e1,r1) = parseE xs; (e2,r2) = parseE r1 in (M e1 e2, r2) parseE ("-":xs) = let (a,r) = parseE r in (N a, r) parseE ("(":xs) = let (a,")":r) = parseE r in (a,r) parseE (x:xs) = (V (read x :: Int), xs) eval xs = parseE xs When my input is something like: * + 1 2 * 3 +

How to define a function using foldr for rpn in haskell? [closed]

核能气质少年 提交于 2020-01-07 09:57:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed yesterday . i am in the process of learning haskell and i am currently trying to define a function using foldr for an rpn evaluator. this is the function, it takes in a list of strings and returns it as an int any help is appreciated. rpnRec :: [String] -> Int for example: rpnRec ["2", "4", "+"] = 6 however i

Matrix division in Haskell

醉酒当歌 提交于 2020-01-07 09:44:09
问题 What is wrong? This code is supposed to do matrix division by finding a times b inverse. I try to understand what its errors are but I fail to see how to link it. import List import Ratio inverse :: [[Rational]] -> [[Rational]] inverse mat = sweep ([], zipWith (++) mat unit) where unit = map (take (length mat)) $ iterate (0 :) (1 : [0,0..]) sweep (xss, []) = xss sweep (xss, yss) = sweep (xss' ++ [ws], filter (any (/= 0)) yss') where Just (x : xs) = find ((/= 0) . head) yss ws = map (/ x) xs

Matrix division in Haskell

狂风中的少年 提交于 2020-01-07 09:44:03
问题 What is wrong? This code is supposed to do matrix division by finding a times b inverse. I try to understand what its errors are but I fail to see how to link it. import List import Ratio inverse :: [[Rational]] -> [[Rational]] inverse mat = sweep ([], zipWith (++) mat unit) where unit = map (take (length mat)) $ iterate (0 :) (1 : [0,0..]) sweep (xss, []) = xss sweep (xss, yss) = sweep (xss' ++ [ws], filter (any (/= 0)) yss') where Just (x : xs) = find ((/= 0) . head) yss ws = map (/ x) xs

Haskell. Numbers in binary numbers. words

一世执手 提交于 2020-01-07 08:23:41
问题 import Data.Char blockCode :: S lett2num :: Char -> Int lett2num y | (or num2bin :: Int -> [Int] num2bin n: negative number" where n2b 0 = [] n2b n = n `mod` 2 : n2b (n `div` 2) 回答1: You can use concatMap show to transform a list into a string: Main> num2bin 8 [0,0,0,1] Main> concatMap show $ num2bin 8 "0001" but note that your function's output is reversed. To do everything in one go, do num2bin :: Int -> String num2bin n | n >= 0 = concatMap show $ reverse $ n2b n | otherwise = error

How to read/ add/ print a number string in Haskell

两盒软妹~` 提交于 2020-01-07 08:22:12
问题 Below if my code right now. I want to be able to take in user input like the following: "6 1 2 3 4 5 6" and the get the sum and print. it would also be cool to understand how to use the first number entered as the total numbers. SO here the first number is 6 and the total numbers inputted is 6. Thank you in advance for helping me with this. I have been researching for weeks and cannot figure this out. main = do putStrLn "Enter how many numbers:" -- clearer num<-getLine putStrLn("Enter a

`gcc.exe' failed in phase `C Compiler'. (Exit code: 1)

亡梦爱人 提交于 2020-01-07 08:11:10
问题 When Cabal or Stack install hackage the error message like title. Full mes is: cabal install ghc-mod realgcc.exe: error: C:\Users\浠ユ亽\AppData\Local\Temp\4118467.c: No such file or directory realgcc.exe: warning: '-x c' after last input file has no effect realgcc.exe: fatal error: no input files compilation terminated. gcc.exe' failed in phase C Compiler'. (Exit code: 1) 回答1: Try doing 'stack setup--reinstall'. I had a similar message cleaned up this way. 来源: https://stackoverflow.com

How to build Stack Yesod?

孤街浪徒 提交于 2020-01-07 06:49:46
问题 I've been trying to use stack to start a yesod-simple project, but nothing I do will build this project. I've looked into other similar stackoverflow questions but couldn't get this to work. Can some one help me figure out what I need to do to get started? Error > stack build Populated index cache. yesod-persistent-1.4.0.6: configure yesod-persistent-1.4.0.6: build yesod-persistent-1.4.0.6: copy/register yesod-form-1.4.9: configure yesod-form-1.4.9: build yesod-form-1.4.9: copy/register yesod

empty space work-around in haskell

拟墨画扇 提交于 2020-01-07 05:12:06
问题 I've been making little baby programs with haskell about i/o and some other stuff and i came out something interesting. I wonder that in a file, can we process empty lines? For example can we count the number of empty lines or can we retrieve data between two empty lines? If so how can we implement such a code fragment? I am really new with haskell and it is getting me very hard to understand the syntax of the language so i really need some help to learn it. I tried like ; readFile "/tmp/foo