Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1022313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:26:48+00:00 2026-05-16T11:26:48+00:00

I am new to Haskell,I should write a function that takes a function among

  • 0

I am new to Haskell,I should write a function that takes a function among its parameter, uses it and return a function whose behavior change depending the function I passed in,
to be clear: in my application: a simple cache server, I read a context file and among all the parameter ther is “LOG: active”if log is active I have to give the log on the screen for debugfing purpose otherwise I write nothing, I do not want to use a chain of if-then, thus I thought of writing a function

setLogging a = do
   if a
   then let logging x = putStrLn x
    in  return logging
   else let logging x = putStrLn ""
        in return logging

I use it in this way doLog <- setLogging True and it works, I have problem with the function that should return the function for removing
the old elements from my cache: how many element it must contain is determined by the context; this is my function

--ritorna la funzione che riduce la cache in funzione dell environment
setTrimming a = do
     if a=="active"
          then let trimming c logging = reduceCache 9  logging c --è attivo lowbandwidth
               in return trimming
          else let trimming c logging = reduceCache 5  logging c --non è attivo lowbandwidth
               in return trimming

reduceCache  s log cache
    | (M.size cache) >= s = do
         log "LOG: ciao"
         reduceCache s log (snd (M.deleteFindMin cache))
    | otherwise =  cache

when I use this function with doTrimm <- setTrimming "active" I got this error

ghc --make "trimmer.hs" (in directory: /media/Volume/doc/progetti/haskell)
[1 of 1] Compiling Main             ( trimmer.hs, trimmer.o )
trimmer.hs:33:31:
Couldn't match expected type `t1 -> t'
       against inferred type `M.Map k a'
In the expression: reduceCacheLow 9 logging c
In the definition of `trimming':
    trimming c logging = reduceCacheLow 9 logging c
In the expression:
    let trimming c logging = reduceCacheLow 9 logging c
    in return trimming
trimmer.hs:35:30:
Couldn't match expected type `t1 -> t'
       against inferred type `M.Map k a'
In the expression: reduceCacheHigh 4 logging c
In the definition of `trimming':
    trimming c logging = reduceCacheHigh 4 logging c
In the expression:
    let trimming c logging = reduceCacheHigh 4 logging c
    in return trimming
Compilation failed.

how can I fix this program?
thanks in advance

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:26:49+00:00Added an answer on May 16, 2026 at 11:26 am

    It might help to start by writing out the type of reduceCache. Your current version has the following type:

    reduceCache :: (Monad (M.Map k)) => Int -> ([Char] -> M.Map k a1) -> M.Map k a -> M.Map k a
    

    But we know that the second argument is a logging function of type String -> IO (), so something’s wrong. The following type makes more sense:

    reduceCache :: Int -> (String -> IO ()) -> M.Map k v -> IO (M.Map k v)
    

    If we’re executing the logging function, we know we need to end up in the IO monad. We only need to make one change to your implementation to match the new signature:

    reduceCache s log' cache
      | M.size cache >= s = do
        log' "LOG: ciao"
        reduceCache s log' (snd $ M.deleteFindMin cache)
      | otherwise = return cache
    

    We can also simplify the set... functions:

    setLogging :: Bool -> String -> IO ()
    setLogging True = putStrLn
    setLogging False = const $ return ()
    
    setTrimming :: String -> M.Map k v -> (String -> IO ()) -> IO (M.Map k v)
    setTrimming "active" = \c logging -> reduceCache 9 logging c
    setTrimming _        = \c logging -> reduceCache 5 logging c
    

    This should do what you want. Note that setLogging and setTrimming are no longer monadic, so you’d just write doLog = setLogging True (or let doLog = setLogging True in a do-expression).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm new in Haskell. I have to write function that takes list of Integers
I am trying to write a list function that takes a simple list and
I'm new to Haskell and Snap and I want to write a simple bug-tracking
I'm developing a small haskell program that uses an external static library I've developed
I am new to Haskell and facing a "cannot construct infinite type" error that
I'm fairly new to Haskell and have been slowly getting the idea that there's
[Disclaimer] I am very new to Haskell (and any FPL for that matter), just
I am new at haskell, I have to write a program context-aware,so I thought
I am new to Haskell programming, Foreign Function Interface and Stackoverflow. I am trying
I'm new to Haskell and so far quite enjoying it. One thing that strikes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.