ghci

Convert a list of digits to a number HASKELL

吃可爱长大的小学妹 提交于 2019-12-13 03:45:41
问题 I want to make a function in haskell that given a list of single digits, i make the full number. I was thinking in using intensive lists and patrons, as the code it follows: funcion5 (x:xs) = [y*(10^w) | y <- (x:xs) w] The idea is, to go over the list and multiplie each digit to 10 pow to the position of the number. Finally i only have to sum all digits and I have the number as this: sum (funcion5 (x:xs)) Can anyone help me, please? Thanks 回答1: This may simply be done by folding with foldl1 :

Function variable not in scope Haskell

人走茶凉 提交于 2019-12-12 17:51:16
问题 Hi i have the following code import Data.Maybe import Test.QuickCheck import System.Random rndExpr :: Gen Expr -> IO Expr rndExpr gen = do rnd <- newStdGen return (generate 5 rnd gen) But i get "not in scope "generate", why is this so? Regards Darren Edit i am importing Test.QuickCheck but it still complaints about the "generate" is not in scope. Edit 2 How would you write this function so that it would work with quickcheck version 2? I simple tried to put "unGen" where generate was with no

Make GHCi load and interpret a module with a “foreign export” declaration (for FFI with C)?

流过昼夜 提交于 2019-12-12 13:09:01
问题 I have a module ( Safe.hs ) with foreign export ccall respond_hs :: CWString -> IO CWString for FFI with C. I'd like to load Safe.hs in GHCi and evaluate some things with it. But ghci fails to load it (I'm specifying two source files because it depends on valencies.lhs ): $ ghci src/valencies.lhs src/Safe.hs GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ...

Inconsistent behavior with fromIntegral in GHCi

时间秒杀一切 提交于 2019-12-12 11:23:50
问题 I was hoping someone could explain the following behavior in GHCi, when using the function fromIntegral: Prelude> let x = 1 :: Integer Prelude> :t x x :: Integer Prelude> sqrt $ fromIntegral x 1.0 Prelude> let y = fromIntegral x Prelude> sqrt y <interactive>:181:1: No instance for (Floating Integer) arising from a use of `sqrt' Possible fix: add an instance declaration for (Floating Integer) In the expression: sqrt y In an equation for `it': it = sqrt y Why does it matter whether I set y and

Requiring sudo to run ghci on OSx

我与影子孤独终老i 提交于 2019-12-12 03:44:46
问题 I've installed the Haskell platform on my OSX (Yosemite). When I try to execute the ghci, the following error occurs: GHCi, version 7.8.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... <command line>: can't load .so/.DLL for: libiconv.dylib (dlopen(libiconv.dylib, 9): image not found) If I execute using sudo (sudo ghci), it runs normally. The same behavior occurs with cabal. I

Deriving the type of (foldr (.))

◇◆丶佛笑我妖孽 提交于 2019-12-12 01:33:17
问题 I'm trying to manually derive the type of (foldr (.)) foldr :: (a1 -> b1 -> b1) -> b1 -> [a1] -> b1 (.) ::(b2 -> c2) -> (a2 -> b2) -> a2 -> c2 Then: a1 ~ (b2 -> c2) b1 ~ (a2 -> b2) b1 ~ a2 So I get that (foldr (.)) :: (a2 -> b2) -> [(b2 -> c2)] -> (a2 -> b2) But GHCi returns: :t (foldr (.)) :: (a -> b) -> [b -> b] -> a -> b Why b2 and c2 are the same? Thanks, Sebastián. 回答1: If you look at the type of (.) as (b2 -> c2) -> (a2 -> b2) -> (a2 -> c2) then b1 ~ (a2 -> b2) b1 ~ (a2 -> c2) so (b2 ~

problems with trivial number conversions in Haskell

旧街凉风 提交于 2019-12-11 11:52:51
问题 I am attempting to write a trivial function to drop the last digit of a number and return the rest of the number. dropLastDigit :: (Integral b) => b -> b dropLastDigit x = (quot x 10) * (floor $ logBase 10 x) however, when I try to load this into ghci, I get: Could not deduce (Floating b) arising from a use of ‘logBase’ from the context (Integral b) bound by the type signature for dropLastDigit :: Integral b => b -> b at haskelljokes.hs:6:18-39 Possible fix: add (Floating b) to the context of

Why is GHCi accepting something at the prompt that won't compile?

Deadly 提交于 2019-12-11 04:29:00
问题 I'm trying to play around with Haskell types, creating a data taking a type constructor and a concrete type (inspired by this). Here is my kung.hs file: data Kung t a = Kung { field :: t a } deriving (Show, Eq) val1 = Kung { field = [1,5] } val2 = Kung { field = Just 3 } --val3 = Kung { field = 3 } It compiles fine and loads ok: *Main> :load C:\Test\Haskell\kung.hs [1 of 1] Compiling Main ( C:\Test\Haskell\kung.hs, interpreted ) Ok, one module loaded. *Main> val1 Kung {field = [1,5]} *Main>

Is it possible to define new ADTs in GHCi

若如初见. 提交于 2019-12-11 03:36:03
问题 While commenting on new features in ghci I wished that ghci had the ability to declare type declaration and declaring new ADT types, someone informed that it was indeed possible, and after searching I found this page which told me I could do let numUniques' :: (Eq a) => [a] -> Int; numUniques' = length . nub Apparently that same sort of syntax works for pattern matching (ex. let a 1=True;a 2=False). Creating ADTs would make it almost perfect? Does anyone know if it is currently possible?

Need help writing prop to blak :: Sudoku → [Pos] (Haskell)

一曲冷凌霜 提交于 2019-12-11 03:07:23
问题 i stumbled over this thread Haskell List Comprehension And am now trying to write a prop for it that states that all cells in this function actually are blank, but have only gotten this far with the following error message when trying to compile it. {- Property that states that all cells in the blanks list are actually blank -} prop_blank_pos :: Sudoku → Bool prop_blank_pos sud = (rows sud) !! (fst pair) !! (snd pair) ≡ Nothing where pair = blanks sud could't match expected type '(a, b)'